Giter Site home page Giter Site logo

easytask's Introduction

EasyTask

基于swoole的任务队列

可多进程并发执行, 主进程监听多个任务进程, 任务进程挂掉会被主进程自动拉起

install

使用composer: composer require ruziyi/easy-task:dev-master

手动引入: include include 'Loader.php'

example

创建task, 把要实现的业务逻辑放到run方法里

class EchoTask extends EasyTask\Task
{
    private $msg;

    public function __construct($msg)
    {
        parent::__construct();
        $this->msg = $msg;
    }

    public function run()
    {
        echo $this->msg;
    }
}

触发任务

$task = (new EasyTask\EchoTask("hehe\n"))->after(1000);
(new EasyTask\queue\RedisQueue)->putTask($task);

创建任务消费进程文件server.php

$taskProcess = new EasyTask\TaskProcess();
$taskProcess->run();

启动任务消费进程

php server.php

例子

//立即执行
$task = (new EasyTask\EchoTask("hehe\n"));
//3分钟后执行
$task = (new EasyTask\EchoTask("hehe\n"))->at(strtotime('+3 minute'));
//延迟1000ms立即执行
$task = (new EasyTask\EchoTask("hehe\n"))->after(1000);
//每1000ms执行一次, 共执行5次。不设置次数, 则一直重复执行
$task = (new EasyTask\EchoTask("hehe\n"))->every(1000, 5);
//1000ms后, 每1000ms执行一次, 共执行5次。不设置次数, 则一直重复执行
$task = (new EasyTask\EchoTask("hehe\n"))->after(1000)->every(1000, 5);

(new EasyTask\queue\RedisQueue)->putTask($task);

easytask's People

Watchers

James Cloos avatar  avatar

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.