skrip42 / cron-bundle
Symfony Cron-bundle
v1.2.3
2021-01-28 05:51 UTC
Requires
- php: ^7.1.3
- skrip42/advanced-repository: ^2.2
- symfony/config: ^4.0|^5.0
- symfony/dependency-injection: ^4.0|^5.0
Requires (Dev)
- doctrine/doctrine-bundle: ^1.5|^2.0
- symfony/console: ^4.4|^5.0
- symfony/yaml: ^4.4|^5.0
This package is auto-updated.
Last update: 2024-09-28 13:30:51 UTC
README
symfony的定时任务调度器,扩展了类似cron的语法。
安装
- 运行
composer require skrip42/cron-bundle
- 创建数据库
php ./bin/console make:migration
和php ./bin/console doctrine:migration:migrate
- 在您的crontab中添加
* * * * * ./bin/console cron:run
定时任务语法
任务模式为 {分钟}_{小时}_{日}_{星期}_{月}_{年}
运算符可以组合使用,例如:0_0_*/2-1_*_*_*
- 将在奇数日的午夜执行。
可用命令
cron:add
- 交互式添加cron任务cron:closest
- 显示最接近的任务列表cron:list
- 显示cron任务列表cron:list --all
- 显示所有活动状态cron任务列表cron:optimize
- 禁用过时的cron任务cron:run
- 运行实际任务cron:update $id
- 更新cron任务cron:toggle $id
- 切换cron任务活动状态
使用方法
....... //you namespace declaration use Skrip42\Bundle\CronBundle\Services\Cron; use Skrip42\Bundle\CronBundle\Component\Pattern; ....... //you class declaration protected $cron; //instance of Cron service public function __construct(Cron $cron) { $this->cron = $cron } ........ //in you method $this->cron->getActualOnCurrentTime(); // return array of actual Schedule entity $this->cron->optimize(); // disable outdated cron tasks $this->cron->closestList($count); // return $count closest task in format: [$id, $command, $c] $this->cron->getList($all); // return all schedule task? if $all = true includes disabled task $this->cron->addSchedule($patternString, $commandString); // create new schedule task $this->cron->toggleSchedule($id); // toggle schedule task activity $this->cron->updateSchedule($id, $patternString, $commandString); // update schedule task $pattern = new Pattern($patternString); // get pattern object $pattern->test(new DateTime('NOW')); // test pattern for match date $pattern->getClosest($count); //return $count closest date that match pattern $schedule = reset($this->cron->getList()); // schedule is standart doctrine entity $schedule->getId(); //return id $schedule->getCommand(); // return command string $schedule->setCommand($commandString); // set command string to schedule $schedule->getPattern(); // return pattern string $schedule->setPattern($patternString); // set pattern string to schedule $schedule->toggleActive(); // toggle schedule activity $this->container->get('doctrine')->getManager()->flush(); // to save change ........