Skip to content

Instantly share code, notes, and snippets.

@tpasipanodya
Created Aug 29, 2019
Embed
What would you like to do?
The Lua Script for Pushing Job Metadata Into Redis
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