• laravel job 队列


    1.数据库建表

    php artisan queue:table<span>				</span>//队列任务表
    php artisan queue:failed-table<span>			</span>//任务执行失败表
    php artisan migrate



    2.创建job类

    <?php
    
    namespace AppJobs;
    
    use AppServicesTestService;
    use IlluminateSupportFacadesLog;
    
    class CommentInfoJob extends Job
    {
    
        public $commentService;
        public $user_id;
        public $comment_id;
        /**
         * Create a new job instance.
         *
         * @return void
         */
        public function __construct($user_id,$comment_id)
        {
            $this->user_id = $user_id;
            $this->comment_id = $comment_id;
        }
    
        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle()
        {
    
          
            (new TestService())->testsssss($this->user_id,$this->comment_id);
        }
    }
    

      

    3.在job类中,实现逻辑

    job主要包括三个函数

    • 构造函数 可选,用来传参
    • handle() 必选,实现队列任务逻辑
    • failed() 可选,当任务失败时执行

    4.分发队列任务

    dispatch(new CommentInfoJob($params['user_id'], $params['comment_id']));

    5.开启队列进程,执行队列任务
    php artisan queue:work

    这种方式不能关闭teminal,比较不方便。所以一般使用Supervisor。


    以上就可以完成一个简单的队列任务。

    在laravel中有一个很关键的点:配置为database驱动。

    在/config/queue.php中,需要将connections设置为database。而这一配置是从.env文件中获取的

    将queue_driver配置为database。

  • 相关阅读:
    Python 常用内置函数
    Java Graphics 2D绘制图片 在Liunx上乱码
    LInux Centos7 重装yum
    Spring Boot 异步调用
    Linux 清除N天前的 日期文件夹(yyyy-MM-dd)
    Python 2.75升级3.6.3
    Linux 移除python Error: Trying to remove “yum”, which is protected
    Java Future 使用场景
    CF446D DZY Loves Games
    三 lambda表达式有什么用
  • 原文地址:https://www.cnblogs.com/brady-wang/p/12726705.html
Copyright © 2020-2023  润新知