Skip to content

Instantly share code, notes, and snippets.

@tpasipanodya
Created August 30, 2019 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpasipanodya/ea1150205aba3f3ac3f9e23e2eb8f24c to your computer and use it in GitHub Desktop.
Save tpasipanodya/ea1150205aba3f3ac3f9e23e2eb8f24c to your computer and use it in GitHub Desktop.
The Lua Script to Reschedule a Job
redis.replicate_commands()
local job_id = ARGV[1]
-- source the jobs metadata for some accounting
metadata_key = 'jobs:' .. job_id
local job_metadata = redis.call('HMGET', metadata_key, 'tenant_id', 'queue')
local tenant_id = job_metadata[1]
local queue = job_metadata[2]
-- cleanup the job's running state
local running_jobs = queue .. '_running_jobs:' .. tenant_id
redis.call('SREM', running_jobs, job_id)
local pending_jobs = queue .. '_pending_jobs:' .. tenant_id
redis.call('LPUSH', pending_jobs, job_id)
-- if rescheduling this job made the tenant runnable, mark them as such
local limit = tonumber(redis.call('HGET', 'tenant_limits', queue))
local running_jobs_count = redis.call('SCARD', running_jobs)
if (running_jobs_count < limit) then
local runnable_tenants = queue .. '_runnable_tenants'
redis.call('SADD', runnable_tenants, tenant_id)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment