Skip to content

Instantly share code, notes, and snippets.

Created Aug 29, 2019
What would you like to do?
The Lua Script for Pushing Job Metadata Into Redis
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'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'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('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 ='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''SADD', runnable_tenants, tenant_id)
return redis.status_reply('OK')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment