PHP: Running Scheduled Jobs (Cron Jobs)

Better way to schedule cron jobs based on job orders from php script

While it is possible to use the database as a queue, it is commonly known as an anti-pattern (next to using the database for logging), and as you are looking for:

So any way / package / system to accomplish this behavior?

I use the free-form of your question thanks to the placed bounty to suggest: Beanstalk.

Beanstalk is a simple, fast work queue.

Its interface is generic, but was originally designed for reducing the latency of page views in high-volume web applications by running time-consuming tasks asynchronously.

It has client libraries in the languages you mention in your question (and many more), is easy to develop with and to run in production.

Run a cron job that performs other cron jobs schedules in MYSQL

If you're ok with the minimum cron-job interval, which is one minute, then your approach is good to go. I assume you're storing the on-demand jobs within one of your database's table, in which case you need to also consider the following:

  1. you need to mark the on-demand jobs as in progress to avoid the scenarios where an on-demand job takes more than a minute and is also picked up by the next job
  2. you might also consider retrying failed jobs, or jobs for which the processing script failed (either crashed or was killed by system for whatever reason)

If the on-demand actions need to be taken care of as soon as possible, and you also want to take care of the above two items, then you might consider a job scheduling approach, like using RabbitMQ. This approach has a bit more steep learning curve then the cron-based one, however the great advantage is that once you schedule the job you don't need to care much about it, except telling the job scheduler how to run it.

CronJob not running

Finally I found the solution. Following is the solution:-

  1. Never use relative path in python scripts to be executed via crontab.
    I did something like this instead:-

    import os
    import sys
    import time, datetime

    CLASS_PATH = '/srv/www/live/mainapp/classes'
    SETTINGS_PATH = '/srv/www/live/foodtrade'
    sys.path.insert(0, CLASS_PATH)
    sys.path.insert(1,SETTINGS_PATH)

    import other_py_files
  2. Never supress the crontab code instead use mailserver and check the mail for the user. That gives clearer insights of what is going.



Related Topics



Leave a reply



Submit