symfonyでバッチ処理
symfonyで開発している場合普通のアクションをcronでバッチ処理しようとしてもうまくいかないので、taskとしてプログラムを作成する必要がある。
symfony generate:task 任意のネームスペース名:任意のバッチ名
このコマンドでsymfonyルート/lib/task/に「ネームスペース名バッチ名Task.class.php」が作成される。
例えば
symfony generate:task hoge:fuga-fuga ↓ symfonyルート/lib/task/hogeFugaFugaTask.class.php
ファイルの中身はこんな感じ
<?php class feedReadFeedTask extends sfBaseTask { protected function configure() { // // add your own arguments here // $this->addArguments(array( // new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'), // )); $this->addOptions(array( new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'), new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'), // add your own options here )); $this->namespace = 'ネームスペース名'; $this->name = 'バッチ名'; $this->briefDescription = ''; $this->detailedDescription = <<<EOF The [feed:readFeed|INFO] task does things. Call it with: [php symfony feed:readFeed|INFO] EOF; } protected function execute($arguments = array(), $options = array()) { // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $connection = $databaseManager->getDatabase($options['connection'])->getConnection(); // add your code here } }
「// add your code here」以降にコードを書く
後は
crontab -e
でcronの書式に従ってスクリプトを登録
0 * * * * /home/symfonyRoot/symfony hoge:huga-huga >/dev/null 2>&1
※毎時0分に実行
おわり