2017 © Pedro Peláez
 

library task-balancer

lightweight and powerful task load balancing.

image

toplan/task-balancer

lightweight and powerful task load balancing.

  • Monday, May 15, 2017
  • by toplan
  • Repository
  • 2 Watchers
  • 36 Stars
  • 43,016 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 20 Versions
  • 11 % Grown

The README.md

Intro

Latest Stable Version Total Downloads, (*1)

Lightweight and powerful task load balancing., (*2)

like the nginx load balancing :smile:, (*3)

Features

  • Support multiple drives for every task.
  • Automatically choose a driver to execute task by drivers' weight value.
  • Support multiple backup drivers.
  • Task lifecycle and hooks system.

Install

composer require toplan/task-balancer:~0.5

Usage

//define a task
Balancer::task('task1', function($task){
    //define a driver for current task like this:
    $task->driver('driver_1 100 backup', function ($driver, $data) {
        //do something here
        ...
        //set whether run success/failure at last
        if ($success) {
            $driver->success();
        } else {
            $driver->failure();
        }
        //return some data you need
        return 'some data you need';
    });

    //or like this:
    $task->driver('driver_2', 90, function ($driver, $data) {
        //...same as above..
    })->data(['this is data 2']);

    //or like this:
    $task->driver('driver_3')
    ->weight(0)->backUp()
    ->data(['this is data 3'])
    ->work(function ($driver, $data) {
        //...same as above..
    });
});

//run the task
$result = Balancer::run('task1');

The $result structure:, (*4)

[
    'success' => true,
    'time' => [
        'started_at' => timestamp,
        'finished_at' => timestamp
    ],
    'logs' => [
        '0' => [
            'driver' => 'driver_1',
            'success' => false,
            'time' => [
                'started_at' => timestamp,
                'finished_at' => timestamp
            ],
            'result' => 'some data you need'
        ],
        ...
    ]
]

API

Balancer

Balancer::task($name[, $data][, Closure $ready]);

Create a task instance, and return it. The closure $ready immediately called with argument $task., (*5)

Balancer::task('taskName', $data, function($task){
    //task's ready work, such as create drivers.
});

$data will store in the task instance., (*6)

Balancer::run($name[, array $options])

Run the task by name, and return the result data., (*7)

The keys of $options: - data - driver, (*8)

Task

name($name)

set the name of task., (*9)

data($data)

Set the data of task., (*10)

driver($config[, $weight][, 'backup'], Closure $work)

Create a driver for the task. The closure $work will been called with arguments $driver and $data., (*11)

Expected $weight to be a integer, default 1., (*12)

$task->driver('driverName 80 backup', function($driver, $data){
    //driver's job content.
});

hasDriver($name)

Whether has the specified driver., (*13)

getDriver($name)

Get driver by name., (*14)

removeDriver($name)

Remove driver from drivers' pool by name., (*15)

Driver

weight($weight)

Set the weight value of driver., (*16)

backup($is)

Set whether backup driver., (*17)

Expected $is to be boolean, default true., (*18)

data($data)

Set the data of driver., (*19)

$data will store in driver instance., (*20)

work(Closure $work);

Set the job content of driver., (*21)

$data equals to $driver->getData(), (*22)

reset($config[, $weight][, 'backup'], Closure $work)

Reset driver's weight value, job content and reset whether backup., (*23)

destroy()

Remove the driver from task which belongs to., (*24)

failure()

Set the driver running failure., (*25)

success()

Set the driver run successfully., (*26)

getDriverData()

Get the data which store in driver instance., (*27)

getTaskData()

Get the data which store in task instance., (*28)

Lifecycle & Hooks

Support multiple handlers for every hooks!, (*29)

Hooks

Hook name handler arguments influence of the last handler's return value
beforeCreateDriver $task, $props, $index, &$handlers, $prevReturn if an array will been merged into original props
afterCreateDriver $task, $driver, $index, &$handlers, $prevReturn -
beforeRun $task, $index, &$handlers, $prevReturn if false will stop run task and return false
beforeDriverRun $task, $driver, $index, &$handlers, $prevReturn if false will stop to use current driver and try to use next backup driver
afterDriverRun $task, $driverResult, $index, &$handlers, $prevReturn -
afterRun $task, $taskResult, $index, &$handlers, $prevReturn if not boolean will override result value

Usage

  • $task->hook($hookName, $handler, $override), (*30)

  • $task->beforeCreateDriver($handler, $override), (*31)

  • $task->afterCreateDriver($handler, $override), (*32)

  • $task->beforeRun($handler, $override), (*33)

  • $task->beforeDriverRun($handler, $override), (*34)

  • $task->afterDriverRun($handler, $override), (*35)

  • $task->afterRun($handler, $override), (*36)

$override default false., (*37)

//example
$task->beforeRun(function($task, $index, $handlers, $prevReturn){
    //what is $prevReturn?
    echo $prevReturn == null; //true
    //what is $index?
    echo $index == 0; //true
    //what is $handlers?
    echo count($handlers); //2
    //do something..
    return 'beforeRun_1';
}, false);

$task->beforeRun(function($task, $index, $handlers, $prevReturn){
    //what is $prevReturn?
    echo $prevReturn == 'beforeRun_1'; //true
    //what is $index?
    echo $index == 1; //true
    //what is $handlers?
    echo count($handlers); //2
    //do other something..
}, false);

Dependents

License

MIT, (*38)

The Versions

15/05 2017

dev-master

9999999-dev

lightweight and powerful task load balancing.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

15/05 2017

0.5.0

0.5.0.0

lightweight and powerful task load balancing.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance

15/05 2017

0.5.0-beta.1

0.5.0.0-beta1

lightweight and powerful task load balancing.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance

15/05 2017

dev-dev

dev-dev

lightweight and powerful task load balancing.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

14/04 2016

0.4.2

0.4.2.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

12/04 2016

0.4.1

0.4.1.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

02/03 2016

0.4.0

0.4.0.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

31/12 2015

0.3.0

0.3.0.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

14/12 2015

0.2.3

0.2.3.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

10/12 2015

0.2.2

0.2.2.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

08/12 2015

0.2.1

0.2.1.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

07/12 2015

0.2.0

0.2.0.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

06/12 2015

0.1.5

0.1.5.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

05/12 2015

0.1.4

0.1.4.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

30/11 2015

0.1.3

0.1.3.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

20/11 2015

0.1.2

0.1.2.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

18/11 2015

0.1.1

0.1.1.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

16/11 2015

0.1.0

0.1.0.0

lightweight and powerful task load balancing for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task load balancing balance balancer

14/11 2015

0.0.3

0.0.3.0

task balancer for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task balance balancer

12/11 2015

0.0.1

0.0.1.0

task balancer for php (like the nginx load balancing)

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar toplan

task balance balancer