• laravel 简单的延迟分发任务队列


    修改配置文件.env 为redis储存

    QUEUE_CONNECTION=redis

    运行队列处理

    php artisan queue:work

    创建延迟队列任务

    php artisan make:job TestOrder
    <?php
    
    namespace App\Jobs;
    
    use App\Models\Order;
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Queue\SerializesModels;
    
    class TestOrder implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        protected $order_id;
    
        /**
         * Create a new job instance.
         *
         * @return void
         */
        public function __construct($order_id, $delay)
        {
            $this->order_id = $order_id;
            // 核心配置:设置延迟的时间,delay()方法的参数代表多少秒之后执行
            $this->delay($delay);
        }
    
        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle()
        {
            $order = Order::where('id', $this->order_id)->first();
            if ($order->closed == 1){
                Order::where('id', $this->order_id)->update(['closed' => 3]);
            }else{
                return;
            }
        }
    }

    调用上边的延迟任务

    dispatch(new TestOrder(1, 5));
    dispatch(new TestOrder(2, 6));
    dispatch(new TestOrder(3, 30));
  • 相关阅读:
    数学—快速幂
    离散化
    造树计划——线段树
    Python map()函数
    python的discard和remove方法
    C++学习笔记之NULL vs nullptr
    哈姆雷特单词的排名
    读书笔记—《网络是怎么连接的》4.11
    滑动窗口—UVA11572 唯一的雪花 Unique Snowflakes
    javascript基础语法1.0
  • 原文地址:https://www.cnblogs.com/zjj1990/p/16355037.html
Copyright © 2020-2023  润新知