简单使用lumen队列

需求:在项目中,需要采集文章并把文章中的图片本地化/更新到oss中。如果按照以往的方式,直来直往地采集并替换到图片,耗时约20-50秒。因此,可以考虑实用lumen中的队列任务Queue.

代码片段

Controller 写入数据库后,再执行队列任务
 if (is_array($return)){
            TabHeadlineArticle::insert($return); //采集的文章入表
            $res    =   ['code'=>200,'count'=>count($return),'msg'=>'采集成功']; //返回格式
            dispatch(new PhotoJob($return));//执行队列任务
            return $res;返回结果
        }
队列文件位置在/app/jobs
PhotoJobs

 public function __construct($return)
    {
        $data   =   DB::table('tab_tool')->where('name','oss_storage')->get();
        $data   =   json_decode($data,true);
        $config     =   json_decode($data[0]['config'],true);;
        $this->accessKeyId = $config['accesskeyid'];
        $this->accessKeySecret = $config['accesskeysecr'];
        $this->endpoint = $config['domain'];
        $this->bucket= $config['bucket'];
        $this->return   =   $return;
    }

 public function handle()
    {

        foreach($this->return as $key =>$val) {
            $val['article_content'] = $this->replaceimg($val['article_content']);
            DB::table('tab_headline_article')->where('md5',$val['md5'])->update(['article_content'=>$val['article_content']]);
        }
    }

最后,在命令行中执行

php artisan queue:work 即可

本地化的代码参见之前文章就有.以此记录

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容