mintware-de / native-cron-bundle
一个用于mintware-de/native-cron的Symfony包
v1.0.1
2022-11-10 13:18 UTC
Requires
- php: ^8.1
- mintware-de/native-cron: ^1.1.2
- symfony/config: ^6.1
- symfony/dependency-injection: ^6.1
- symfony/http-kernel: ^6.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5
README
此包提供了一个用于mintware-de/native-cron
包的Symfony Bundle。
主要目的是轻松管理您的应用程序的cron作业。
安装
composer require mintware-de/native-cron-bundle
由于cron作业的存储方式根据操作系统而不同,您需要在服务定义中设置CrontabFileLocatorInterface
。
此包仅与嵌入式crontab文件一起工作。因此,请确保实现支持此功能。
services: MintwareDe\NativeCron\Filesystem\CrontabFileLocatorInterface: class: MintwareDe\NativeCron\Filesystem\DebianCrontabFileLocator
要求
- PHP 8.1+
- Symfony 6.1
使用方法
标记命令为cron作业
安装包后,您可以在控制台命令上使用#[CronJob(...)]
注解(MintwareDe\NativeCronBundle\Attribute\CronJob
)。
此注解接受2-5个参数
#[CronJob( name: 'my_cron_job', // This is the name of the cron job. It is used to trigger the command // using the mw:cron:run command # .---------------- minute (0 - 59) # | .-------------- hour (0 - 23) # | | .------------ day of month (1 - 31) # | | | .---------- month (1 - 12) # | | | | .-------- day of week (0 - 6; Sunday=0) # | | | | | executeAt: '* * * * *', // This is the execution time definition. The format is identical to the crontab file. user: 'root', // The user that will be used to run the command. Optional, default = root arguments: [ // The arguments that are passed to your command. See also here (ArrayInput): https://symfony.com.cn/doc/current/console/calling_commands.html 'name' => 'Foo', // InputArgument '--yell' => true, // Input option ], )] #[AsCommand(...)] class MyCommand extends \Symfony\Component\Console\Command\Command { // ... }
列表 / 安装 / 卸载
您可以使用Symfony控制台列出、安装和卸载cron作业。**记住:安装和卸载可能需要更高的用户权限!**(如su root / sudo等)
列表
$ php bin/console mw:cron:list Cron jobs ========= -------------- ------------ ---------------- ------------ Name Execute At Arguments Command -------------- ------------ ---------------- ------------ my_cron_job * * * * * [] MyCronJob my_cron_job2 0 1 * * * {"arg2":"foo"} MyCronJob2 -------------- ------------ ---------------- ------------
安装
$ php bin/console mw:cron:install New crontab content =================== * * * * * root /usr/local/Cellar/php/8.1.10_1/bin/php /source/bin/console mw:cron:run my_cron_job 0 1 * * * root /usr/local/Cellar/php/8.1.10_1/bin/php /source/bin/console mw:cron:run my_cron_job2 Confirm crontab? (yes/no) [yes]: > yes
卸载
$ php bin/console mw:cron:uninstall Uninstall cron jobs =================== [WARNING] The following cron jobs will be uninstalled: * * * * * root /usr/local/Cellar/php/8.1.10_1/bin/php /source/bin/console mw:cron:run my_cron_job 0 1 * * * root /usr/local/Cellar/php/8.1.10_1/bin/php /source/bin/console mw:cron:run my_cron_job2 Confirm uninstall? (yes/no) [yes]: >