alpixel / cronbundle
Symfony应用简单的Cronjob管理
2.0.1
2017-04-07 08:07 UTC
Requires
- php: >=5.4
- doctrine/doctrine-bundle: ~1.4
- doctrine/orm: ^2.4.8
- symfony/console: >=2.7
- symfony/symfony: >=2.7
This package is not auto-updated.
Last update: 2024-09-14 18:09:48 UTC
README
AlpixelCronBundle 是 predakanga/CronBundle 的分支,该分支已经不再维护。它提供了一个能够保存Cronjob并在指定间隔运行的Symfony Bundle。
安装
- 安装包
composer require 'alpixel/cronbundle:^2.0'
- 更新 AppKernel.php
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Alpixel\Bundle\CronBundle\CronBundle(),
);
// ...
}
// ...
}
- 如果你使用 doctrine 迁移,创建新的迁移
php app/console doctrine:migrations:diff
php app/console doctrine:migrations:migrate
- 或更新 DB 模式
php app/console doctrine:schema:update
- 开始使用bundle
//analyze all the cron task available and register them
php app/console cron:scan
//Run the cron analyzer
php app/console cron:run
- Cron设置
为了运行symfony cron:run任务,你需要在服务器上设置一个真实的cronjob,如下所示。此示例每5分钟检查一次cron脚本。
*/5 * * * * /usr/bin/php /path/to/symfony/install/app/console cron:run --env="prod"
创建新任务
使用CronBundle创建自己的任务非常简单 - 你只需创建一个普通的Symfony2 Command(或ContainerAwareCommand)并使用@CronJob注解标记,如下所示
use Alpixel\Bundle\CronBundle\Annotation\CronJob; /** * @CronJob(value="P1D", startTime="today 12:00") */ class DemoCommand extends Command { public function configure() { // Must have a name configured // ... } public function execute(InputInterface $input, OutputInterface $output) { // Your code here } }
间隔规范(如上面的示例中的"PT1H")在DateInterval文档页面上有记录,并可修改。为了使CronJob被扫描并包含在未来的运行中,你必须首先运行app/console cron:scan - 它将在你下一次运行app/console cron:run时安排运行
你也可以配置startTime来安排cronjob的第一次运行。
限制
请注意,你的cron将由PHP CLI运行,因此你的Symfony将没有有关你网站真实URL的任何信息(除非你在某处指定了它)。所以,例如,如果你使用cron发送电子邮件,你不能依赖twig模板中的"url()",因为Symfony不知道你的域名。