laravel延时队列的使用

本文介绍了在Laravel中使用延时队列处理订单推送、发货和删除等任务的场景。通过配置数据库、执行命令创建队列表、创建处理类及编写控制器代码,实现定时重试功能。例如,订单失败的任务会在3或10分钟后自动重新发起请求。

一、需求场景:
订单推送,订单发货,订单删除(针对第三方公司)
当推送是不,发货失败,删除失败的订单要重新发起请求,这里就可以用延时队列,
配置5分钟请求一次,或者10分钟再次请求

1、使用前在配置文件queue 配置好database

2、执行以下命令迁移文件 如果已经存在请忽略 php artisan queue:table  &&  php artisan migrate

3、创建类

$ php artisan make:job FailOrder
Job created successfully.

4、写代码

在控制器总执行如下:

FailOrder::dispatch($order_data, 'UpdateOrderExpress')->delay(Carbon::now()->addminutes(5))->onQueue('failOrder');exit;

表中被插入一条记录,3分钟后执行

执行队列如下:

php artisan queue:work database --queue=failOrder --sleep=1 --tries=3

执行完,3分钟后,到时间之后自动执行,数据库的记录没了,成功。

 

类测试方法:

<?php

namespace App\Jobs;

use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Support\Facades\Log;

class FailOrder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $program;
    public $method;
    public $api_url;

    const LANXILI_ORDER_RELATION = 'logistics';

    /**
     * Create a new job instance.
     *
     * FailOrder constructor.
     * @param $program @参数
     * @param $method @接口类型
     */
    public function __construct($program, $method)
    {
        //
        $this->api_url = env('LANXILI_ORDER_API');
        $this->program = $program;
        $this->method = $method;

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        $url = '';
        switch ($this->method) {
            case 'UpdateOrder':

                $url = $this->api_url . '/InternalApi/Order/UpdateOrder';
                break;

            case 'UpdateOrderExpress':

                $url = $this->api_url . '/InternalApi/Order/UpdateOrderExpress';
                break;

            case 'RemoveOrder':

                $url = $this->api_url . '/InternalApi/Order/RemoveOrder';
                break;

            default:

                break;
        }

        echo date('Y-m-d H:i:s') . ": 开始 ===========" . PHP_EOL;
        try {

            $result = $this->postJson($url, $this->program);
            if($result['IsSuccess']){

                if('UpdateOrder' == $this->method){
                    Redis::setex(self::LANXILI_ORDER_RELATION . ':' . $this->program['OriginalId'], 2592000, $result['Tag']);
                }

                Log::channel('order_log')->info($this->method . ' : ' . json_encode($this->program) . ',---- :' .json_encode($result) . PHP_EOL);


            }else{

                Log::channel('order_log')->info($this->method . ' : ' . json_encode($this->program) . ',---- :' .json_encode($result) . PHP_EOL);
                echo date('Y-m-d H:i:s') . ' 错误信息 : ' . $result['FailMessage'] . PHP_EOL;
            }

        } catch (\Exception $e) {

            throw new \Exception($e->getMessage(), $e->getCode(), $e);
        }

        echo date('Y-m-d H:i:s') . ": 结束 ===========" . PHP_EOL;

    }



















    protected function postJson($endpoint, $params = [], $headers = [])
    {
        return $this->request('post', $endpoint, [
            'headers' => $headers,
            'json' => $params,
        ]);
    }

    protected function request($method, $endpoint, $options = [])
    {
        return $this->unwrapResponse($this->getHttpClient($this->getBaseOptions())->{$method}($endpoint, $options));
    }

    protected function getHttpClient(array $options = [])
    {
        return new Client($options);
    }

    protected function unwrapResponse(ResponseInterface $response)
    {
        $contentType = $response->getHeaderLine('Content-Type');
        $contents = $response->getBody()->getContents();
        if (false !== stripos($contentType, 'json') || stripos($contentType, 'javascript')) {
            return json_decode($contents, true);
        } elseif (false !== stripos($contentType, 'xml')) {
            return json_decode(json_encode(simplexml_load_string($contents)), true);
        }

        return $contents;
    }

    protected function getBaseOptions()
    {
        $options = [
            'base_uri' => method_exists($this, 'getBaseUri') ? $this->getBaseUri() : '',
            'timeout' => method_exists($this, 'getTimeout') ? $this->getTimeout() : 300.0,
        ];

        return $options;
    }



}

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值