Class: Kraps::RedisQueue
- Inherits:
-
Object
- Object
- Kraps::RedisQueue
- Defined in:
- lib/kraps/redis_queue.rb
Constant Summary collapse
- VISIBILITY_TIMEOUT =
60
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #dequeue ⇒ Object
- #enqueue(payload) ⇒ Object
-
#initialize(redis:, token:, namespace:, ttl:) ⇒ RedisQueue
constructor
A new instance of RedisQueue.
- #size ⇒ Object
- #stop ⇒ Object
- #stopped? ⇒ Boolean
Constructor Details
#initialize(redis:, token:, namespace:, ttl:) ⇒ RedisQueue
Returns a new instance of RedisQueue.
7 8 9 10 11 12 |
# File 'lib/kraps/redis_queue.rb', line 7 def initialize(redis:, token:, namespace:, ttl:) @redis = redis @token = token @namespace = namespace @ttl = ttl end |
Instance Attribute Details
#token ⇒ Object (readonly)
Returns the value of attribute token.
5 6 7 |
# File 'lib/kraps/redis_queue.rb', line 5 def token @token end |
Instance Method Details
#dequeue ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/kraps/redis_queue.rb', line 42 def dequeue @dequeue_script ||= <<~SCRIPT local queue_key, pending_key, status_key, ttl, visibility_timeout = ARGV[1], ARGV[2], ARGV[3], tonumber(ARGV[4]), tonumber(ARGV[5]) local zitem = redis.call('zrange', pending_key, 0, 0, 'WITHSCORES') local job = zitem[1] if not zitem[2] or tonumber(zitem[2]) > tonumber(redis.call('time')[1]) then job = redis.call('lpop', queue_key) end redis.call('expire', queue_key, ttl) redis.call('expire', pending_key, ttl) redis.call('expire', status_key, ttl) if not job then return nil end redis.call('zadd', pending_key, tonumber(redis.call('time')[1]) + visibility_timeout, job) redis.call('expire', pending_key, ttl) return job SCRIPT job = @redis.eval(@dequeue_script, argv: [redis_queue_key, redis_pending_key, redis_status_key, @ttl, VISIBILITY_TIMEOUT]) unless job yield(nil) return end keep_alive(job) do yield(JSON.parse(job)) if job end @remove_script ||= <<~SCRIPT local queue_key, pending_key, status_key, ttl, job = ARGV[1], ARGV[2], ARGV[3], tonumber(ARGV[4]), ARGV[5] redis.call('zrem', pending_key, job) redis.call('expire', queue_key, ttl) redis.call('expire', pending_key, ttl) redis.call('expire', status_key, ttl) SCRIPT @redis.eval(@remove_script, argv: [redis_queue_key, redis_pending_key, redis_status_key, @ttl, job]) end |
#enqueue(payload) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/kraps/redis_queue.rb', line 28 def enqueue(payload) @enqueue_script ||= <<~SCRIPT local queue_key, pending_key, status_key, ttl, job = ARGV[1], ARGV[2], ARGV[3], tonumber(ARGV[4]), ARGV[5] redis.call('rpush', queue_key, job) redis.call('expire', queue_key, ttl) redis.call('expire', pending_key, ttl) redis.call('expire', status_key, ttl) SCRIPT @redis.eval(@enqueue_script, argv: [redis_queue_key, redis_pending_key, redis_status_key, @ttl, JSON.generate(payload)]) end |
#size ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/kraps/redis_queue.rb', line 14 def size @size_script ||= <<~SCRIPT local queue_key, pending_key, status_key, ttl, job = ARGV[1], ARGV[2], ARGV[3], tonumber(ARGV[4]), ARGV[5] redis.call('expire', queue_key, ttl) redis.call('expire', pending_key, ttl) redis.call('expire', status_key, ttl) return redis.call('llen', queue_key) + redis.call('zcard', pending_key) SCRIPT @redis.eval(@size_script, argv: [redis_queue_key, redis_pending_key, redis_status_key, @ttl]) end |
#stop ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/kraps/redis_queue.rb', line 89 def stop @stop_script ||= <<~SCRIPT local queue_key, pending_key, status_key, ttl = ARGV[1], ARGV[2], ARGV[3], tonumber(ARGV[4]) redis.call('hset', status_key, 'stopped', 1) redis.call('expire', queue_key, ttl) redis.call('expire', pending_key, ttl) redis.call('expire', status_key, ttl) SCRIPT @redis.eval(@stop_script, argv: [redis_queue_key, redis_pending_key, redis_status_key, @ttl]) end |
#stopped? ⇒ Boolean
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/kraps/redis_queue.rb', line 103 def stopped? @stopped_script ||= <<~SCRIPT local queue_key, pending_key, status_key, ttl = ARGV[1], ARGV[2], ARGV[3], tonumber(ARGV[4]) redis.call('expire', queue_key, ttl) redis.call('expire', pending_key, ttl) redis.call('expire', status_key, ttl) return redis.call('hget', status_key, 'stopped') SCRIPT @redis.eval(@stopped_script, argv: [redis_queue_key, redis_pending_key, redis_status_key, @ttl]).to_i == 1 end |