Question

I'm in need of a way to create dynamic, one-off cron jobs to execute tasks at different times. Ideally, I would like to achieve this using PHP, so that when a user completes a certain action, the cron job is created and scheduled for a time that is calculated based on the time that the user completes said action. At any one time, there could be multiple cron jobs scheduled at once for different times. These cron jobs also need to be deleted upon completion.

I have tried searching around for something appropriate, however haven't encountered anything that works as I need. If anybody could point me in the right direction, that would be greatly appreciated.

Cheers

Was it helpful?

Solution

A possible solution:

  1. Setting a Cron job that calls to CronJobManager.php every second.

    Just make a regular daemon that calls for the CronJobManager.php.

  2. Create a cronjob table in your database

    The cron job table should contain these basic fields: path (to php file), run_time(datetime), last run (datetime) and type (like suicidal, if as you explain you want some cron jobs to delete themselves)

  3. Connect CronJobManager.php with the cronjob table

    Every time CronJobManager.php runs (that is, every second), loads the cron jobs. Then, comparing "now"'s time with each cron job's run_time you'll get which cron jobs to run.

    For example, if cron job "foo" run_time is set to 18/04/2014 22:02:01, CronJobManager will run it when reaching that moment.

    Notice that if Cron jobs executing time needs a lot of time, they'll get delayed and eventually a second or two will get lost.

    Now, for every cron job that needs an execution, you would execute the related php file of that cron job, indicated in the path.

This is a general idea, and of course you would have to extend it with for example cron job states (idle, running, stop, etc).

In order to delete cron jobs you would implement this feature in the cron job object.

That is: the Cron Job class, once it has executed what it had to do, it would check its type (as defined in database). If it is 'suicidal', then it would delete the database row.

UPDATE

I updated the answer but I want to note something. If what you need is several cron jobs to run at once, in a specific second with 0 delay, then you need a cron job per task out of php that runs a specific file.

OTHER TIPS

To achieve this functionality, you need to have a daemon that is running all the time that checks for these dynamic jobs and launches them. The setup is a tad complicated to put together, but if you are ready for such an endeavor, you can use the project PHP Resque Scheduler.

https://github.com/chrisboulton/php-resque-scheduler

You start up a daemon that runs all the time and then you can add jobs to a dynamic queue to be executed at any specified time in the future. I think you will find this suitable to everything you are looking to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top