xactsystems/command-scheduler

一个 Symfony 命令调度器。

安装: 878

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 2

类型:symfony-bundle


README

此组件允许您通过配置在 DBAL 存储中的作业列表来提供作业调度。

可以创建一次性的作业以及基于 cron 表达式的重复作业。组件具有管理预定命令的管理员界面,以及通过 CommandScheduler 类。

文档

1) 将 XactCommandScheduler 添加到您的项目中

composer require xactsystems/command-scheduler

2) 创建调度器表

php bin/console doctrine:schema:create

3) 添加调度器管理员视图的路由

config/routes.yaml

command_scheduler:
    resource: "@XactCommandSchedulerBundle/Resources/config/routing.yaml"

4) 使用管理员视图

浏览到 http://my-project/command-scheduler/list

5) 运行命令调度器

php bin/console xact:command-scheduler

该命令接受以下选项

  • --max-runtime=nnn 设置调度器运行的秒数。0(默认)运行无限期。
  • --idle-time=nnn 设置当命令队列空时调度器休眠的秒数。默认为5。

通过代码管理调度器

添加预定命令

use Xact\CommandScheduler\Scheduler\CommandScheduler;
use Xact\CommandScheduler\Entity\ScheduledCommand;
use Cron\CronExpression;
...

public function myControllerAction(CommandScheduler $scheduler)
{
    $scheduledCommand = new ScheduledCommand();
    $scheduledCommand->setDescription('My daily command');
    $scheduledCommand->setCommand( 'app:my-daily-command' );
    $scheduledCommand->setCronExpression( CronExpression::factory('@daily') );
    $scheduledCommand->setPriority(5);  // Defaults to 1
    $scheduler->add( $scheduledCommand );
}

禁用命令

use Xact\CommandScheduler\Scheduler\CommandScheduler;
use Xact\CommandScheduler\Entity\ScheduledCommand;
...

public function myControllerAction(int $commandId, CommandScheduler $scheduler)
{
    $scheduler->disable($commandId);
}

立即运行命令

use Xact\CommandScheduler\Scheduler\CommandScheduler;
use Xact\CommandScheduler\Entity\ScheduledCommand;
...

public function myControllerAction(int $commandId, CommandScheduler $scheduler)
{
    $scheduler->runImmediately($commandId);
}

删除命令

use Xact\CommandScheduler\Scheduler\CommandScheduler;
use Xact\CommandScheduler\Entity\ScheduledCommand;
...

public function myControllerAction(int $commandId, CommandScheduler $scheduler)
{
    $scheduler->delete($commandId);
}

获取活动命令列表

use Xact\CommandScheduler\Scheduler\CommandScheduler;
use Xact\CommandScheduler\Entity\ScheduledCommand;
...

public function myControllerAction(CommandScheduler $scheduler)
{
    $commands = $scheduler->getActive();
    foreach ($commands as $command) {
        ...
    }
}

通过 CommandSchedulerFactory 创建命令

当使用工厂时,您可以设置以下命令设置的配置值

#config/bundles/xact_command_scheduler.yml

xact_command_scheduler:
    clear_data: ~          # Defaults to true
    retry_on_fail: ~       # Defaults to false
    retry_delay: ~         # Defaults to 60
    retry_max_attempts: ~  # Defaults to 60

然后您可以使用工厂方法创建您的预定命令。上述配置参数将应用于通过工厂方法创建的命令,除非在方法调用中覆盖。

use Xact\CommandScheduler\CommandSchedulerFactory;

class MyController extends AbstractController
{
    private CommandSchedulerFactory $commandFactory;

    public function __controller(CommandSchedulerFactory $commandFactory) {
        $this->commandFactory = $commandFactory;
    }

    public function myEmailAction(EntityManagerInterface $em) {
        $myLargeDataObject = 'Some enormous serialized value you want to store in the command and probably delete once the command is complete.';
        $command = $this->commandFactory->createImmediateCommand(
            'My test command',
            'app:test-command',
            null,
            $myLargeDataObject,
        );
        $em->persist($command);
        $em->flush();
    }
}

工厂方法

    /**
     * Create a command to run immediately
     *
     * @param string[]|null $arguments
     * @param bool|null $clearData If null, the configuration value 'clear_data' is used. Default true.
     * @param bool|null $retryOnFail If null, the configuration value 'retry_on_fail' is used. Default false.
     * @param int|null $retryDelay If null, the configuration value 'retry_delay' is used. Default 60.
     * @param int|null $retryMaxAttempts If null, the configuration value 'retry_max_attempts' is used. Default 60.
     */
    public function createImmediateCommand(
        string $description,
        string $command,
        ?array $arguments = null,
        ?string $data = null,
        ?bool $clearData = null,
        ?bool $retryOnFail = null,
        ?int $retryDelay = null,
        ?int $retryMaxAttempts = null
    ): ScheduledCommand {}

    /**
     * Create a command scheduled by a CRON expression
     *
     * @param string[]|null $arguments
     * @param bool|null $clearData If null, the configuration value 'clear_data' is used. Default true.
     * @param bool|null $retryOnFail If null, the configuration value 'retry_on_fail' is used. Default false.
     * @param int|null $retryDelay If null, the configuration value 'retry_delay' is used. Default 60.
     * @param int|null $retryMaxAttempts If null, the configuration value 'retry_max_attempts' is used. Default 60.
     */
    public function createCronCommand(
        string $cronExpression,
        string $description,
        string $command,
        ?array $arguments = null,
        ?string $data = null,
        ?bool $clearData = null,
        ?bool $retryOnFail = null,
        ?int $retryDelay = null,
        ?int $retryMaxAttempts = null
    ): ScheduledCommand {}

cron 备注

该组件使用 dragonmantank/cron-expression CronExpression 类来确定运行时间,并且您可以使用非标准的预定义调度定义。有关更多详细信息,请参阅 Cron 格式

致谢

许可证

此组件在 MIT 许可下发布。请参阅组件中的完整许可证。

LICENSE