Download the PHP package toplan/task-balancer without Composer
On this page you can find all versions of the php package toplan/task-balancer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download toplan/task-balancer
More information about toplan/task-balancer
Files in toplan/task-balancer
Package task-balancer
Short Description lightweight and powerful task load balancing.
License MIT
Informations about the package task-balancer
Intro
Lightweight and powerful task load balancing.
like the
nginx
load balancing :smile:
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
Usage
The $result
structure:
API
Balancer
Balancer::task($name[, $data][, Closure $ready]);
Create a task instance, and return it.
The closure $ready
immediately called with argument $task
.
$data
will store in the task instance.
Balancer::run($name[, array $options])
Run the task by name, and return the result data.
The keys of $options
:
data
driver
Task
name($name)
set the name of task.
data($data)
Set the data of task.
driver($config[, $weight][, 'backup'], Closure $work)
Create a driver for the task. The closure $work
will been called with arguments $driver
and $data
.
Expected
$weight
to be a integer, default1
.
hasDriver($name)
Whether has the specified driver.
getDriver($name)
Get driver by name.
removeDriver($name)
Remove driver from drivers' pool by name.
Driver
weight($weight)
Set the weight value of driver.
backup($is)
Set whether backup driver.
Expected
$is
to be boolean, defaulttrue
.
data($data)
Set the data of driver.
$data
will store in driver instance.
work(Closure $work);
Set the job content of driver.
$data
equals to$driver->getData()
reset($config[, $weight][, 'backup'], Closure $work)
Reset driver's weight value, job content and reset whether backup.
destroy()
Remove the driver from task which belongs to.
failure()
Set the driver running failure.
success()
Set the driver run successfully.
getDriverData()
Get the data which store in driver instance.
getTaskData()
Get the data which store in task instance.
Lifecycle & Hooks
Support multiple handlers for every hooks!
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)
-
$task->beforeCreateDriver($handler, $override)
-
$task->afterCreateDriver($handler, $override)
-
$task->beforeRun($handler, $override)
-
$task->beforeDriverRun($handler, $override)
-
$task->afterDriverRun($handler, $override)
- $task->afterRun($handler, $override)
$override
defaultfalse
.
Dependents
License
MIT