worker_roulette 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -45,23 +45,24 @@ module WorkerRoulette
45
45
 
46
46
  local work_order = ARGV[1]
47
47
  local job_notification = ARGV[2]
48
+ local redis_call = redis.call
49
+ local zscore = 'ZSCORE'
50
+ local incr = 'INCR'
51
+ local zadd = 'ZADD'
52
+ local rpush = 'RPUSH'
53
+ local publish = 'PUBLISH'
48
54
 
49
55
  local function enqueue_work_orders(work_order, job_notification)
50
- local result = sender_key .. ' updated'
51
- local sender_is_on_job_board = redis.call('ZSCORE', job_board_key, sender_key)
52
-
53
- if (sender_is_on_job_board == false) then
54
- local count = redis.call('INCR', counter_key)
55
- local job_added = redis.call('ZADD',job_board_key, count, sender_key)
56
- result = sender_key .. ' added'
56
+ if (redis_call(zscore, job_board_key, sender_key) == false) then
57
+ local count = redis_call(incr, counter_key)
58
+ redis_call(zadd, job_board_key, count, sender_key)
57
59
  end
58
60
 
59
- local work_added = redis.call('RPUSH',sender_key, work_order)
60
- local job_board_update = redis.call('PUBLISH', channel, job_notification)
61
- return result
61
+ redis_call(rpush,sender_key, work_order)
62
+ redis_call(publish, channel, job_notification)
62
63
  end
63
64
 
64
- return enqueue_work_orders(work_order, job_notification)
65
+ enqueue_work_orders(work_order, job_notification)
65
66
  HERE
66
67
  end
67
68
  end
@@ -38,47 +38,56 @@ module WorkerRoulette
38
38
  private
39
39
  def self.lua_drain_work_orders
40
40
  <<-HERE
41
+ local empty_string = ""
41
42
  local job_board_key = KEYS[1]
42
- local last_sender_key = KEYS[2]
43
- local sender_key = ARGV[1]
43
+ local last_sender_key = KEYS[2] or empty_string
44
+ local sender_key = ARGV[1] or empty_string
45
+ local redis_call = redis.call
46
+ local lock_key_prefix = "L*:"
47
+ local lock_value = "L"
48
+ local ex = "EX"
49
+ local nx = "NX"
50
+ local get = "GET"
51
+ local set = "SET"
52
+ local del = "DEL"
53
+ local zrank = "ZRANK"
54
+ local zrange = "ZRANGE"
44
55
 
45
56
  local function drain_work_orders(job_board_key, last_sender_key, sender_key)
46
- if last_sender_key ~= "" and last_sender_key ~= nil then
47
- local last_sender_lock_key = 'L*:' .. last_sender_key
48
- redis.call('DEL', last_sender_lock_key)
57
+ if last_sender_key ~= empty_string then
58
+ local last_sender_lock_key = lock_key_prefix .. last_sender_key
59
+ redis_call(del, last_sender_lock_key)
49
60
  end
50
61
 
51
- if (not sender_key) or (sender_key == "") then
52
- sender_key = redis.call('ZRANGE', job_board_key, 0, 0)[1]
53
- if (not sender_key) or (sender_key == "") then
62
+ if sender_key == empty_string then
63
+ sender_key = redis_call(zrange, job_board_key, 0, 0)[1] or empty_string
64
+ if sender_key == empty_string then
54
65
  return {}
55
66
  end
56
67
  end
57
68
 
58
- local lock_key = 'L*:' .. sender_key
59
- local locked = (redis.call('GET', lock_key) == 'L')
69
+ local lock_key = lock_key_prefix .. sender_key
70
+ local locked = (redis_call(get, lock_key) == lock_value)
60
71
 
61
72
  if not locked then
62
- local results = {}
63
- results[1] = sender_key
64
- results[2] = redis.call('LRANGE', sender_key, 0, -1)
65
- redis.call('DEL', sender_key)
66
- redis.call('ZREM', job_board_key, sender_key)
67
- redis.call('SET', lock_key, 'L', 'EX', 1, 'NX')
73
+ local results = {sender_key, redis_call('LRANGE', sender_key, 0, -1)}
74
+ redis_call(del, sender_key)
75
+ redis_call('ZREM', job_board_key, sender_key)
76
+ redis_call(set, lock_key, lock_value, ex, 1, nx)
68
77
  return results
69
78
  else
70
- local sender_index = redis.call('ZRANK', job_board_key, sender_key)
79
+ local sender_index = redis_call(zrank, job_board_key, sender_key)
71
80
  local next_index = sender_index + 1
72
- local next_sender_key = redis.call('ZRANGE', job_board_key, next_index, next_index)[1]
81
+ local next_sender_key = redis_call(zrange, job_board_key, next_index, next_index)[1]
73
82
  if next_sender_key then
74
- return drain_work_orders(job_board_key, "", next_sender_key)
83
+ return drain_work_orders(job_board_key, empty_string, next_sender_key)
75
84
  else
76
85
  return {}
77
86
  end
78
87
  end
79
88
  end
80
89
 
81
- return drain_work_orders(job_board_key, last_sender_key, "")
90
+ return drain_work_orders(job_board_key, last_sender_key, empty_string)
82
91
  HERE
83
92
  end
84
93
  end
@@ -1,3 +1,3 @@
1
1
  module WorkerRoulette
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -25,7 +25,6 @@ describe WorkerRoulette do
25
25
  foreman = WorkerRoulette.a_foreman('foreman')
26
26
  foreman.enqueue_work_order('some old fashion work') do |redis_response, stuff|
27
27
  called = true
28
- redis_response.should == 'foreman added'
29
28
  end
30
29
  done(0.1) {called.should == true}
31
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worker_roulette
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: