承接 dades/scheduledtask 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dades/scheduledtask

Composer 安装命令:

composer require dades/scheduledtask

包简介

The Symfony Bundle to schedule tasks

README 文档

README

A Symfony Bundle that schedule tasks and commands on Windows and Linux.
It uses the cron system from Linux on both operating systems.

Installation

  1. Run the following command to add the bundle to your project as a composer dependency:
    composer require dades/scheduledtask

  2. Add the bundle to your application kernel:

// app/AppKernel.php
public function registerBundles()
{
    // ...
    $bundle = array(
        // ...
        new Dades\ScheduledTaskBundle\DadesScheduledTaskBundle(),
    );
    // ...

    return $bundles;
}
  1. If you didn't create your database before, run php bin/console doctrine:database:create

  2. Then you have to create the table that will store your scheduled tasks:
    php bin/console doctrine:schema:update --force

Windows OS

  1. You have to tell to your system to check every minute if a task should be run. To do that, run the following command on your cmd:
    schtasks /CREATE /TN "uniqueName" /TR "php D:\path\to\your\project\bin\console cron:run" /SC minute

Make sure that schtasks is globally install on your system.

Linux OS

  1. You have to tell to your system to check every minute if a task should be run. To do that, you have to add a cronjob in your crontab:
    1. run crontab -e to edit your crontab

    2. add * * * * * php /path/to/your/project/bin/console cron:run >> ~/tmp 2>&1 in the file

How it works

Now you're ready to create all the scheduled tasks you want. You just need to handle these 2 classes:
Dades\ScheduledTaskBundle\Entity\ScheduledTask
Dades\ScheduledTaskBundle\Service\ScheduledTaskService

Inject the service that handles ScheduledTask class:

use Dades\ScheduledTaskBundle\Service\ScheduledTaskService;

public function indexAction(Request $request, ScheduledTaskService $scheduled)
{
    //code
}

Note that this is a Service, so you can inject it anywhere you want.

Create a scheduled task

Once your Service is injected, you can do the following thing:

public function indexAction(Request $request, ScheduledTaskService $scheduledTaskService)
{
    $task = $scheduledTaskService->create();
    $task->setCommand("php --version")->setCronExpresion("* * * * *");
    $scheduledTaskService->save($task);

    return new Response("it works");
}

In this example, the "php --version" command will be run every minute.

Get one or more scheduled tasks

You can get a task by its id or get all tasks in an array. Example:

public function indexAction(Request $request, ScheduledTaskService $scheduledTaskService)
{
    //get the task with id 1
    $task = $scheduledTaskService->getScheduledTask(1);
    //get all tasks
    $tasks = $scheduledTaskService->getScheduledTasks();

    //...
}

Update a scheduled task

To update a task, just set the value that you want to change and call the ScheduledTaskService:

public function indexAction(Request $request, ScheduledTaskService $scheduledTaskService)
{
    //get the task with id 1
    $task = $scheduledTaskService->getScheduledTask(1);
    $task->setCommand("crontab -l")->setCronExpresion("0 5 * * *");
    $scheduledTaskService->update($task);

    //...
}

Delete a task

You have to get the task that you want to remove and call the delete method:

public function indexAction(Request $request, ScheduledTaskService $scheduledTaskService)
{
    //get the task with id 1
    $task = $scheduledTaskService->getScheduledTask(1);
    $scheduledTaskService->delete($task);

    //...
}

Where the magic happens

Now you know how to install, setup and handle scheduled tasks, but you don't know yet where they are executed.
Just have a look in the Dades\ScheduledTaskBundle\Command\RunCronCommand class.

if ($this->scheduledTaskService->isDue($task)) {
    exec($task->getCommand(), $stderr, $status);

These 2 lines to the trick.
The first line check if the task $task must be run now.
The second line execute the command.

More informations

The stdout and stderr streams are logged in the var/logs/dades_scheduled_task_bundle.log.
Thanks to this file, you have a trace of all your task executions.
If the file doesn't exist, don't worry, it will be automatically created.

This bundle use the dragonmantank/cron-expression library.
This lib read the cron expression of each task to determine if this task should be run now.
I invite you to read more about this here.

统计信息

  • 总下载量: 92
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固