Skip to content

Commit f9b9a0a

Browse files
committed
upgrade hook handler`s arguments
1 parent 4385ab2 commit f9b9a0a

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

README.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ lightweight and powerful task load balancing for php
88
- Support multiple drives for every task.
99
- Automatically choose a driver to execute task by drivers` weight value.
1010
- Support multiple backup drivers.
11-
- Task lifecycle and hooks.
11+
- Task lifecycle and hooks system.
1212

1313
# Install
1414

1515
```php
16-
composer require 'toplan/task-balancer:~0.2.0'
16+
composer require 'toplan/task-balancer:~0.2.1'
1717
```
1818

1919
# Usage
@@ -59,13 +59,18 @@ The `$result` structure:
5959
[
6060
'success' => true,
6161
'time' => [
62-
'started_at' => '',
63-
'finished_at' => ''
62+
'started_at' => timestamp,
63+
'finished_at' => timestamp
6464
],
6565
'logs' => [
6666
'0' => [
67-
'driver' => 'Luosimao',
68-
...
67+
'driver' => 'driver_1',
68+
'success' => false,
69+
'time' => [
70+
'started_at' => timestamp,
71+
'finished_at' => timestamp
72+
],
73+
'result' => 'some data here'
6974
],
7075
...
7176
]
@@ -168,12 +173,12 @@ get data value of task instance.
168173

169174
| Hook name | handler arguments | influence of the last handler`s return value |
170175
| --------- | :----------------: | :-----: |
171-
| beforeCreateDriver | $task, $preReturn, $index | no effect |
172-
| afterCreateDriver | $task, $preReturn, $index | no effect |
173-
| beforeRun | $task, $preReturn, $index | if `false` will stop run task and return `false` |
174-
| beforeDriverRun | $task, $preReturn, $index | no effect |
175-
| afterDriverRun | $task, $preReturn, $index | no effect |
176-
| afterRun | $task, $results, $preReturn, $index | if not boolean will override result value |
176+
| beforeCreateDriver | $task, $preReturn, $index, $count | no effect |
177+
| afterCreateDriver | $task, $preReturn, $index, $count | no effect |
178+
| beforeRun | $task, $preReturn, $index, $count | if `false` will stop run task and return `false` |
179+
| beforeDriverRun | $task, $preReturn, $index, $count | no effect |
180+
| afterDriverRun | $task, $preReturn, $index, $count | no effect |
181+
| afterRun | $task, $taskResult, $preReturn, $index, $count | if not boolean will override result value |
177182

178183
###Use Hooks
179184

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

197202
```php
198203
//example
199-
$task->beforeRun(function($task, $preReturn, $index ){
204+
$task->beforeRun(function($task, $preReturn, $index, $count){
200205
//what is $preReturn?
201-
echo $preReturn == null; //true
206+
$preReturn == null; //true
202207
//what is $index?
203-
echo $index == 0; //true
208+
$index == 0; //true
209+
//what is $count?
210+
echo $count; //2
204211
//do something..
205212
return 'beforeRun_1';
206213
}, false);
207214

208-
$task->beforeRun(function($task, $preReturn, $index ){
215+
$task->beforeRun(function($task, $preReturn, $index, $count){
209216
//what is $preReturn?
210-
echo $preReturn == 'beforeRun_1'; //true
217+
$preReturn == 'beforeRun_1'; //true
211218
//what is $index?
212-
echo $index == 1; //true
219+
$index == 1; //true
220+
//what is $count?
221+
echo $count; //2
213222
//do other something..
214223
}, false);
215224
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "toplan/task-balancer",
33
"description": "lightweight and powerful task load balancing for php (like the nginx load balancing)",
44
"license": "MIT",
5-
"version": "0.2.0",
5+
"version": "0.2.1",
66
"keywords": ["task", "balance", "load balancing", "balancer"],
77
"authors": [
88
{

src/TaskBalancer/Task.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,11 @@ protected function callHookHandler($hookName, $data = null)
524524
if (array_key_exists($hookName, $this->handlers)) {
525525
$handlers = $this->handlers[$hookName] ?: [];
526526
$result = null;
527-
foreach ($handlers as $key => $handler) {
528-
$handlerArgs = $data == null ?
529-
[$this, $result, $key]:
530-
[$this, $data, $result, $key];
527+
$count = count($handlers);
528+
foreach ($handlers as $index => $handler) {
529+
$handlerArgs = $data === null ?
530+
[$this, $result, $index, $count]:
531+
[$this, $data, $result, $index, $count];
531532
$result = call_user_func_array($handler, $handlerArgs);
532533
}
533534
if ($result === null) {

0 commit comments

Comments
 (0)