goksagun/scheduler-bundle

定时任务包

安装次数: 9 333

依赖: 0

建议者: 0

安全: 0

星标: 6

关注者: 2

分支: 5

开放问题: 0

类型:symfony-bundle

v2.3.4 2024-09-03 07:09 UTC

README

命令调度器允许你在应用程序内部流畅地定义你的命令调度。使用调度器时,你只需要在服务器上添加单个Cron条目。你的任务调度在scheduler.yaml文件、Schedule注解或database中定义。使用调度器时,你只需在服务器上添加以下Cron条目

* * * * * php /path-to-your-project/bin/console scheduler:run >> /dev/null 2>&1

这个Cron条目会每分钟调用命令调度器。当执行scheduler:run命令时,应用程序将评估你的计划任务并运行到期的任务。如果你想异步运行任务,可以使用带有async标志的命令调度器scheduler:run --async

安装

请确保已经全局安装了Composer,如Composer文档中的安装章节所述。

使用Symfony Flex的应用程序

打开命令行,进入你的项目目录,然后执行以下命令

$ composer require goksagun/scheduler-bundle

不使用Symfony Flex的应用程序

步骤1:下载Bundle

打开命令行,进入你的项目目录,并执行以下命令以下载此Bundle的最新稳定版本

$ composer require goksagun/scheduler-bundle

步骤2:启用Bundle

然后,将Bundle添加到你的项目config/bundles.php文件中注册的Bundle列表中,以启用Bundle

// config/bundles.php

return [
    // ...
    Goksagun\SchedulerBundle\SchedulerBundle::class => ['all' => true],
];

然后,将控制台命令添加到config/packages目录中的scheduler yaml文件scheduler.yaml

scheduler:
    enabled: true
    async: ~
    log: ~
    tasks:
         - { name: command:name argument --option, expression: "* * * * *" }
         - { name: another-command:name, expression: "@hourly" }

或使用属性

use Goksagun\SchedulerBundle\Attribute\Schedule;
use Symfony\Component\Console\Command\Command;

#[Schedule(name: 'command:name argument --option', expression: '*\/10 * * * *')]
class AttributedCommand extends Command
{
    // 
}

或使用注解

use Goksagun\SchedulerBundle\Annotation\Schedule;
use Symfony\Component\Console\Command\Command;

/**
 * @Schedule(name="command:name argument --option", expression="*\/10 * * * *")
 */
class AnnotatedCommand extends Command
{
    // 
}

或将任务添加到数据库,你可以使用scheduler:add命令将任务添加到数据库

php bin/console scheduler:add 'command:name argument --option' '@daily'

如果你想要编辑任务,可以使用scheduler:edit命令

php bin/console scheduler:edit [id] 'command:name argument --no-option' '@hourly'

如果你想要删除任务,可以使用scheduler:delete命令

php bin/console scheduler:delete [id]

如果你想要列出任务,可以使用scheduler:list命令

php bin/console scheduler:list

步骤4:添加Bundle日志表模式(可选)

然后,如果你想跟踪计划任务,请添加Bundle日志表模式并将执行的任务存储到数据库中

php bin/console doctrine:schema:update --force