Monday, July 18, 2011

How to set batch / cron jobs in symfony.

I cam across an instance, where i needed to set up the batch job for certain API which were getting information from 3rd party vendors on certain time intervals.
So I started to look for doing same with symfony 1.4 and doctrine.
Below are some of my findings and better ways to create the batch / cron jobs.

I will walk you through the steps in order to get it working properly.

Batch Instead of Web

Symfony has already set up to make it easy to create small controllers that will be run from the command line.

The “/batch” directory is where you will be putting your special controller file, instead of the “/web” directory.  This batch file is looks pretty much like the front controllers that go in the “web” directory. The only thing you will be leaving out is the dispatch method which starts running the controller and action. We are going to be replacing it with a line that still fires up the sfContext instance so that we have access to our database, etc.

Batch File:

  1. <?php
  2. // this file is saved as SF_PROJECT/batch/cron_example.php
  3. define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/..'));
  4. define('SF_APP', 'frontend');
  5. define('SF_ENVIRONMENT', 'prod');
  6. define('SF_DEBUG', true);
  7. require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
  8. // get the application instance
  9. sfContext::getInstance();
  10. // put whatever code you need here…
  11. echo "\r\n running cron job….\r\n";
    Or  we can also set configuration from Project configuration file as below:

    require_once(dirname(__FILE__) . '/../config/ProjectConfiguration.class.php');
    $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true);
    sfContext::createInstance($configuration);
 it’s similar to a standard front controller file. Now all you require is add code / function your cron job requires do.

Run through command prompt:
  1. go to your PROJECT_DEIRECTORY
  2. php batch/cron_example.php
  3. running cron job…
If you get the expected output, then everything is good to go.

The Crontab File

set up the cron to run every minute and output to null.
  1. * * * * * * php /home/me/my_project/batch/cron_example.php > /dev/null 2>&1
Hope it helps you. comment in case any issue/concern.

4 comments:

  1. Thanks for taking the time to discuss that, I really feel strongly about it and love learning more on that topic. If achievable, as you gain competence, would you mind updating your blog with more information? It is highly helpful for me. WOW gold

    ReplyDelete
  2. The information and the aspect were just wonderful. I think that your viewpoint is deep, it’s just well thought out and truly incredible to see someone who knows how to put these thoughts so well.
    mdf wood signs

    ReplyDelete
  3. I wholeheartedly agree with your review
    mdf wood signs

    ReplyDelete
  4. This is a great post ! it was very informative. I look forward in reading more of your work. Also, I made sure to bookmark your website so I can come back later. I enjoyed every moment of reading it. buy gold

    ReplyDelete