8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/rjob/scripts/check_leadership.rb', line 8
def lua_script
" local worker_name = ARGV[1]\n local time_now = ARGV[2]\n local prefix = ARGV[3]\n local heartbeat_timeout = tonumber(ARGV[4])\n local r = redis\n if r.call('setnx', prefix .. ':leaderworker', worker_name) == 1 then\n return worker_name\n else\n local leader = r.call('get', prefix .. ':leaderworker')\n local last_hb = tonumber(r.call('hget', prefix .. ':worker:' .. leader, 'heartbeat'))\n if last_hb == nil or time_now - last_hb > heartbeat_timeout then\n r.call('set', prefix .. ':leaderworker', worker_name)\n return worker_name\n end\n return leader\n end\n LUA\nend\n"
|