Class: Hyrax::RedisEventStore

Inherits:
EventStore show all
Defined in:
lib/hyrax/redis_event_store.rb

Overview

TODO:

stop swallowing all errors! let clients determine how to handle failure cases.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EventStore

for, #initialize, logger

Constructor Details

This class inherits a constructor from Hyrax::EventStore

Class Method Details

.create(action, timestamp) ⇒ Fixnum?

Returns the id of the event, or ‘nil` on failure(?!).

Returns:

  • (Fixnum, nil)

    the id of the event, or ‘nil` on failure(?!)



9
10
11
12
13
14
15
16
17
18
# File 'lib/hyrax/redis_event_store.rb', line 9

def create(action, timestamp)
  instance.then do |redis|
    event_id = redis.incr("events:latest_id")
    redis.hmset("events:#{event_id}", "action", action, "timestamp", timestamp)
    event_id
  end
rescue Redis::CommandError => e
  logger.error("unable to create event: #{e}")
  nil
end

.instanceRedis

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

this is NOT a singleton-ilke ‘.instance` method, it returns a `Redis` client.

Returns:

  • (Redis)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hyrax/redis_event_store.rb', line 27

def instance
  if  Hyrax.config.redis_connection&.is_a?(Redis::Namespace)
    c = Hyrax.config.redis_connection
    c.namespace = namespace
    c
  elsif Hyrax.config.redis_connection
    Hyrax.config.redis_connection
  else
    Redis::Namespace.new(namespace, redis: Redis.new)
  end
end

.namespaceString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


42
43
44
# File 'lib/hyrax/redis_event_store.rb', line 42

def namespace
  Hyrax.config.redis_namespace
end

Instance Method Details

#fetch(size) ⇒ Enumerable<Hash<Symbol, String>>

Parameters:

  • size (Integer)

Returns:

  • (Enumerable<Hash<Symbol, String>>)


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hyrax/redis_event_store.rb', line 51

def fetch(size)
  Hyrax::RedisEventStore.instance.then do |redis|
    redis.lrange(@key, 0, size).map do |event_id|
      {
        action: redis.hget("events:#{event_id}", "action"),
        timestamp: redis.hget("events:#{event_id}", "timestamp")
      }
    end
  end
rescue Redis::CommandError, Redis::CannotConnectError
  RedisEventStore.logger.error("unable to fetch event: #{@key}")
  []
end

#push(value) ⇒ Integer?

Adds a value to the end of a list identified by key

Parameters:

  • value (Integer)

Returns:

  • (Integer, nil)

    the value successfully pushed; or ‘nil` on failure(!?)



71
72
73
74
75
76
# File 'lib/hyrax/redis_event_store.rb', line 71

def push(value)
  Hyrax::RedisEventStore.instance.then { |r| r.lpush(@key, value) }
rescue Redis::CommandError, Redis::CannotConnectError
  RedisEventStore.logger.error("unable to push event: #{@key}")
  nil
end