Delayed execution / scheduling with Redis?

天大地大妈咪最大 提交于 2019-11-27 15:21:09

问题


Any tricks to do delayed task execution (i.e. scheduling) based on Redis?

Perhaps some clever way to delay BLPOP for a given number of seconds?..


回答1:


You can work with a ring of multiple LISTs that have a time component in their name. As time component you can take the current second (0-59).

You always add tasks to the list for the current second. To get the jobs you do a BLPOP (with low timeout) only on those lists where it is guaranteed, that the content is older than the given number of seconds.

If you work from multiple hosts you have to take care, that the clocks are in sync (NTP).




回答2:


If you want to do scheduling with redis, i would suggest using sorted set (the z*) commands:

http://code.google.com/p/redis/wiki/SortedSets

what you can do is something like this:

ZADD jobs <unix timestamp of when you want the job to run> <job identifier>

e.g:

ZADD jobs 1291348355

Then, every so often (up to every second) you can pull scheduled jobs that should run (or should have run by now):

ZRANGEBYSCORE jobs -inf, <current unix timestamp>

Boom, you got your jobs to run. Of course, make sure to delete done jobs from the sorted set.




回答3:


While the @efalcao's answer is a very good one, your question might indicate that redis does not perfectly suit your application needs. if your application has the nature of a message box, please consider using rabbitMQ, which features delayed messages or akka if you feel bold



来源:https://stackoverflow.com/questions/4138951/delayed-execution-scheduling-with-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!