-
-
Save tpasipanodya/6b181c5bfc90a323a46ea47ef247ccba to your computer and use it in GitHub Desktop.
The Lua Script to Remove A job
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
-- if rescheduling this job made the tenant runnable, mark them as such | |
local limit = tonumber(redis.call('HGET', 'tenant_limits', queue)) | |
local runing_jobs_count = redis.call('SCARD', running_jobs) | |
local pending_jobs = queue .. '_pending_jobs:' .. tenant_id | |
local pending_jobs_count = redis.call('LLEN', pending_jobs) | |
if (runing_jobs_count < limit and pending_jobs_count > 0) 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