How to write a batch process
Overview
Creating batch processes in symfony is very simple. Batch processes can be used to automate any function that might need to be repeated on a regular basis. Some examples of useful batch functions might include a tool to load the development database with test data, or a production tool to periodically expire accounts, or send batched emails at a scheduled time.
Batch skeleton
In symfony, a batch process refers to any script that is run outside of the normal web front controller. To create a batch process, you need to define the location, application and environment that the batch process is to use.
This is easily accomplished by creating a PHP file in the myproject/batch/ directory and starting it with the following:
<?php
define('SF_ROOT_DIR', realpath(dirname(__file__).'/..'));
define('SF_APP', 'myapp');
define('SF_ENVIRONMENT', 'prod');
define('SF_DEBUG', false);
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
// initialize database manager
$databaseManager = new sfDatabaseManager();
$databaseManager->initialize();
// batch process here
?>
This script does nothing, or close to nothing: It defines a path, an application and an environment to get to a configuration, and loads that configuration. But that's already a lot because it means that all of the code written in your batch process will take advantage of the auto-loading of classes, automatic connection to Propel objects, and the symfony root classes.
Note: If you examine symfony's front controllers (like web/index.php) you will find this code extremely familiar. That's because every web request requires access to the same objects and configuration as a batch process.
The value that you define for SF_APP could be the name of any of the applications that you have defined in your project.
The SF_ENVIRONMENT has to be initialized with the name of the environment you want to run the batch in. The default possible environments are prod, dev, and test.
The SF_DEBUG constant defines whether the configuration has to be parsed every time the batch is run (true), or if a cached version can be used to imrpove speed (false). In general, the debug mode is used only in development. You can read more about the debug mode in the debug chapter.
Example batch processes
Below are some examples of batch processes that you might create.
Loading Test Data
Loading test data in the development and/or test environments is something that happens repeatedly. A PHP batch can automate this process. First, define the data to be loaded in a YAML file with the following format:
TableName:
recordlabel:
column: value
column: value
Save one or more data files with this format in a myproject/data/fixtures/ directory. You can find more about how to create the data population files in the data files chapter.
The following script excerpt reads all YAML files in the fixtures/ directory and loads the data into the database defined in the app/config/databases.yml configuration file.
// batch process here
$data = new sfPropelData();
$data->loadData(sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'fixtures');
Processing
TODO
- Create additional examples
- Scheduling batches
|