Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Cronjobs (big ones) best practice
  • Hi, I have some really big cronjobs (by execution time)
    Each job has it's own controller/task and they're put together in linux commandline cron. Cron calls a shell script which calls the controller & task, for instance wget --max-redirect=999 http://www.mysite.tld/controller/task
    I now want to migrate to cronjobs defined in a mysql table and 1 poller (http://mysite.tld/cron) which invokes all tasks at their defined times and dates.
    The first part is ready, scripts are called ok, but what will happen with the scripts running recursively or taking much time. Is this the right way..?


  • Do you have cron access on the target machine?

    If so, you should use oil tasks, instead of web triggers (which is sooooo Wordpress, not very secure, and a big opportunity for DDOS attacks), and schedule the tasks locally.

    If you want to use pollers, you should use a queue mechanism, with a next execution timestamp on the queue entry.

    Your poller locks the queue, runs a query for all tasks for which the timestamp has expired, marks them as "in execution", unlocks the queue, fires the tasks found, and when a task is finished, locks the queue, remove the task from the queue (or mark them "finished" if you need history), and unlocks the queue.

    I would make the task responsible for re-scheduling itself. This way you can never have multiple entries for the same task in the queue.

    It does mean that if your task crashes, you will never have a new entry in the queue, so you need proper monitoring of your cron tasks. You could work around that by having your poller create a new queue entry when the task finishes, but that would require that the poller knows the execution interval.


  • Hi Harro,

    What I didn't tell you is that the web trigger are not accessable by the public, only by localhost. Does that make a difference? 
  • well, if you have access to cron on localhost (to add the web trigger), I would definately use oil tasks instead of plain controllers. They run using php-cli, and don't use your precious webserver resources.

    You don't want to know how many Wordpress servers I've seen crashing because of resource depletion, caused by calling batch operations through the frontend...
  • I did some tests with the oil method and the results seems promising. However I have some MVC related questions. My cron calls a cron controller which calls several tasks and subtasks.
    For instance /app/tasks/email.php consists of several functions, for instance sendEmail()
    sendEmail uses twigs located in /views/messages/*.twig and a emailmanager located in /classes/controller
    Is this ok, or should twigs and/or emailmanager be located in a package?
  • HarroHarro
    Accepted Answer
    How you organize your application is entirely your choice.

    In general, you would use modules or packages when you expect re-use of your code in multiple applications, or your application is so big that you need segmentation.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion