Let’s suppose we have a list of tasks to be executed and some workers that pop items from that list.
If a worker crashes unexpectedly before finishing the execution of the task then that task is lost.
What kind of mechanism could prevent that so we can reprocess abandoned tasks?
2
Answers
There is no
EXPIRE
for set or zset members and there is no atomic operation to pop from zset and push to list. So I ended writing this lua script which runs atomically.First I add a task to the
executing-tasks
zset with a timestamp score ((new Date()).valueOf()
in javascript):ZADD 1619028226766 executing-tasks
Then I run the script:
EVAL [THE SCRIPT] 2 executing-tasks tasks 1619028196766
If the task is more than 30 seconds old it will be sent to the
tasks
list. If not, it will be sent back to theexecuting-tasks
zset.Here is the script
You need to use ZSET to solve this issue
Pop operation
Ack Operation
Worker
You need to run a scheduled worker that will move items from ZSET to list if they are expired
Read it in detail, how I did in Rqueue https://medium.com/@sonus21/introducing-rqueue-redis-queue-d344f5c36e1b
Github Code: https://github.com/sonus21/rqueue