rikudou/cron-bundle
Composer 安装命令:
composer require rikudou/cron-bundle
包简介
Simple and effective cron implementation for Symfony 4+
README 文档
README
Via composer: composer require rikudou/cron-bundle.
The bundle should be enabled automatically if you use the Symfony Flex.
How to use
Every cron job must implement the Rikudou\CronBundle\Cron\CronJobInterface
(every class that implements the interface is automatically registered as cron job),
which defines two methods:
string getCronExpression()- the cron expressionexecute(InputInterface $input, OutputInterface $output, LoggerInterface $logger)- the method that will be executed, you can write to console output, use console input and use logger
Example:
<?php namespace App\CronJobs; use Psr\Log\LoggerInterface; use Rikudou\CronBundle\Cron\CronJobInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MyTestCronJob implements CronJobInterface { /** * The cron expression for triggering this cron job * @return string */ public function getCronExpression(): string { return "*/5 * * * *"; } /** * This method will be executed when cron job runs * * @param InputInterface $input * @param OutputInterface $output * @param LoggerInterface $logger * @return mixed */ public function execute(InputInterface $input, OutputInterface $output, LoggerInterface $logger) { $logger->debug("The cron job was executed!"); } }
The cron jobs are executed using the console command cron:run, this command
should be executed every minute.
Example for crontab:
* * * * * php /path/to/project/bin/console cron:run
Listing cron jobs
If you want to see list of all jobs, run cron:list command from Symfony console.
Example: php bin/console cron:list
Example output:
+----------------------------+---------+-----------------+--------+---------------------+
| Class | Enabled | Cron expression | Is due | Next run |
+----------------------------+---------+-----------------+--------+---------------------+
| App\CronJobs\MyTestCronJob | yes | */5 * * * * | no | 2018-08-07 18:30:00 |
+----------------------------+---------+-----------------+--------+---------------------+
The listing will also let you know if the cron expression is invalid:
+----------------------------+---------+-----------------+----------------------------+----------------------------+
| Class | Enabled | Cron expression | Is due | Next run |
+----------------------------+---------+-----------------+----------------------------+----------------------------+
| App\CronJobs\MyTestCronJob | yes | */5 * * * * * | Cron expression is invalid | Cron expression is invalid |
+----------------------------+---------+-----------------+----------------------------+----------------------------+
Disabling a cron job
If you have a cron job that you don't want to delete but at the same time you don't
want to trigger it, you can define method isEnabled() and let it return false.
You can also use the packaged helper trait Rikudou\CronBundle\Helper\DisabledCronJob.
Example 1 (wihout trait):
<?php namespace App\CronJobs; use Psr\Log\LoggerInterface; use Rikudou\CronBundle\Cron\CronJobInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MyTestCronJob implements CronJobInterface { /** * The cron expression for triggering this cron job * @return string */ public function getCronExpression(): string { return "*/5 * * * *"; } /** * This method will be executed when cron job runs * * @param InputInterface $input * @param OutputInterface $output * @param LoggerInterface $logger * @return mixed */ public function execute(InputInterface $input, OutputInterface $output, LoggerInterface $logger) { $logger->debug("The cron job was executed!"); } public function isEnabled() { return false; } }
Example 2 (using trait):
<?php namespace App\CronJobs; use Psr\Log\LoggerInterface; use Rikudou\CronBundle\Cron\CronJobInterface; use Rikudou\CronBundle\Helper\DisabledCronJob; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MyTestCronJob implements CronJobInterface { use DisabledCronJob; /** * The cron expression for triggering this cron job * @return string */ public function getCronExpression(): string { return "*/5 * * * *"; } /** * This method will be executed when cron job runs * * @param InputInterface $input * @param OutputInterface $output * @param LoggerInterface $logger * @return mixed */ public function execute(InputInterface $input, OutputInterface $output, LoggerInterface $logger) { $logger->debug("The cron job was executed!"); } }
In both cases the output will be similar to:
+----------------------------+---------+-----------------+--------+---------------------+
| Class | Enabled | Cron expression | Is due | Next run |
+----------------------------+---------+-----------------+--------+---------------------+
| App\CronJobs\MyTestCronJob | no | */5 * * * * | no | 2018-08-07 18:40:00 |
+----------------------------+---------+-----------------+--------+---------------------+
Running commands as cron jobs
If you want to run commands as cron jobs, you must configure them in yaml file.
Example:
rikudou_cron: commands: clearCache: command: cache:clear cron_expression: "*/2 * * * *"
This setting will run cache:clear every two minutes.
The config file will be created (if it doesn't already exist) when you first
run any cron command (cron:list, cron:run) at
%projectDir%/config/packages/rikudou_cron.yaml.
IF you need to pass arguments or options to the command, you can do so:
rikudou_cron: commands: someCommand: command: app:my-command cron_expression: "*/2 * * * *" options: some-option: some-value arguments: some-argument: some-value
Note that the option and argument names need to match their names as defined in the target command.
Example output from cron:list:
Classes
+----------------------------+---------+-----------------+--------+---------------------+
| Class | Enabled | Cron expression | Is due | Next run |
+----------------------------+---------+-----------------+--------+---------------------+
| App\CronJobs\MyTestCronJob | no | */5 * * * * | no | 2018-09-06 15:25:00 |
+----------------------------+---------+-----------------+--------+---------------------+
Commands
+-------------+-----------------+--------+---------------------+
| Command | Cron expression | Is due | Next run |
+-------------+-----------------+--------+---------------------+
| cache:clear | */2 * * * * | yes | 2018-09-06 15:24:00 |
+-------------+-----------------+--------+---------------------+
rikudou/cron-bundle 适用场景与选型建议
rikudou/cron-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.82k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rikudou/cron-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rikudou/cron-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-19