mintware-de/native-cron-bundle

一个用于mintware-de/native-cron的Symfony包

安装: 217

依赖: 0

建议: 0

安全: 0

星星: 6

观察者: 1

分支: 0

公开问题: 0

类型:symfony-bundle

v1.0.1 2022-11-10 13:18 UTC

This package is auto-updated.

Last update: 2024-09-10 17:20:30 UTC


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]:
 >