redis_queue 0.4 → 0.5

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/redis_queue.rb +35 -25
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 070b9946924ad3d989f088bbf2d4e246bcdeb46c
4
- data.tar.gz: e271e4dc1e7ee9423b60d7f76309833d6cf3144d
3
+ metadata.gz: 83d7bc9d87624d6c6e049a3fe88daa963895d85f
4
+ data.tar.gz: 696a10c9b9f5c0d822b85e0521ead406a56c49e8
5
5
  SHA512:
6
- metadata.gz: 5265930448447d46b649203c00020e7ed21136c18e36ac6351f58fadc8b350ba03e8b5a1eeee92f53bb19f21cc669ecd30a704895a90b610df796a7396efdb08
7
- data.tar.gz: d4821d42bcbc251170f4071f48ccbc6324576ce5fb16d592d27f792e7ac53ce3a03a28d6cee63fb5b629c9b4eda66b2317093ff8d4930635d0969a524b019c02
6
+ metadata.gz: 66beafeceb0ee5f8436cf9c9e2a6f978bdbde4387ad2e381db6773b1fb1dccbedd1ccbaaefa9607385025d40ce00b5ddd5f3a132677aec7ae2f687a9a055e9c1
7
+ data.tar.gz: c31e7772d52044493d111fbfdea9d1a234c52b12b1930d4487f3b953fa72e1aac59161e98aa7ee33d54ba1d70e3131aebd5baa321d1b461bc80c276c2df04c2f
data/lib/redis_queue.rb CHANGED
@@ -1,17 +1,22 @@
1
1
  require_relative 'redis_connection'
2
2
 
3
3
  class RedisQueue
4
- SCRIPTS = {
5
- push: """
6
- if ARGV[3] == 'true' then
7
- local insert = redis.call('linsert', ARGV[1], 'before', '', ARGV[2])
8
- if insert == -1 or insert == 0 then
9
- redis.call('lpush', ARGV[1], '')
10
- redis.call('lpush', ARGV[1], ARGV[2])
11
- end
12
- else
13
- redis.call('rpush', ARGV[1], ARGV[2])
4
+ PUSH_CODE = """
5
+ if ARGV[3] == 'true' then
6
+ local insert = redis.call('linsert', ARGV[1], 'before', '', ARGV[2])
7
+ if insert == -1 or insert == 0 then
8
+ redis.call('lpush', ARGV[1], '')
9
+ redis.call('lpush', ARGV[1], ARGV[2])
14
10
  end
11
+ else
12
+ redis.call('rpush', ARGV[1], ARGV[2])
13
+ end"""
14
+
15
+ SCRIPTS = {
16
+ push: PUSH_CODE,
17
+ repush: """
18
+ #{PUSH_CODE}
19
+ redis.call('srem', ARGV[1]..'_in_use', ARGV[2])
15
20
  """,
16
21
  fail: """
17
22
  redis.call('sadd', ARGV[1]..'_failed', ARGV[2])
@@ -32,7 +37,8 @@ class RedisQueue
32
37
  end"""
33
38
  }
34
39
 
35
- def initialize args={id: :messages, url: 'redis://localhost:6379/0'}
40
+ def initialize args={}
41
+ args = {id: :messages, url: 'redis://localhost:6379/0'}.merge(args)
36
42
  @id = args.delete(:id)
37
43
  @redis = RedisConnection.new(args)
38
44
  @redis_blocking = RedisConnection.new(args)
@@ -41,30 +47,34 @@ class RedisQueue
41
47
 
42
48
  def pop
43
49
  begin
44
- task = @redis_blocking.run { |redis| redis.blpop(@id) }.last
45
- end while task == ''
46
- @redis.run { |redis| redis.sadd "#{@id}_in_use", task }
47
- task
50
+ message = @redis_blocking.run { |redis| redis.blpop(@id) }.last
51
+ end while message == ''
52
+ @redis.run { |redis| redis.sadd "#{@id}_in_use", message }
53
+ message
54
+ end
55
+
56
+ def push message, priority=false
57
+ script :push, @id, message, priority
48
58
  end
49
59
 
50
- def push task, priority=false
51
- script :push, @id, task, priority
60
+ def fail message
61
+ script :fail, @id, message
52
62
  end
53
63
 
54
- def fail task
55
- script :fail, @id, task
64
+ def done message
65
+ script :done, @id, message
56
66
  end
57
67
 
58
- def done task
59
- script :done, @id, task
68
+ def unpop message
69
+ script :unpop, @id, message
60
70
  end
61
71
 
62
- def unpop task
63
- script :unpop, @id, task
72
+ def repush message, priority=false
73
+ script :repush, @id, message, priority
64
74
  end
65
75
 
66
- def forget task
67
- @redis.run { |redis| redis.srem "#{@id}_in_use", task }
76
+ def forget message
77
+ @redis.run { |redis| redis.srem "#{@id}_in_use", message }
68
78
  end
69
79
 
70
80
  def reset
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jose Ignacio Fernandez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-18 00:00:00.000000000 Z
11
+ date: 2014-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis