rikudou/cron-bundle

为 Symfony 4+ 提供简单有效的 cron 实现

安装次数: 4,090

依赖项: 0

建议者: 0

安全: 0

星级: 3

关注者: 3

分支: 2

开放问题: 0

类型:symfony-bundle

v1.2.0 2023-09-22 14:05 UTC

README

通过 composer 安装: composer require rikudou/cron-bundle

如果使用 Symfony Flex,此包将自动启用。

如何使用

每个 cron 作业必须实现 Rikudou\CronBundle\Cron\CronJobInterface 接口(实现该接口的每个类都将自动注册为 cron 作业),该接口定义了两个方法

  • string getCronExpression() - cron 表达式
  • execute(InputInterface $input, OutputInterface $output, LoggerInterface $logger) - 将要执行的方法,您可以写入控制台输出、使用控制台输入和使用记录器

示例

<?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!");
    }
}

cron 作业通过控制台命令 cron:run 执行,该命令应每分钟执行一次。

crontab 示例

* * * * * php /path/to/project/bin/console cron:run

列出 cron 作业

如果您想查看所有作业的列表,请从 Symfony 控制台运行 cron:list 命令。

示例: php bin/console cron:list

示例输出

+----------------------------+---------+-----------------+--------+---------------------+
| Class                      | Enabled | Cron expression | Is due | Next run            |
+----------------------------+---------+-----------------+--------+---------------------+
| App\CronJobs\MyTestCronJob | yes     | */5 * * * *     | no     | 2018-08-07 18:30:00 |
+----------------------------+---------+-----------------+--------+---------------------+

列表还将告知您 cron 表达式是否无效

+----------------------------+---------+-----------------+----------------------------+----------------------------+
| Class                      | Enabled | Cron expression | Is due                     | Next run                   |
+----------------------------+---------+-----------------+----------------------------+----------------------------+
| App\CronJobs\MyTestCronJob | yes     | */5 * * * * *   | Cron expression is invalid | Cron expression is invalid |
+----------------------------+---------+-----------------+----------------------------+----------------------------+

禁用 cron 作业

如果您有一个不想删除但也不想触发的 cron 作业,您可以定义方法 isEnabled() 并使其返回 false。

您还可以使用打包的辅助特型 Rikudou\CronBundle\Helper\DisabledCronJob

示例 1(无特型)

<?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;
    }
}

示例 2(使用特型)

<?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!");
    }

}

在这两种情况下,输出将类似

+----------------------------+---------+-----------------+--------+---------------------+
| Class                      | Enabled | Cron expression | Is due | Next run            |
+----------------------------+---------+-----------------+--------+---------------------+
| App\CronJobs\MyTestCronJob | no      | */5 * * * *     | no     | 2018-08-07 18:40:00 |
+----------------------------+---------+-----------------+--------+---------------------+

将命令作为 cron 作业运行

如果您想将命令作为 cron 作业运行,您必须在 yaml 文件中配置它们。

示例

rikudou_cron:
    commands:
      clearCache:
        command: cache:clear
        cron_expression: "*/2 * * * *"

此设置将每两分钟运行一次 cache:clear。

当您首次运行任何 cron 命令(cron:listcron:run)时,将创建配置文件(如果尚未创建),位于 %projectDir%/config/packages/rikudou_cron.yaml

如果您需要传递参数或选项给命令,您可以这样做

rikudou_cron:
    commands:
      someCommand:
        command: app:my-command
        cron_expression: "*/2 * * * *"
        options:
          some-option: some-value
        arguments:
          some-argument: some-value

请注意,选项和参数名称需要与目标命令中定义的名称匹配。

来自 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 |
+-------------+-----------------+--------+---------------------+