登录  /  注册
首页 > php框架 > Laravel > 正文

后台执行超长时间任务解决方案

藏色散人
发布: 2019-09-23 09:24:07
转载
4105人浏览过

解决的问题:

● 耗时较长

● 各端无法调取相关任务进度进行反馈

● 自定义任务过后反馈结果

● 请教下,Laravel 如何让程序在后台执行超长时间的代码?

流程简述

● 使用异步队列执行相关任务

● 使用助手方法进行任务 / 进度创建

● 通过暴露接口反馈相关进度

助手类源码如下

<?php
// +----------------------------------------------------------------------
// | Do what we can do
// +----------------------------------------------------------------------
// | Date  : 2019/9/11 - 9:25 AM
// +----------------------------------------------------------------------
// | Author: seebyyu <seebyyu@gmail.com> :)
// +----------------------------------------------------------------------
namespace App\Lib\Support;
trait MissionFrom
{
    /**
     * 标记前缀 模块名称#业务模块#板块标记
     *
     * @var string
     */
    public $prefix = &#39;school:task:default&#39;;
    /**
     * 任务详情
     * @var array
     */
    public $original = [];
    /**
     * Redis 链接
     *
     * The Redis factory implementation.
     *
     * @var \Illuminate\Redis\Connections\Connection
     */
    protected $redis;
    /**
     * 任务存在有效期
     *
     * @var int
     */
    protected $seconds = 600;
    /**
     * 创建任务
     *
     * @param string $sheet
     * @param int $len 总长度
     * @return string
     */
    public function createTask($sheet = &#39;&#39;, $len = 100)
    {
        $sheet = $sheet ?: $this->sheet();
        $detail = [
            //  开始时间
            &#39;begin&#39; => time(),
            //  标记号
            &#39;sheet&#39; => $sheet,
            //  总长度
            &#39;total_len&#39; => $len,
            //  当前长度
            &#39;schedule&#39; => 0
        ];
        //  主体信息
        $this->connect()->setex($this->prefix. &#39;:&#39;. $sheet, $this->seconds, serialize($detail));
        //  初始化任务进度
        $this->connect()->setex($this->prefix. &#39;:schedule:&#39;. $sheet, $this->seconds, 1);
        return $sheet;
    }
    /**
     * 设置任务内容
     *
     * @param $sheet
     * @param $value
     * @return MissionFrom
     */
    public function setTaskContent($sheet, $value)
    {
        if( $this->connect()->exists($this->prefix. &#39;:&#39;. $sheet)){
            $this->connect()->setex($this->prefix. &#39;:content:&#39;. $sheet, $this->seconds, serialize($value));
        }
        return $this;
    }
    /**
     * 获取任务内容
     *
     * @param $sheet
     * @return MissionFrom
     */
    public function getTaskContent($sheet)
    {
        return empty($data = $this->connect()->get($this->prefix. &#39;:content:&#39;. $sheet)) ? null : unserialize($data);
    }
    /**
     * 设置任务前缀
     *
     * @param string $prefix
     * @return $this
     */
    public function setPrefix($prefix = &#39;&#39;)
    {
        $this->prefix = &#39;school:task:&#39;. ($prefix ?: &#39;default&#39;);
        return $this;
    }
    /**
     * 任务详情
     *
     * @param string $sheet
     * @return array
     */
    public function taskDetail($sheet = &#39;&#39;)
    {
        $detail = $this->connect()->get($key = ($this->prefix. &#39;:&#39;. $sheet));
        if( !empty($detail)){
            $this->original = array_merge( unserialize($detail), [
                &#39;schedule&#39; => (int)$this->getSchedule($sheet),
                &#39;content&#39; => $this->getTaskContent($sheet)
            ]);
        }
        return (array) $this->original;
    }
    /**
     * 进度递增
     *
     * @param string $sheet
     * @return int
     */
    public function increments($sheet = &#39;&#39;)
    {
        $inc = 0;
        if( !empty($detail = $this->taskDetail($sheet)) &&
            $detail[&#39;schedule&#39;] < $detail[&#39;total_len&#39;]){
            $inc = $this->connect()->incr($this->prefix. &#39;:schedule:&#39;. $sheet);
        }
        return $detail[&#39;schedule&#39;] ?? $inc;
    }
    /**
     * 获取任务进度
     *
     * @param string $sheet
     * @return string
     */
    public function getSchedule($sheet = &#39;&#39;)
    {
        return $this->connect()->exists($key = ($this->prefix. &#39;:schedule:&#39;. $sheet)) ? $this->connect()->get($key) : 0;
    }
    /**
     * 生成任务单号
     */
    private static function sheet()
    {
        return md5(\Hash::make(date(&#39;YmdHis&#39;)));
    }
    /**
     * 所有任务进度
     *
     * @return array
     */
    public function taskAll()
    {
        $task_group_list = [];
        //  分组
        foreach( (array)$this->connect()->keys(&#39;school:task:*&#39;) as $task) {
            if( count($task_item = explode(&#39;:&#39;, $task)) == 4){
                list($model, $model_name, $business, $key) = $task_item;
                $task_group_list[$business][] = $this->setPrefix($business)->taskDetail($key);
            }
        }
        return $task_group_list;
    }
    /**
     * @return \Illuminate\Foundation\Application|mixed
     */
    public function connect()
    {
        return app(&#39;redis.connection&#39;);
    }
}
登录后复制

调用过程如下

<?php
namespace App\Jobs;
use App\Lib\Support\MissionFrom;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
/**
 * Excel 导入
 *
 * Class importExcel
 * @package App\Jobs
 */
class importExcel implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MissionFrom;
    /**
     * 任务运行的超时时间。
     *
     * @var int
     */
    public $timeout = 300;
    /**
     * @var string
     */
    public $sheet;
    /**
     * importExcel constructor.
     * @param $sheet
     */
    public function __construct($sheet = &#39;&#39;)
    {
        $this->sheet = $sheet;
    }
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //  自定义业务前缀
        $prefix = &#39;export_students&#39;;
        //  创建任务进度
        $this->sheet = $this->setPrefix($prefix)->createTask($this->sheet, 20);
        //  开始执行任务
        echo &#39;任务开始:&#39;. $this->sheet. "\n";
        for ($i = 1; $i <= 20; $i++){
            //  延时模拟长时间任务
            sleep(rand(1, 2));
            //  进度 +1
            echo &#39;任务进度:&#39;. ($this->setPrefix($prefix)->increments($this->sheet)). "\n";
        }
        //  追加结果 任何类型
        $this->setPrefix($prefix)->setTaskContent($this->sheet, [
            &#39;url&#39; => &#39;http://www.baidu.com&#39;
        ]);
    }
}
登录后复制

控制器部分

....
    /**
     * 学校pc端后台任务进度列表
     *
     * @return array
     */
    public function duties()
    {
        if( empty($key = request(&#39;key&#39;))){
            $key = md5(\Hash::make(date(&#39;YmdHis&#39;)));
            //  创建任务
            $this->dispatch(new importExcel($key));
            return $key;
        }else{
            //  查询单条任务信息
            //  $this->setPrefix(&#39;export_students&#39;)->taskDetail($key);
            return success([&#39;data&#39; => array_merge([
                //  导出每餐记录列表
                &#39;meal_records&#39; => [],
                //  每日记录列表
                &#39;daily_records&#39; => [],
                //  其他记录列表
                &#39;other_records&#39; => [],
                //  照片库
                &#39;photo_gallery&#39; => [],
                //  采购计划
                &#39;purchasing_plan&#39; => [],
                //  凭证记录
                &#39;voucher_records&#39; => [],
                //  食材库
                &#39;ingredient_records&#39; => [],
                //  导入学生
                &#39;import_students&#39; => [],
                //  导出学生
                &#39;export_students&#39; => []
            ], $this->taskAll())]);
        }
    }
    ....
登录后复制

达到的效果

c9646803419fe44d5e37122b9937f02.png

注意事项

QUEUE_DRIVER=sync 变更为 redis

开发阶段强烈建议把 horizon 这玩意儿装上,Laravel 自带的报错异常我实在无力吐槽,不方便排错.

队列排错参考:

laravel/430639.html" target="_blank">Laravel 队列:如何查看队列报错信息?

最后

● 代码上面的业务完全根据我自身项目编写,直接照搬 可能会引起不兼容。

● 分享 更多的是一种解决思路,希望能帮到后面的小伙伴。

● 如果对代码 有什么优化思路 或者 建议 也可以探讨下。

更多Laravel相关技术文章,请访问Laravel框架入门教程栏目进行学习!

以上就是后台执行超长时间任务解决方案的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:learnku网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号