1. Go to this page and download the library: Download maxsky/think5-queue 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/ */
namespace app\job;
use think\queue\Job;
class Job1 {
public function fire(Job $job, $data) {
//....这里执行具体的任务
if ($job->attempts() > 3) {
// 通过这个方法可以检查这个任务的重试次数
}
// 如果任务执行成功,记得删除任务,否则任务会重复执行,直到达到最大重试次数后失败后执行 failed 方法
$job->delete();
// 重新发布任务
$job->release($delay); // $delay 为延迟时间,单位秒
}
public function failed($data) {
// 任务达到最大重试次数后执行此方法
}
}
namespace app\lib\job;
use think\queue\Job;
class Job2 {
public function task1(Job $job, $data) {
}
public function task2(Job $job, $data) {
}
public function failed($data) {
}
}