-
-
Save tpasipanodya/d9af76ed7a26c3c5df35c85041bf645a to your computer and use it in GitHub Desktop.
The Lua Script for Pushing Job Metadata Into Redis
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] | |
local tenant_id = ARGV[2] | |
local queue = ARGV[3] | |
-- Metadata used to dynamically lookup all other structures given a job id | |
local metadata = 'jobs:' .. job_id | |
redis.call('HMSET', metadata, | |
'job_id', job_id | |
'tenant_id', organization_id, | |
'queue', queue | |
) | |
-- An ordered list that keeps track of a tenant's pending jobs | |
local pending_jobs = queue .. '_pending_jobs:' .. tenant_id | |
redis.call('LPUSH', pending_jobs, job_id) | |
-- Check if this tenant is runnable. | |
-- Assumes the tenant_limits hash specifies limits for different queues, | |
-- e.g { queue_1 => 5, queue_2 => 20, ..} | |
local limit = tonumber(redis.call('HGET', 'tenant_limits', queue)) | |
-- A set that keeps track of a tenants currently running jobs | |
local running_jobs = queue .. '_running_jobs:' .. tenant_id | |
local runing_jobs_count = redis.call('SCARD', running_jobs) | |
if (runing_jobs_count < limit) then | |
-- A set that keeps track of tenants who have pending jobs but | |
-- havn't exceeded our set limit. | |
local runnable_tenants = queue .. '_runnable_tenants' | |
redis.call('SADD', runnable_tenants, tenant_id) | |
end | |
return redis.status_reply('OK') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment