菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
3136
0

Laravel 扩展包,让你 Functions 在后台执行,基于 MySQL 的后台任务管理工具

原创
05/13 14:22
阅读数 2528

分享链接:https://github.com/Chester-Hee/background-...

Background-Tasks

让你的 Functions 支持后台执行

Laravel 扩展包,基于 MySQL 后台任务管理工具,使用 Laravel Event Console 执行任务,负责管理和监控任务代码执行状态,让后台任务执行更简单。

demo

Composer 安装

仓库源:https://packagist.org/packages/chester/bac...

composer require chester/background-mission

MySQL Migration

创建数据表:

background-tasks/src/2018_11_14_104840_test.php

配置 Provider:

 Chester\BackgroundMission\Providers\MissionProvider::class

命令测试

添加任务:

$ php artisan mission:test-add-task

查看任务列表:

$ php artisan mission:records
+------------------+--------------------------+--------+---------+--------+-------------------+
| unique_id        | method                   | type   | state   | params | content           |
+------------------+--------------------------+--------+---------+--------+-------------------+
| gngkiytndfratiho | helloWorldAfter15Seconds | system | success | []     | after 15 seconds. |
+------------------+--------------------------+--------+---------+--------+-------------------+

自定义 Logic Functions

新建类:

<?php
namespace App;

use Chester\BackgroundMission\Logic;

class TestLogic extends Logic
{
    public function myTest()
    {
        sleep(20);
        return $this->response(1, 'my test');
    }
}

添加配置项 config/const.php:

'background_logic' => '\App\TestLogic'

测试任务提交:

Route::get('bg-test', function () {
    app('chester.bg.queue')->push(['method' => 'myTest'])->runTask();
});

查看任务列表:

myTest 这个方法正在执行中。

$ php artisan mission:records      
+------------------+--------+--------+-----------+--------+---------+
| unique_id        | method | type   | state     | params | content |
+------------------+--------+--------+-----------+--------+---------+
| rtrrvdedljvqdvcv | myTest | system | executing | []     |         |
+------------------+--------+--------+-----------+--------+---------+

20秒后,再次查看任务列表:

myTest 执行完成,并输出 'my test'

$ php artisan mission:records      
+------------------+--------+--------+-----------+--------+---------+
| unique_id        | method | type   | state     | params | content |
+------------------+--------+--------+-----------+--------+---------+
| tjyfakjpdghjgvba | myTest | system | success   | []     | my test |
+------------------+--------+--------+-----------+--------+---------+

发表评论

0/200
3136 点赞
0 评论
收藏
为你推荐 换一批