Hard, cron-like tasks

44 views
Skip to first unread message

Paul Tiseo

unread,
Aug 26, 2015, 10:44:18 AM8/26/15
to actionHero.js
I'm assuming from reading docs that AH's task handling and scheduling is essentially one or more FIFO job queues, where the queues are popped continuously as long as there are available workers AND the next job is not scheduled in the future (i.e. "allowed to run")? Also, the queues are emptied completely by order in api.config.tasks.queues?

This seems to differ from the Ruby-based resque, in which recurring jobs are stated to be cron-like. Admittedly, I am not a rubyist, so I am not sure if that is incorrect.

If I need to have tasks run with specific time requirements, cron-like iow, node-resque doesn't seem to satisfy that need, as a long-running task may result in an every-minute task being run late, correct? How should I handle tasks with hard time requirements?

Evan Tahler

unread,
Aug 26, 2015, 11:09:53 AM8/26/15
to actionHero.js
You are correct that actionhero's task system is a FIFO queue.  You are also correct that node-resque (which actionhero uses) is also only a FIFO system, which ruby-resque is as well.  There is a plugin to ruby-resque, resque-scheduler which has 2 features: the ability to enqueue tasks in the future (which node-resque coppied over), and a cron-ish "scheduled job" system, which node-resque does not have. 

I would love to see a pull request that adds a "runAt" option which would be a Date object to signal cron-like scheduling.  This would be another choice which would be be incompatible with the existing "frequency" option.  There are a number of tools which exist to add cron-like features to node, like https://github.com/tejasmanohar/node-schedule, which are great.  The tricky part is working within the actionhero cluster to ensure that only one instance of actionhero is running the schedule, and that all nodes check periodically that at least one other node is running the schedule.  I would suggest accomplishing this a TTLd key in redis that runs in an initializer.  

The logic would be something like like:
  • node boots
  • checks for `actionhero:isScheduler` key in redis
    • If does not exist, sets key with short TTL and becomes scheduler
    • if does exist, set a `setInterval` to check every minute to ensure key exists
  • If node is the scheduler, it can read task attributes.runAt to build up the schedule
  • Use node-scheduler to enqueue normal actionhero tasks (api.tasks.enqueue) at the proper time

Evan Tahler

unread,
Sep 6, 2015, 2:37:05 PM9/6/15
to actionHero.js
A lot of people have been asking for this kind of thing lately....

Here you go! 

Evan Tahler

unread,
Sep 6, 2015, 2:38:11 PM9/6/15
to actionHero.js
Keep in mind that this doesn't implement the "run at" functionality we talked about above, but rather uses a more loose ad-hoc system you can customize to run tasks when you need. 
Reply all
Reply to author
Forward
0 new messages