Git Product home page Git Product logo

background-tasks's Introduction

Background-Tasks

让你的 PHP 代码异步执行起来

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

任务选项

参数名称 是否必填 描述
method 任务执行方法 Logic
params 执行 Method 所需要的参数
type 任务分类,自定义字符串
creator 任务提交者,自定义字符串

demo

Composer 安装

仓库源:https://packagist.org/packages/chester/background-mission

composer require chester/background-mission

MySQL Migration

创建数据表:不创建数据表是无法正常提交任务的

background-tasks/src/2018_11_14_104840_test.php

配置 Provider:

 Chester\BackgroundMission\Providers\MissionProvider::class

测试示例

假设后台需要导出1000万条数据,并下载这个文件,下面模拟导出 csv

// hello world. 测试10万条记录后台导出 csv 文件
public function helloWorld()
{
    $filename = storage_path('app') . '/' . date('YmdHis') . ".csv";
    $handle = fopen($filename, 'w');
    fwrite($handle, chr(0xEF) . chr(0xBB) . chr(0xBF));

    $cur_page = 0;
    fputcsv($handle, ['num', 'datetime']);
    do {
        $cur_page++;
        fputcsv($handle, [$cur_page, date('Y-m-d H:i:s')]);
        // 模拟10万条记录
        if ($cur_page >= 10000000) {
            break;
        }
    } while (true);
    return $this->response(1, "output target file dir: {$filename}");
}

运行测试示例:

$ php artisan mission:add

查看执行状态:

看到任务已经进入后台执行,并显示 Executing 状态。

➜  laravel git:(5.5) ✗ php artisan mission:records
+------------------+------------+--------+-----------+--------+--------------------+
| unique_id        | method     | type   | state     | params | content            |
+------------------+------------+--------+-----------+--------+--------------------+
| nauaxnrfnuflwcqn | helloWorld | system | executing | []     |                    |
+------------------+------------+--------+-----------+--------+--------------------+

经过一段时间后台,可以看到任务已经完成,并且输出 Content 包含文件 Csv 所在的路径,同时任务为 Success 状态。

➜  laravel git:(5.5) ✗ php artisan mission:records
+------------------+------------+--------+---------+--------+---------------------------------------------------------------------+
| unique_id        | method     | type   | state   | params | content                                                             | 
+------------------+------------+--------+---------+--------+---------------------------------------------------------------------+
| nauaxnrfnuflwcqn | helloWorld | system | success | []     | output target file dir: /www/laravel/storage/app/20181117151438.csv |
+------------------+------------+--------+---------+--------+---------------------------------------------------------------------+

自定义 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']);
});

background-tasks's People

Contributors

helingfeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

royalwang

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.