tp6 消息队列

composer require topthink/think-queue

/config/queue.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------

use think\facade\Env;

return [
    'default'     => 'redis',
    'connections' => [
        'sync'     => [
            'type' => 'sync',
        ],
        'database' => [
            'type'       => 'database',
            'queue'      => 'default',
            'table'      => 'jobs',
            'connection' => null,
        ],
        'redis'    => [
            'type'       => 'redis',
            'queue'      => 'default',
            'host'       => '127.0.0.1',
            'port'       => 6379,
            'password'   => Env::get('REDIS.REDIS_PASSWORD', ''),
            'select'     => Env::get('REDIS.SELECT', ''),
            'timeout'    => 0,
            'persistent' => false,
        ],
    ],
    'failed'      => [
        'type'  => 'none',
        'table' => 'failed_jobs',
    ],
];

调用消息队列

<?php

declare (strict_types = 1);

namespace app\services\tool;

use app\services\BaseServices;
use think\facade\Db;
use think\Queue;

/**
 *消息队列
 * Class JobServices
 * @package app\services\tool
 */
class JobServices extends BaseServices {

    /**
     * 添加队列公共函数        php think queue:work --queue helloJobQueue
     * supervisor 命令      /www/server/php/73/bin/php think queue:work --queue PushCourseMsg
     * @param type $type  1 
     * @param type $data   数据 数组格式
     * @param type $seconds   延迟秒数  0 立即推送  大于0 延迟消息队列
     */
    public function push_queue( $type, $data, $seconds=0 ) {
        $isPushed = false;
        switch ($type) {
            case 1:
                $jobHandlerClassName = 'app\job\course\PushCourseMsg';
                $jobQueueName = "PushCourseMsg";
                $jobData = $data;
                $QueueApp = app()->make(Queue::class);
                if ($seconds > 0) {
                    $isPushed = $QueueApp->later($seconds, $jobHandlerClassName, $jobData, $jobQueueName);
                } else {
                    $isPushed = $QueueApp->push($jobHandlerClassName, $jobData, $jobQueueName);
                }
                break;
            default:
                break;
        }
        return $isPushed;
    }

}

执行逻辑

app\job\test\Test1.php

<?php
namespace app\job\test;


use think\queue\Job;

class Test1 {
    /**
     * fire方法是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array $data 发布任务时自定义的数据
     */
    public function fire(Job $job, array $data) {
        // 有些任务在到达消费者时,可能已经不再需要执行了
        $isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data);
        if (!$isJobStillNeedToBeDone) {
            $job->delete();
            return;
        }

        $isJobDone = $this->doHelloJob($data);
        if ($isJobDone){
            $job->delete();
            echo "删除任务" . $job->attempts() . '\n';
        }else{
            if ($job->attempts() > 3){
                $job->delete();
                echo "超时任务删除" . $job->attempts() . '\n';
            }
        }
    }

    /**
     * 有些消息在到达消费者时,可能已经不再需要执行了
     * @param array $data
     * @return bool
     */
    private function checkDatabaseToSeeIfJobNeedToBeDone(array $data) {
        return true;
    }

    /**
     * 根据消息中的数据进行实际的业务处理...
     * @param array $data
     * @return bool
     */
    private function doHelloJob(array $data) {
        echo "<pre>";var_dump(  $data , date("Y-m-d H:i:s")  );
        return true;
    }
}

坑:

runtime日志出现2006连接数据库丢失

config/database.php

// 是否需要断线重连
'break_reconnect' => false,
// 断线标识字符串
'break_match_str' => ['2006'],

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值