Para realizar una cola simple en redis que se puede usar para volver a enviar trabajos bloqueados, probaría algo como esto:
- 1 lista "disponible para agarrar"
- 1 lista "siendo_trabajado_en"
- bloqueos con caducidad automática
un trabajador que intenta conseguir un trabajo haría algo como esto:
timeout = 3600
#wrap this in a transaction so our cleanup wont kill the task
#Move the job away from the queue so nobody else tries to claim it
job = RPOPLPUSH(up_for_grabs, being_worked_on)
#Set a lock and expire it, the value tells us when that job will time out. This can be arbitrary though
SETEX('lock:' + job, Time.now + timeout, timeout)
#our application logic
do_work(job)
#Remove the finished item from the queue.
LREM being_worked_on -1 job
#Delete the item's lock. If it crashes here, the expire will take care of it
DEL('lock:' + job)
Y de vez en cuando, podríamos tomar nuestra lista y verificar que todos los trabajos que están allí realmente tengan un bloqueo. Si encontramos algún trabajo que NO tenga un bloqueo, esto significa que expiró y nuestro trabajador probablemente se bloqueó. este caso lo volveríamos a enviar.
Este sería el pseudocódigo para eso:
loop do
items = LRANGE(being_worked_on, 0, -1)
items.each do |job|
if !(EXISTS("lock:" + job))
puts "We found a job that didn't have a lock, resubmitting"
LREM being_worked_on -1 job
LPUSH(up_for_grabs, job)
end
end
sleep 60
end