PHP code example of maxsky / think5-queue

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/ */

    

maxsky / think5-queue example snippets


return [
    'connector' => 'redis' // 驱动类型,可选择 Redis(默认),sync:同步执行,database:数据库驱动,topthink:Topthink 驱动
                           // 或其他自定义的完整的类名
];

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) {
        
    }

}

think\Queue::push($job, $data = [], $queue = null)

think\Queue::later($delay, $job, $data = [], $queue = null)

Queue::remove($jobReturnValue, 'push', $queue);

// 假设我们的任务类叫 Job1 并符合前文要求,项目为单模块,命名空间为 app\job,同时传了一个数组过去
think\Queue::push('Job1', ['abc' => 123], 'testJob');
bash
php think queue:work

php think queue:listen
bash
php think queue:work
bash
php think queue:work --daemon
bash
php think queue:work --queue testJob

# 一直监听加上 --daemon
php think queue:work --queue testJob --daemon
bash
php think queue:work --queue testJob,default,xxxTask --daemon