Skip to content

Instantly share code, notes, and snippets.

@marcbachmann
Last active January 11, 2022 18:52
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 marcbachmann/8a143a7af4d26cf25cf6957af1168380 to your computer and use it in GitHub Desktop.
Save marcbachmann/8a143a7af4d26cf25cf6957af1168380 to your computer and use it in GitHub Desktop.
Redis Scheduled Jobs
-- zaddhset sortedsetkey score hsetkey fieldname fieldvalue [fieldname fieldvalue ...]
-- sortedsetkey = KEYS[1]
-- score = KEYS[2]
-- hsetkey = KEYS[3]
-- fields = ARGV
if #KEYS ~= 3 or #ARGV < 2 or #ARGV % 2 ~= 0 then
return {err="ERR wrong number of arguments for 'zaddhset' command"}
end
if redis.call('EXISTS', KEYS[3]) == 1 then return nil end
redis.call('ZADD', KEYS[1], KEYS[2], KEYS[3])
redis.call('HSET', KEYS[3], unpack(ARGV))
return 'OK'
-- zrangehgetallxadd sortedsetkey streamkey minscore maxscore
-- sortedsetkey = KEYS[1]
-- streamkey = KEYS[2]
-- minscore = ARGV[1]
-- maxscore = ARGV[2]
if #KEYS ~= 2 or #ARGV ~= 2 then
return {err="ERR wrong number of arguments for 'zrangehgetallxadd' command"}
end
local hashkeys = redis.call('ZRANGEBYSCORE', KEYS[1], ARGV[1], ARGV[2])
if not hashkeys or #hashkeys == 0 then return 0 end
for i = 1, #hashkeys do
local fields = redis.call('HGETALL', hashkeys[i])
if #fields ~= 0 then redis.call('XADD', KEYS[2], '*', unpack(fields)) end
end
return redis.call('ZREM', KEYS[1], unpack(hashkeys))
@marcbachmann
Copy link
Author

marcbachmann commented Jan 11, 2022

  1. Call zaddhset to queue a job, deduplication happens by hsetkey passed in.
  2. Call zrangehgetallxadd peridically to move the jobs into a redis stream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment