打赏

thinkphp调用Redis队列使用例子

1.Controller顶部引入use think\Queue;

2.Controller调用Redis任务

                    // 1.当前任务将由哪个类来负责处理。
                    $job  = 'app\api\job\ResumeTaskExecution';
                    // 2.将该任务推送到消息队列,等待对应的消费者去执行
                    $task_id = config('qiji_resume');//简历任务id是21
                    $data = ['task_id'=>$task_id,'uuid'=>$uuid,'mobile'=>$userinfo['data']['mobile']];
                    $isPushed = Queue::push( $job , $data);
                    // database 驱动时,返回值为 1|false  ;   redis 驱动时,返回值为 随机字符串|false
                    if( $isPushed !== false ){
                        return true;
                    }

3.队列控制器

<?php
/**
 * Created by PhpStorm.
 * User: fgf
 * Date: 2018-01-25
 * Time: 下午15:44
 */

namespace app\api\job;

use think\Log;
use think\queue\Job;
use org\Response;
use Potting\Curl;
use think\Cache;

/**
 * Redis队列推送完善简历信息给奇骥
 * Class TaskExecution
 * @package app\api\job
 */
class ResumeTaskExecution
{
    //完善简历信息更新异步通知合作第三方
    public function fire(Job $job, $data )
    {
        trace('****************TaskExecution start**********************');
        //获取奇骥 access_token
        $token_info = self::get_qj_token();
        $token_type = $token_info['message']['token_type'].' ';
        $access_token = $token_info['message']['access_token'];//提取access_token
        //获取奇骥任务列表
//        $task_list_info = self::task_list($token_type,$access_token);
//        $task_id = $task_list_info['message']['data'][0]['task_id'];//提取任务id
        $task_id = $data['task_id'];//完善简历任务id是21
        $uuid    = $data['uuid'];//奇骥标识uuid
        $tel  = $data['mobile'];
        $mobile = self::mobile_change($tel);//手机号中间四位转*号
//        print("<info>Hello Job has been done and deleted".$mobile."</info>\n");
        //任务执行
        $task_execution_info = self::task_execution($token_type,$access_token,$task_id,$uuid,$mobile);

        //如果任务执行成功后 记得删除任务,不然这个任务会重复执行,直到达到最大重试次数后失败后,执行failed方法
        if ($task_execution_info){
            trace(json_encode($task_execution_info));
//            print("<info>Hello Job has been done and deleted".print_r($task_execution_info)."</info>\n");
            $job->delete();
        }
    }

    /**
     * 手机号中间四位转*号
     * @param $tel 手机号码
     * @return mixed
     */
    public function mobile_change($tel){
        $str = substr($tel,0,3);
        $strs = substr($tel,0,4);
        if ($str == '+86'){
            $tel_sub = substr($tel,3,11);
        }elseif ($strs == '+852' || $strs == '+853' || $strs == '+886'){
            $tel_sub = substr($tel,4,11);
        }
        return substr_replace($tel_sub, '****', 3, 4);
    }

    /**
     * 获取奇骥 access_token
     * @return array
     */
    public function get_qj_token()
    {
        $appid = config('qiji_appid');
        $secret = config('qiji_secret');
        $url = config('qiji_url').'/api/client/token?appid='.$appid.'&secret='.$secret;
        //静态调用引入的扩展Curl类的curlGet()方法获取奇骥 access_token
        $data =  Curl::curlGet($url);
        return Response::response(0,[],$data);
    }

    /**
     * 获取奇骥任务列表
     * @return array
     */
    public function task_list($token_type,$access_token)
    {
//        $authorization = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImQyMzAxZGJhNjBjM2YxZmRiYmNiNDU2MzYyY2Y3Njc3MmZlZjk0OGFhMDIyNTE1ZmEzYjg0ZDJlMTA4NzY5MmY4YTZkNGFmM2E2N2ZiYzg4In0.eyJhdWQiOiIyIiwianRpIjoiZDIzMDFkYmE2MGMzZjFmZGJiY2I0NTYzNjJjZjc2NzcyZmVmOTQ4YWEwMjI1MTVmYTNiODRkMmUxMDg3NjkyZjhhNmQ0YWYzYTY3ZmJjODgiLCJpYXQiOjE1MjQ0NDY0MzAsIm5iZiI6MTUyNDQ0NjQzMCwiZXhwIjoxNTU1OTgyNDI5LCJzdWIiOiIxMyIsInNjb3BlcyI6WyIqIl19.flvULsa_kyrsW11yHplqSnFH8bvKFrYfUD_y0b6OjrjdHJ-mhKCMZy64h1UkfYJVGK-ntTIx8dXQQzqbDzWiRS8XWOVQznu9hKMbRh3KzRd28hv7FuYPHLZEfUhS8qsMykyIbVK3-gbjJXn093l1VqRIkls_YehNn7xsViJng73yNma4B160rUfrUZX7jOqlaazDflSepudPEfeYwKWYEXwo8uXAsRS3l0kN3i84Zwc0DOZfjG4FebXAqqNVBINKOFaexPtvJIkyV_0oHQpujoJBIwx528QlZHyrmsVsm9tlMRLpZctRQpkbIvWyPjkZ6pREV8AablQGGIXkCBLU9mak1RVyV7J3LnCQ4UgHn1vAao6gUQ_YoBEmiymnwCeScNJwxM9VzDKICBjtDJh7rlarx_R9VKYpGURzYydN2HBRBRsrx0ju1qFFC5_lm4zLLPC2XlnlJzlnMDjyQeLexGbRVUe9uxzWVCBpg-GP-REzaTLLBLpHNAbv_x5H0-lMc3uAqSAHlugQIPUE3GCsN576OkW7eEr_kXbcjYytZjYZmEYRW0IRXNQaJOm39DsWDdE59MCnWmkC2QYAB3H4ShsSnoCd3XW37BDTVQkEGqeFKQ49z47j1WKlzvdJ90vSNPkqeOld8n3vtRPGCN9kYw4nXRnmg86OcMeG-4OFVf0';
        $authorization = $token_type.$access_token;
        $url = config('qiji_url').'/api/client/tasks';
        //静态调用引入的扩展Curl类的curlGetHeader()方法请求任务列表
        $data =  Curl::curlGetHeader($url,$authorization);
        return Response::response(0,[],$data);
    }

    /**
     * 任务执行
     * @param $uuid 奇骥的用户标识
     * @param $task_id 任务id
     * @return array
     */
    public function task_execution($token_type,$access_token,$task_id,$uuid,$mobile)
    {
        $authorization = $token_type.$access_token;
        $url = config('qiji_url').'/api/client/tasks/'.$task_id.'/execute';
//        $uuid = "20333930-4478-11e8-b2ec-bb1cfbef82f1";//奇骥的用户标识
//        $record = "ceshi!";//执行过程记录
        $data = array(
            "uuid"=>$uuid,//奇骥的用户标识
            "record"=>'完成信息:用户“'.$mobile.'”完成“易展翅完善简历”任务',//执行过程记录
        );
        //静态调用引入的扩展Curl类的curlPostHerder()方法请求任务执行
        $info =  Curl::curlPostHerder($url,$data,$authorization);
        return Response::response(0,[],$info);
    }

    public function failed($data)
    {
        return 'failed';
        // ...任务达到最大重试次数后,失败了
    }
}

 

posted on 2018-06-08 16:27  头大的冯冯  阅读(479)  评论(0编辑  收藏  举报

导航