Skip to content

Instantly share code, notes, and snippets.

@yanatan16
Last active April 19, 2017 21:03
Show Gist options
  • Save yanatan16/6688052 to your computer and use it in GitHub Desktop.
Save yanatan16/6688052 to your computer and use it in GitHub Desktop.
recurring tasks in relyq/qb lua scripts
-- KEYS:[zset], ARGV:[now]
-- the sorted set contains key / next pairs, where next is the next execution time
-- Find all upcoming tasks
-- Get their intervals and requeue them
local refs = redis.call("zrangebyscore", KEYS[1], 0, ARGV[1])
for i,ref in pairs(refs) do
tref, int = string.match(ref, "([^|]*)|(\d+)")
redis.call("zincrby", KEYS[1], int, tref)
refs[i] = tref
end
return refs
-- KEYS:[zset], ARGV:[ref, now, interval]
-- the sorted set contains key / next pairs, where next is the next execution time
local ref, now, interval = unpack(ARGV)
redis.call("zadd", KEYS[1], now + interval, ref .. "|" .. interval)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment