skrip42/cron-bundle

Symfony Cron-bundle

安装: 257

依赖: 0

建议者: 0

安全: 0

星标: 2

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v1.2.3 2021-01-28 05:51 UTC

This package is auto-updated.

Last update: 2024-09-28 13:30:51 UTC


README

symfony的定时任务调度器,扩展了类似cron的语法。

安装

  • 运行 composer require skrip42/cron-bundle
  • 创建数据库 php ./bin/console make:migrationphp ./bin/console doctrine:migration:migrate
  • 在您的crontab中添加 * * * * * ./bin/console cron:run

定时任务语法

任务模式为 {分钟}_{小时}_{日}_{星期}_{月}_{年}

运算符可以组合使用,例如:0_0_*/2-1_*_*_* - 将在奇数日的午夜执行。

可用命令

  • cron:add - 交互式添加cron任务
  • cron:closest - 显示最接近的任务列表
  • cron:list - 显示cron任务列表
  • cron:list --all - 显示所有活动状态cron任务列表
  • cron:optimize - 禁用过时的cron任务
  • cron:run - 运行实际任务
  • cron:update $id - 更新cron任务
  • cron:toggle $id - 切换cron任务活动状态

使用方法

....... //you namespace declaration

use Skrip42\Bundle\CronBundle\Services\Cron;
use Skrip42\Bundle\CronBundle\Component\Pattern;

....... //you class declaration

protected $cron; //instance of Cron service

public function __construct(Cron $cron) {
    $this->cron = $cron
}

........ //in you method
$this->cron->getActualOnCurrentTime(); // return array of actual Schedule entity
$this->cron->optimize(); // disable outdated cron tasks
$this->cron->closestList($count); // return $count closest task in format: [$id, $command, $c]
$this->cron->getList($all); // return all schedule task? if $all = true includes disabled task
$this->cron->addSchedule($patternString, $commandString); // create new schedule task
$this->cron->toggleSchedule($id); // toggle schedule task activity
$this->cron->updateSchedule($id, $patternString, $commandString); // update schedule task

$pattern = new Pattern($patternString); // get pattern object
$pattern->test(new DateTime('NOW')); // test pattern for match date
$pattern->getClosest($count); //return $count closest date that match pattern

$schedule = reset($this->cron->getList()); // schedule is standart doctrine entity
$schedule->getId(); //return id
$schedule->getCommand(); // return command string
$schedule->setCommand($commandString); // set command string to schedule
$schedule->getPattern(); // return pattern string
$schedule->setPattern($patternString); // set pattern string to schedule
$schedule->toggleActive(); // toggle schedule activity

$this->container->get('doctrine')->getManager()->flush(); // to save change
........