Thursday, 4 July 2013

How to create cron in symfony?


Hi All,

First of all create task using the below command:

 
php symfony generate:task [your task] 



for example:

php symfony generate:task notificationFailed

After executing above command you will see that following file has been created.

Creating "/var/www/archive/ipay4me_zen/lib/task /notificationFailedTask.class.php" task file


Now,

change the following variable in above created file:

$this->namespace        = 'project';
$this->name             = '[name-for-your-task]';
$this->briefDescription = '[some short explanation of what your task does]';


You will see another method called execute() given below:

protected function execute($arguments = array(), $options = array())
{
 // initialize the database connection
 $databaseManager = new sfDatabaseManager($this->configuration);
 $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();

 // add your code here

 echo "I just did nothing at all, but at least I did it successfully!\n\n";
}


Let’s give it a test run. Open your command line and enter

php symfony project:[your task]


if everything is fine then you will see "I just did nothing at all, but at least I did it successfully!" otherwise correct error and run again.

If you see the expected output you’re then ready to go to the next step.


Note: if you want to access your app.yml variable here then you have to write following line:

$this->createConfiguration('frontend', 'prod')

or

$this->createConfiguration('frontend', 'dev')

Cron Setup:

* * * * * cd [YOUR SF APP DIR] && /usr/bin/symfony project:[YOUR TASK] >>[YOUR SF APP DIR]/log/crontab.log
e.g,

* * * * * cd /var/www/archive/ipay4me_new && /usr/bin/php symfony project:notificationFailed >>/var/www/archive/ipay4me_new/log/crontab.log


Cheers!!!

No comments:

Post a Comment