Class: WorkerRoulette::Foreman

Inherits:
Object
  • Object
show all
Defined in:
lib/worker_roulette/foreman.rb

Constant Summary collapse

LUA_ENQUEUE_WORK_ORDERS =
"  local counter_key       = KEYS[1]\n  local job_board_key     = KEYS[2]\n  local sender_key        = KEYS[3]\n  local channel           = KEYS[4]\n\n  local work_order        = ARGV[1]\n  local job_notification  = ARGV[2]\n  local redis_call        = redis.call\n  local zscore            = 'ZSCORE'\n  local incr              = 'INCR'\n  local zadd              = 'ZADD'\n  local rpush             = 'RPUSH'\n  local publish           = 'PUBLISH'\n  local zcard             = 'ZCARD'\n  local del               = 'DEL'\n\n  local function enqueue_work_orders(work_order, job_notification)\n    redis_call(rpush, sender_key, work_order)\n\n    -- called when a work from a new sender is added\n    if (redis_call(zscore, job_board_key, sender_key) == false) then\n      local count     = redis_call(incr, counter_key)\n      redis_call(zadd, job_board_key, count, sender_key)\n    end\n  end\n\n  enqueue_work_orders(work_order, job_notification)\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis_pool, sender, namespace = nil) ⇒ Foreman

Returns a new instance of Foreman.



35
36
37
38
39
40
41
# File 'lib/worker_roulette/foreman.rb', line 35

def initialize(redis_pool, sender, namespace = nil)
  @sender     = sender
  @namespace  = namespace
  @redis_pool = redis_pool
  @channel    = namespace || WorkerRoulette::JOB_NOTIFICATIONS
  @lua        = Lua.new(@redis_pool)
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



3
4
5
# File 'lib/worker_roulette/foreman.rb', line 3

def channel
  @channel
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



3
4
5
# File 'lib/worker_roulette/foreman.rb', line 3

def namespace
  @namespace
end

#senderObject (readonly)

Returns the value of attribute sender.



3
4
5
# File 'lib/worker_roulette/foreman.rb', line 3

def sender
  @sender
end

Instance Method Details

#counter_keyObject



57
58
59
# File 'lib/worker_roulette/foreman.rb', line 57

def counter_key
  @counter_key ||= WorkerRoulette.counter_key(@namespace)
end

#enqueue_work_order(work_order, headers = {}, &callback) ⇒ Object



43
44
45
46
# File 'lib/worker_roulette/foreman.rb', line 43

def enqueue_work_order(work_order, headers = {}, &callback)
  work_order = {'headers' => default_headers.merge(headers), 'payload' => work_order}
  enqueue_work_order_without_headers(work_order, &callback)
end

#enqueue_work_order_without_headers(work_order, &callback) ⇒ Object



48
49
50
51
# File 'lib/worker_roulette/foreman.rb', line 48

def enqueue_work_order_without_headers(work_order, &callback)
  @lua.call(LUA_ENQUEUE_WORK_ORDERS, [counter_key, job_board_key, sender_key, @channel],
            [WorkerRoulette.dump(work_order),  WorkerRoulette::JOB_NOTIFICATIONS], &callback)
end

#job_board_keyObject



53
54
55
# File 'lib/worker_roulette/foreman.rb', line 53

def job_board_key
  @job_board_key ||= WorkerRoulette.job_board_key(@namespace)
end

#sender_keyObject



61
62
63
# File 'lib/worker_roulette/foreman.rb', line 61

def sender_key
  @sender_key ||= WorkerRoulette.sender_key(@sender, @namespace)
end