redis_queue 0.1.2 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/redis_connection.rb +3 -3
- data/lib/redis_queue.rb +21 -20
- 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: 068a0a6c1728f9f5f807aa9894d893594587f868
|
4
|
+
data.tar.gz: a534194a5007445263ef9a3326b74399ad6a1703
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87d378af57edf944f92bce2ec9b49af8728d6273d9e70666b71ae7f97f2054141df19f9fb7d9cc254cc7d6724e0da170b218245e27427ab940dd29d3572295b4
|
7
|
+
data.tar.gz: 0884f179db7552beacdd41b5247664516b995c40347c2bedd47cb42779af5764e2d04deb899fc41245089400833c88dda1430f6ba3272f84296334dabb07d416
|
data/lib/redis_connection.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'redis'
|
2
2
|
|
3
3
|
class RedisConnection
|
4
|
-
def initialize
|
5
|
-
@
|
4
|
+
def initialize args
|
5
|
+
@args = args
|
6
6
|
end
|
7
7
|
|
8
8
|
def run
|
@@ -20,6 +20,6 @@ class RedisConnection
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def new_redis
|
23
|
-
::Redis.connect(
|
23
|
+
::Redis.connect(@args)
|
24
24
|
end
|
25
25
|
end
|
data/lib/redis_queue.rb
CHANGED
@@ -1,23 +1,24 @@
|
|
1
1
|
require_relative 'redis_connection'
|
2
2
|
|
3
3
|
class RedisQueue
|
4
|
-
def initialize id
|
5
|
-
@id
|
6
|
-
@
|
4
|
+
def initialize args={id: :messages, url: 'redis://localhost:6379/0'}
|
5
|
+
@id = args.delete(:id)
|
6
|
+
@redis = RedisConnection.new(args)
|
7
|
+
@redis_blocking = RedisConnection.new(args)
|
7
8
|
end
|
8
9
|
|
9
10
|
def pop
|
10
|
-
@
|
11
|
+
@redis_blocking.run do |redis|
|
11
12
|
redis.blpop(@id).last.tap { |msg| redis.sadd "#{@id}_in_use", msg }
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
16
|
def push task
|
16
|
-
@
|
17
|
+
@redis.run { |redis| redis.rpush @id, task }
|
17
18
|
end
|
18
19
|
|
19
20
|
def fail task
|
20
|
-
@
|
21
|
+
@redis.run do |redis|
|
21
22
|
redis.pipelined do
|
22
23
|
redis.sadd "#{@id}_failed", task
|
23
24
|
redis.srem "#{@id}_in_use", task
|
@@ -26,7 +27,7 @@ class RedisQueue
|
|
26
27
|
end
|
27
28
|
|
28
29
|
def done task
|
29
|
-
@
|
30
|
+
@redis.run do |redis|
|
30
31
|
redis.pipelined do
|
31
32
|
redis.sadd "#{@id}_done", task
|
32
33
|
redis.srem "#{@id}_in_use", task
|
@@ -35,7 +36,7 @@ class RedisQueue
|
|
35
36
|
end
|
36
37
|
|
37
38
|
def unpop task
|
38
|
-
@
|
39
|
+
@redis.run do |redis|
|
39
40
|
redis.pipelined do
|
40
41
|
redis.lpush @id, task
|
41
42
|
redis.srem "#{@id}_in_use", task
|
@@ -45,16 +46,16 @@ class RedisQueue
|
|
45
46
|
|
46
47
|
def reset
|
47
48
|
init_from "#{@id}_in_use"
|
48
|
-
@
|
49
|
+
@redis.run { |redis| redis.del "#{@id}_in_use" }
|
49
50
|
end
|
50
51
|
|
51
52
|
def restart
|
52
53
|
init_from "#{@id}_done"
|
53
|
-
@
|
54
|
+
@redis.run { |redis| redis.del "#{@id}_done" }
|
54
55
|
end
|
55
56
|
|
56
57
|
def init_from set
|
57
|
-
@
|
58
|
+
@redis.run do |redis|
|
58
59
|
redis.eval "local vals = redis.call('smembers', '#{set}')
|
59
60
|
for i = 1, table.getn(vals) do
|
60
61
|
redis.call('rpush', '#{@id}', vals[i])
|
@@ -63,35 +64,35 @@ class RedisQueue
|
|
63
64
|
end
|
64
65
|
|
65
66
|
def size
|
66
|
-
@
|
67
|
+
@redis.run { |redis| redis.llen @id }.to_i
|
67
68
|
end
|
68
69
|
|
69
70
|
def done_size
|
70
|
-
@
|
71
|
+
@redis.run { |redis| redis.scard "#{@id}_done" }.to_i
|
71
72
|
end
|
72
73
|
|
73
74
|
def failed_size
|
74
|
-
@
|
75
|
+
@redis.run { |redis| redis.scard "#{@id}_failed" }.to_i
|
75
76
|
end
|
76
77
|
|
77
78
|
def in_use_size
|
78
|
-
@
|
79
|
+
@redis.run { |redis| redis.scard "#{@id}_in_use" }.to_i
|
79
80
|
end
|
80
81
|
|
81
82
|
def list
|
82
|
-
@
|
83
|
+
@redis.run { |redis| redis.lrange @id, 0, -1 }
|
83
84
|
end
|
84
85
|
|
85
86
|
def done_list
|
86
|
-
@
|
87
|
+
@redis.run { |redis| redis.smembers "#{@id}_done" }
|
87
88
|
end
|
88
89
|
|
89
90
|
def failed_list
|
90
|
-
@
|
91
|
+
@redis.run { |redis| redis.smembers "#{@id}_failed" }
|
91
92
|
end
|
92
93
|
|
93
94
|
def in_use_list
|
94
|
-
@
|
95
|
+
@redis.run { |redis| redis.smembers "#{@id}_in_use" }
|
95
96
|
end
|
96
97
|
|
97
98
|
def print_stats
|
@@ -109,7 +110,7 @@ class RedisQueue
|
|
109
110
|
end
|
110
111
|
|
111
112
|
def clear
|
112
|
-
@
|
113
|
+
@redis.run do |redis|
|
113
114
|
redis.del @id
|
114
115
|
redis.del "#{@id}_in_use"
|
115
116
|
redis.del "#{@id}_done"
|