lingoda/cron-bundle

Lingoda cron bundle

安装次数: 31,982

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 14

分支: 0

开放问题: 0

类型:symfony-bundle

2.0.2 2024-03-14 07:17 UTC

README

此bundle允许调度将在与触发它们的进程不同的进程中执行的作业

使用方法

可以通过扩展ScheduleBaseCronJob抽象类或实现CronJobInterface接口来使用。这些将被您的Symfony应用程序自动识别。然后就没有什么可做的了,只需执行命令bin/console lg:cron:trigger-due-jobs。这将向应用程序的消息总线发送每个已识别的到期的cron作业的消息。通常,这意味着消息将被放入某种队列中,然后被处理程序取走并实际运行作业。

在实现cron作业时,不应期望cron确实会在计划的时间运行。

  • 队列可能不可用或过载,并且您无法预测延迟
  • 实际的先前运行时间将传递到您的运行方法中。使用它!
  • 作业可能是手动运行的。或者它可能已经被手动运行过。甚至多次。

触发单个作业

bin/console lg:cron:run-job App\\Cron\\SomeCronJobImplementation

示例

扩展ScheduleBaseCronJob

class MyCronJob extends ScheduleBaseCronJob
{
    protected function getSchedule(): Schedule
    {
        // this job will execute every hour at the 30th minute
        return Schedule::everyHour(30);
    }

    public function run(): void
    {
        // do the job here
    }
}

实现CronJobInterface

class MyCronJob implements CronJobInterface
{
    public function run(): void
    {
        // do the job
    }

    public function shouldRun(DateTimeInterface $lastFinishTime = null): bool
    {
        // implement your own logic for determining if a job should
        // at a particular time or not
    }

    public function getLockTTL(): float
    {
        // before running, an expiring lock is acquired for each job to prevent
        // a job of the same type to run at the same time; with this method
        // the job can specify a custom expiry period in seconds for the lock

        return 30.0;
    }
    
    public function __toString(): string
    {
        // Here comes the cron expression as string, e.g. 30 1 * * *
        return '30 1 * * *';
    }
}