lightweight and powerful task load balancing.
Lightweight and powerful task load balancing., (*2)
like the
nginx
load balancing :smile:, (*3)
composer require toplan/task-balancer:~0.5
//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' ], ... ] ]
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)
Run the task by name, and return the result data., (*7)
The keys of $options
:
- data
- driver
, (*8)
set the name of task., (*9)
Set the data of task., (*10)
Create a driver for the task. The closure $work
will been called with arguments $driver
and $data
., (*11)
Expected
$weight
to be a integer, default1
., (*12)
$task->driver('driverName 80 backup', function($driver, $data){ //driver's job content. });
Whether has the specified driver., (*13)
Get driver by name., (*14)
Remove driver from drivers' pool by name., (*15)
Set the weight value of driver., (*16)
Set whether backup driver., (*17)
Expected
$is
to be boolean, defaulttrue
., (*18)
Set the data of driver., (*19)
$data
will store in driver instance., (*20)
Set the job content of driver., (*21)
$data
equals to$driver->getData()
, (*22)
Reset driver's weight value, job content and reset whether backup., (*23)
Remove the driver from task which belongs to., (*24)
Set the driver running failure., (*25)
Set the driver run successfully., (*26)
Get the data which store in driver instance., (*27)
Get the data which store in task instance., (*28)
Support multiple handlers for every hooks!, (*29)
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 |
$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
defaultfalse
., (*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);
MIT, (*38)