1. Go to this page and download the library: Download ezijing/hyperf-tasks 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/ */
declare(strict_types=1);
use Hyperf\Di\Annotation\Inject;
use Hyperf\Tasks\tasks\TaskInterface;
class IndexController extends AbstractController
{
public function index(TaskInterface $task)
{
$userId = 1;
$task->setOperator($userId);
// 设置延迟时间(s)
$task->delay(10);
// 标识
$key = 'test1';
// 参数
$params = ['id' => 1];
$task->publish($key, []);
}
}
declare(strict_types=1);
use Hyperf\Di\Annotation\Inject;
use Hyperf\Tasks\tasks\TaskInterface;
class IndexController extends AbstractController
{
public function index(TaskInterface $task)
{
// 标识
$key = 'test1';
$task->unpublish($key);
}
}
declare(strict_types=1);
use Hyperf\Di\Annotation\Inject;
use Hyperf\Tasks\tasks\TaskInterface;
class IndexController extends AbstractController
{
public function index(TaskInterface $task)
{
// 标识
$key = 'test1';
// 批量执行
$batchSize = 5;
$task->run($key', $batchSize, function($params) {
sleep(5);
// $params 处理传过来的参数
// todo 执行业务内容
// 返回参数,必须是数组或是不返回
return $params;
});
}
}