PHP code example of annon / think-nsq-queue

1. Go to this page and download the library: Download annon/think-nsq-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/ */

    

annon / think-nsq-queue example snippets


namespace app\job;

use annon\queue\job\NSQJob;

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\job;

use annon\queue\job\NSQJob;

class Job2 {
    public function task1(Job $job, $data) {
          
    }
    
    public function task2(Job $job, $data) {
         
    }
    
    public function failed($data) {
        
    }
}

bash
php think nsq:listen