Timed task

In some cases, we need to perform certain tasks at regular intervals. Usually we use the Crontab that comes with the Linux system to execute the scripts we write regularly, but this is not convenient. First, the default Crontab minimum unit of the Linux system is only Can support up to minutes, can not support second-level tasks, and second, if we re-script, it is not convenient and friendly to reuse resources within the framework, such as Mysql connection resources, various class libraries in the framework. In response to the above issues, the framework built a Crontab component for us to support second-level tasks.

Available from >= v2.0.5

installation

Before using the scheduled task, it must be installed and installed as follows

 composer require swoft/crontab 

annotation

In Swoft, the use of timed tasks is very simple, just define your task class with the solution of interest.

@Scheduled()

Used to declare a timed task, if you are declaring a timed task class, you must use this annotation

parameter

  • name task class. If empty, it is the full namespace path of this class.

Example usage: @Scheduled() , @Scheduled("taskName") , @Scheduled(name="taskName")

@Cron()

Declare the method that needs to be run. If this annotation is not used, the method will not be run.

parameter

  • value task's Crontab expression, supported to seconds

Example of use: @Cron("* * * * * *") , @Cron(value="* * * * * *") , the expression can be abbreviated, for example, a task to be executed every second can be defined as @Cron("*")

Cron format description

 *    *    *    *    *    *
-    -    -    -    -    -
|    |    |    |    |    |
|    |    |    |    |    +----- day of week (0 - 6) (Sunday=0)
|    |    |    |    +----- month (1 - 12)
|    |    |    +------- day of month (1 - 31)
|    |    +--------- hour (0 - 23)
|    +----------- min (0 - 59)
+------------- sec (0-59) 

Example:

  1. * * * * * * means executed once per second.
  2. 0 * * * * * means that it is executed every 0th of every minute, that is, every minute.
  3. 0 0 * * * * means that it is executed once every 0 minutes and 0 seconds, that is, every hour.
  4. 0/10 * * * * * means that every 10 seconds starting every 10th of every minute.
  5. 10-20 * * * * * means to execute once every 10-20 seconds.
  6. 10,20,30 * * * * * means that each 10th, 20th, and 30th minute is executed once every minute.

Declare scheduled tasks

Using timed tasks in Swoft is fairly straightforward, with just two steps, 声明定时任务 and 配置启用 , both of which are fairly straightforward. Let's look at the declarative task first.

 <?php declare(strict_types=1);

namespace App\Crontab;

use Swoft\Crontab\Annotaion\Mapping\Cron;
use Swoft\Crontab\Annotaion\Mapping\Scheduled;

/**
 * Class CronTask
 *
 * @since 2.0
 *
 * @Scheduled()
 */
class CronTask
{
    /**
     * @Cron("* * * * * *")
     */
    public function secondTask()
    {
        printf("second task run: %s ", date('Y-m-d H:i:s', time()));
    }

    /**
     * @Cron("0 * * * * *")
     */
    public function minuteTask()
    {
        printf("minute task run: %s ", date('Y-m-d H:i:s', time()));
    }

} 

Configuration enabled

The execution of timed tasks is based on Sweft's processes , so we need to enable the custom process of the Crontab component in the configuration as we do with user processes .

  return [
    'httpServer'     => [
            // ...
            'process' => [
                'crontab' => bean(Swoft\Crontab\Process\CrontabProcess::class)
            ],
            // ...
        ],
 ];    

As we have configured the service successfully, our scheduled task process will start.

Manual execution

In addition to regularly executing our set tasks, we can also manually execute our scheduled tasks directly in the business code, as follows.

     $crontab = BeanFactory::getBean("crontab");
    $crontab->execute("testCrontab", "method"); 

Get the crontab manager through the Bean container, and then directly use the execute($beanName,$methodName) method. This method takes two arguments, $beanName is the $beanName passed in the @Scheduled() annotation, and $methodName is passed. In the class labeled @Scheduled() , the method marked by @Cron() .

/docs/2.x/en/task/crontab.html
progress-bar