PHP code example of uniondrug / crontab

1. Go to this page and download the library: Download uniondrug/crontab library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uniondrug / crontab example snippets



return [
    'default' => [
        ......
        'providers'           => [
            ......
            \Uniondrug\Crontab\CrontabServiceProvider::class,
        ],
    ],
];


return [
    'default' => [
        'host'       => 'http://0.0.0.0:8000',
        'class'      => \Uniondrug\Server\Servitization\Server\HTTPServer::class,
        'options'    => [
            'pid_file'        => __DIR__ . '/../tmp/pid/server.pid',
            'worker_num'      => 8,
            'task_worker_num' => 2,
        ],
        'autoreload' => true,
        'processes'  => [
            \Uniondrug\Crontab\Processes\ManagerProcess::class,     // 增加Crontab管理进程
            \Uniondrug\Crontab\Processes\ExecProcess::class,        // 增加Crontab运行进程
        ],
        ......
    ],


/**
 * crontab.php  Crontab的运行时配置
 *
 * workerCount   启动的任务工作进程数量
 * workerMaxTask 每个工作进程处理的任务上限,处理到这个数量后,自动重启
 *
 */
return [
    'default' => [
        'workerCount'   => 4,
        'workerMaxTask' => 10,
    ],
];


/**
 * TestTask.php
 *
 */

namespace App\Tasks;

use Uniondrug\Server\Task\TaskHandler;

/**
 * Class TestTask
 *
 * @package App\Tasks
 * @Schedule(cron="* * * * *", second="*", times=60, start="2018-03-08 21:01:00")
 */
class TestTask extends TaskHandler
{
    private static $i = 0;
    public function handle($data = [])
    {
        self::$i ++;
        console()->debug(__CLASS__ . " called " . self::$i);
        //sleep(40);
    }
}