redis_queue 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/redis_queue.rb +39 -32
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f49c187b5bb4deb1ca5849534ff7ee9760bc2fcd
|
4
|
+
data.tar.gz: 10468e1f322dc6a4e68fc5f385ed04c79d28829c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce68bfda0e8abdbd3a5b189f47d0496004808e1be7f320349127fc6ff749a0cb66d25d75f0537b8158a6dfaf3e7a1aa8157747f26bc4542afbf0a4bc3c8aa5a2
|
7
|
+
data.tar.gz: 9ee5a1184dacaa73a10906aa03587a6a37eb50599c5e6a56a1d9ecea72fafe7d15c3c2b2a445b40924fb3c06c6c6eb07ad94d95f5cac9c5e12b67c142ea6113a
|
data/lib/redis_queue.rb
CHANGED
@@ -18,7 +18,7 @@ class RedisQueue
|
|
18
18
|
message = redis.call('lpop', ARGV[1])
|
19
19
|
end
|
20
20
|
if message then
|
21
|
-
redis.call('
|
21
|
+
redis.call('hset', ARGV[1]..'_in_use', message, ARGV[2])
|
22
22
|
end
|
23
23
|
return message
|
24
24
|
"'',
|
@@ -34,25 +34,30 @@ class RedisQueue
|
|
34
34
|
"'',
|
35
35
|
repush: ''"
|
36
36
|
#{PUSH_CODE}
|
37
|
-
redis.call('
|
37
|
+
redis.call('hdel', ARGV[1]..'_in_use', ARGV[2])
|
38
38
|
"'',
|
39
39
|
fail: ''"
|
40
|
-
redis.call('
|
41
|
-
redis.call('
|
40
|
+
redis.call('hset', ARGV[1]..'_failed', ARGV[2], ARGV[3])
|
41
|
+
redis.call('hdel', ARGV[1]..'_in_use', ARGV[2])
|
42
42
|
"'',
|
43
43
|
done: ''"
|
44
|
-
redis.call('
|
45
|
-
redis.call('
|
44
|
+
redis.call('hset', ARGV[1]..'_done', ARGV[2], ARGV[3])
|
45
|
+
redis.call('hdel', ARGV[1]..'_in_use', ARGV[2])
|
46
46
|
"'',
|
47
47
|
unpop: ''"
|
48
48
|
redis.call('lpush', ARGV[1], ARGV[2])
|
49
|
-
redis.call('
|
49
|
+
redis.call('hdel', ARGV[1]..'_in_use', ARGV[2])
|
50
50
|
"'',
|
51
51
|
init_from: ''"
|
52
|
-
local vals = redis.call('
|
52
|
+
local vals = redis.call('hkeys', ARGV[2])
|
53
53
|
for i = 1, table.getn(vals) do
|
54
|
-
redis.call('
|
55
|
-
|
54
|
+
local timestamp = redis.call('hget', ARGV[2], vals[i])
|
55
|
+
if timestamp < ARGV[3] then
|
56
|
+
redis.call('lpush', ARGV[1], vals[i])
|
57
|
+
redis.call('hdel', ARGV[2], vals[i])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
"''
|
56
61
|
}.freeze
|
57
62
|
|
58
63
|
def initialize(args = {})
|
@@ -66,7 +71,7 @@ class RedisQueue
|
|
66
71
|
def pop(block: true)
|
67
72
|
return nonblpop unless block
|
68
73
|
message = blpop
|
69
|
-
@redis.run { |redis| redis.
|
74
|
+
@redis.run { |redis| redis.hset "#{@id}_in_use", message, now } if message
|
70
75
|
message
|
71
76
|
end
|
72
77
|
|
@@ -75,11 +80,11 @@ class RedisQueue
|
|
75
80
|
end
|
76
81
|
|
77
82
|
def fail(message)
|
78
|
-
script :fail, @id, message
|
83
|
+
script :fail, @id, message, now
|
79
84
|
end
|
80
85
|
|
81
86
|
def done(message)
|
82
|
-
script :done, @id, message
|
87
|
+
script :done, @id, message, now
|
83
88
|
end
|
84
89
|
|
85
90
|
def unpop(message)
|
@@ -91,7 +96,7 @@ class RedisQueue
|
|
91
96
|
end
|
92
97
|
|
93
98
|
def forget(message)
|
94
|
-
@redis.run { |redis| redis.
|
99
|
+
@redis.run { |redis| redis.hdel "#{@id}_in_use", message }
|
95
100
|
end
|
96
101
|
|
97
102
|
def remove(message)
|
@@ -105,18 +110,12 @@ class RedisQueue
|
|
105
110
|
message
|
106
111
|
end
|
107
112
|
|
108
|
-
def reset
|
109
|
-
init_from "#{@id}_in_use"
|
110
|
-
@redis.run { |redis| redis.del "#{@id}_in_use" }
|
113
|
+
def reset(older_than: nil)
|
114
|
+
init_from "#{@id}_in_use", older_than
|
111
115
|
end
|
112
116
|
|
113
117
|
def restart
|
114
118
|
init_from "#{@id}_done"
|
115
|
-
@redis.run { |redis| redis.del "#{@id}_done" }
|
116
|
-
end
|
117
|
-
|
118
|
-
def init_from(set)
|
119
|
-
script(:init_from, @id, set)
|
120
119
|
end
|
121
120
|
|
122
121
|
def size
|
@@ -124,15 +123,15 @@ class RedisQueue
|
|
124
123
|
end
|
125
124
|
|
126
125
|
def done_size
|
127
|
-
@redis.run { |redis| redis.
|
126
|
+
@redis.run { |redis| redis.hlen "#{@id}_done" }.to_i
|
128
127
|
end
|
129
128
|
|
130
129
|
def failed_size
|
131
|
-
@redis.run { |redis| redis.
|
130
|
+
@redis.run { |redis| redis.hlen "#{@id}_failed" }.to_i
|
132
131
|
end
|
133
132
|
|
134
133
|
def in_use_size
|
135
|
-
@redis.run { |redis| redis.
|
134
|
+
@redis.run { |redis| redis.hlen "#{@id}_in_use" }.to_i
|
136
135
|
end
|
137
136
|
|
138
137
|
def list
|
@@ -140,15 +139,15 @@ class RedisQueue
|
|
140
139
|
end
|
141
140
|
|
142
141
|
def done_list
|
143
|
-
@redis.run { |redis| redis.
|
142
|
+
Hash[@redis.run { |redis| redis.hgetall "#{@id}_done" }]
|
144
143
|
end
|
145
144
|
|
146
145
|
def failed_list
|
147
|
-
@redis.run { |redis| redis.
|
146
|
+
Hash[@redis.run { |redis| redis.hgetall "#{@id}_failed" }]
|
148
147
|
end
|
149
148
|
|
150
149
|
def in_use_list
|
151
|
-
@redis.run { |redis| redis.
|
150
|
+
Hash[@redis.run { |redis| redis.hgetall "#{@id}_in_use" }]
|
152
151
|
end
|
153
152
|
|
154
153
|
def print_stats
|
@@ -177,20 +176,24 @@ class RedisQueue
|
|
177
176
|
private
|
178
177
|
|
179
178
|
def blpop
|
180
|
-
|
179
|
+
loop do
|
181
180
|
message = @redis_blocking.run { |redis| redis.blpop(@id) }.last
|
182
|
-
|
183
|
-
|
181
|
+
return message unless message == ''
|
182
|
+
end
|
184
183
|
end
|
185
184
|
|
186
185
|
def nonblpop
|
187
|
-
script :nonblpop, @id
|
186
|
+
script :nonblpop, @id, now
|
188
187
|
end
|
189
188
|
|
190
189
|
def nonbltouch
|
191
190
|
script :touch, @id
|
192
191
|
end
|
193
192
|
|
193
|
+
def init_from(key, older_than = nil)
|
194
|
+
script(:init_from, @id, key, older_than || now + 100_000)
|
195
|
+
end
|
196
|
+
|
194
197
|
def load_scripts
|
195
198
|
@scripts = {}
|
196
199
|
@redis.run do |redis|
|
@@ -203,6 +206,10 @@ class RedisQueue
|
|
203
206
|
def script(name, *args)
|
204
207
|
@redis.run { |redis| redis.evalsha @scripts[name], argv: args }
|
205
208
|
end
|
209
|
+
|
210
|
+
def now
|
211
|
+
(Time.now.to_f * 1000).to_i
|
212
|
+
end
|
206
213
|
end
|
207
214
|
|
208
215
|
require_relative 'redis_connection'
|