Symfony 1.2 - tasks and cron

I was wondering if there is any simple way in Symfony for calling cron job. I found in the documentation that we can use Batch files - it is kind of PHP script that looks similar to symfony front controller, all we need to do is to initalize symfony, parse project and application Configuration. Quite cool but there is better way (just in my opinion) to access all symfony features from the command line: tasks.
Every time we are creating project, generating modules, building models/forms we use standard symfony tasks. Task are grouped by namespace and name - this allows us to build intuitive script names. For example namespace: propel or doctrine, and the task name: build-model, build-sql. Why we should use tasks instead of batch files? Tasks are really simple, powerful and they take care of parsing command line arguments and options, initalizing needed configuration and classes. To create a simple task we have to create a class that exetend sfBaseTask and put it into lib/task/ directory. File name must ends with “Task.class.php” to be properly loaded.
Another great thing about tasks is that Symfony also provide task for generating new, custom tasks :)

symfony generate:task namespace:task

Using symfony generate:task task we can also use some options:
–brief-description - w can put some description for our task that we will after calling help
–use-database - allow us to use our database classes in task. By default set to propel
–dir - task directory, default lib/task/

For more information call:

symfony help generate:task

After generating we need to custimize our task. We can use our standard Propel/Dcotrine class to get/update data from the database. Simple example:


<?php

class updateUsersTask extends sfBaseTask
{
  protected function configure()
  {
    $this->namespace = 'user';
    $this->name = 'update-statuses';

     $this->briefDescription = 'Updates users status';
     $this->detailDescription = 'Updates users status';
  }

  protected function execute($arguments = array(), $options = array())
  {
    $databaseManager = new sfDatabaseManager($this->configuration);

	$this->log('Updating users statuses...');

	$this->log( sprintf('%d users were successfully updated', UserPeer::updateUsersStatuses( ) ));
  }
}

?>

Body of the function updateUsersStatuses is not important now.
Now we can simple call in commad line prompt:

symfony user:update-statuses

We should get nice message about how many users were updated.
As we have access to the script from the command line we should not have any problems with putting it into crontable to update users statuses (in this case) periodically. For example

* * * * * php /path/to/project/symfony user:update-statuses

where:

* * * * * php script arguments
- - - - -
| | | | |
| | | | +—– day of week (0 - 6) (Sunday=0)
| | | |
| | | +——- month (1 - 12)
| | |
| | +——— day of month (1 - 31)
| |
| +———– hour (0 - 23)
|
+————- minute (0 - 59)

4 Responses to “Symfony 1.2 - tasks and cron”


  1. 1 Jamie

    Perfect! This is EXACTLY what I needed to know! :D :D :D Thanks!

  2. 2 ali nabeel

    Great Bro you solve my problem
    Thanks.

  3. 3 Jack

    Perfect Example. It helps a lot. :)

  4. 4 Denis

    Join to opinions, perfect

  1. 1 Symfony i Zend Framework | Projektant - Programista PHP

Leave a Reply

PodglÄ…d komentarza:




About me:

  • PHP programmer
  • Symfony developer
  • Zend framework developer

Categories: