Skip to content

Commit

Permalink
upgrade hook handler`s arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
toplan committed Dec 8, 2015
1 parent 4385ab2 commit f9b9a0a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
45 changes: 27 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ lightweight and powerful task load balancing for php
- 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.
- Task lifecycle and hooks system.

# Install

```php
composer require 'toplan/task-balancer:~0.2.0'
composer require 'toplan/task-balancer:~0.2.1'
```

# Usage
Expand Down Expand Up @@ -59,13 +59,18 @@ The `$result` structure:
[
'success' => true,
'time' => [
'started_at' => '',
'finished_at' => ''
'started_at' => timestamp,
'finished_at' => timestamp
],
'logs' => [
'0' => [
'driver' => 'Luosimao',
...
'driver' => 'driver_1',
'success' => false,
'time' => [
'started_at' => timestamp,
'finished_at' => timestamp
],
'result' => 'some data here'
],
...
]
Expand Down Expand Up @@ -168,12 +173,12 @@ get data value of task instance.

| Hook name | handler arguments | influence of the last handler`s return value |
| --------- | :----------------: | :-----: |
| beforeCreateDriver | $task, $preReturn, $index | no effect |
| afterCreateDriver | $task, $preReturn, $index | no effect |
| beforeRun | $task, $preReturn, $index | if `false` will stop run task and return `false` |
| beforeDriverRun | $task, $preReturn, $index | no effect |
| afterDriverRun | $task, $preReturn, $index | no effect |
| afterRun | $task, $results, $preReturn, $index | if not boolean will override result value |
| beforeCreateDriver | $task, $preReturn, $index, $count | no effect |
| afterCreateDriver | $task, $preReturn, $index, $count | no effect |
| beforeRun | $task, $preReturn, $index, $count | if `false` will stop run task and return `false` |
| beforeDriverRun | $task, $preReturn, $index, $count | no effect |
| afterDriverRun | $task, $preReturn, $index, $count | no effect |
| afterRun | $task, $taskResult, $preReturn, $index, $count | if not boolean will override result value |

###Use Hooks

Expand All @@ -196,20 +201,24 @@ get data value of task instance.

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

$task->beforeRun(function($task, $preReturn, $index ){
$task->beforeRun(function($task, $preReturn, $index, $count){
//what is $preReturn?
echo $preReturn == 'beforeRun_1'; //true
$preReturn == 'beforeRun_1'; //true
//what is $index?
echo $index == 1; //true
$index == 1; //true
//what is $count?
echo $count; //2
//do other something..
}, false);
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "toplan/task-balancer",
"description": "lightweight and powerful task load balancing for php (like the nginx load balancing)",
"license": "MIT",
"version": "0.2.0",
"version": "0.2.1",
"keywords": ["task", "balance", "load balancing", "balancer"],
"authors": [
{
Expand Down
9 changes: 5 additions & 4 deletions src/TaskBalancer/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,11 @@ protected function callHookHandler($hookName, $data = null)
if (array_key_exists($hookName, $this->handlers)) {
$handlers = $this->handlers[$hookName] ?: [];
$result = null;
foreach ($handlers as $key => $handler) {
$handlerArgs = $data == null ?
[$this, $result, $key]:
[$this, $data, $result, $key];
$count = count($handlers);
foreach ($handlers as $index => $handler) {
$handlerArgs = $data === null ?
[$this, $result, $index, $count]:
[$this, $data, $result, $index, $count];
$result = call_user_func_array($handler, $handlerArgs);
}
if ($result === null) {
Expand Down

0 comments on commit f9b9a0a

Please sign in to comment.