Class: Redis::Promise

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/promise.rb,
lib/redis/promise/resque.rb,
lib/redis/promise/version.rb,
lib/redis/promise/resolver.rb

Overview

rubocop:disable Style/StaticClass

Defined Under Namespace

Modules: Resque Classes: Error, RejectedError, Resolver, TimeoutError

Constant Summary collapse

VERSION =
'0.1.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, id: SecureRandom.uuid_v4, namespace: 'global', key: "promise:#{namespace}:#{id}") ⇒ Promise

: (Redis, ?id: String, ?namespace: String, ?key: String) -> void



56
57
58
59
# File 'lib/redis/promise.rb', line 56

def initialize(redis, id: SecureRandom.uuid_v4, namespace: 'global', key: "promise:#{namespace}:#{id}")
  @redis = redis.dup #: Redis
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

: String



53
54
55
# File 'lib/redis/promise.rb', line 53

def key
  @key
end

#redisObject (readonly)

: Redis



50
51
52
# File 'lib/redis/promise.rb', line 50

def redis
  @redis
end

Class Method Details

.create(redis, id: SecureRandom.uuid_v4, namespace: 'global') ⇒ Object

Create a pair of Promise and Resolver objects for the same key. The resolver can be used to either resolve or reject the promise. The promise can be used to wait for the result.

: (Redis, ?id: String, ?namespace: String) -> [Promise, Resolver]



39
40
41
42
43
44
45
46
# File 'lib/redis/promise.rb', line 39

def create(redis, id: SecureRandom.uuid_v4, namespace: 'global')
  key = "promise:#{namespace}:#{id}"

  promise = Promise.new(redis, key: key)
  resolver = Resolver.new(redis, key: key)

  [promise, resolver]
end

Instance Method Details

#await(timeout: 0) ⇒ Object

Blocks the current thread until the resolved or rejected value is available. By default it blocks indefinitely. If the promise has been successfully resolved its value gets returned. If the promise has been rejected a ‘Redis::Promise::RejectedError` gets thrown.

You can provide an optional ‘timeout:` argument that is a float of number of seconds the client should wait for the value. If the timeout is exceeded a `Redis::Promise::TimeoutError` is thrown.

: (?timeout: Float | Integer) -> untyped

Raises:



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/redis/promise.rb', line 71

def await(timeout: 0)
  result = @redis.blpop(@key, timeout: timeout)
  raise TimeoutError unless result

  parsed = JSON.parse(result[1], symbolize_names: true)

  err = parsed[:err]
  raise RejectedError.new(err) if err # rubocop:disable Style/RaiseArgs

  parsed[:value]
end