From f9b9a0a4407665c3a95c4cb3cd1780ba016fc9fd Mon Sep 17 00:00:00 2001 From: toplan Date: Tue, 8 Dec 2015 10:30:48 +0800 Subject: [PATCH] upgrade hook handler`s arguments --- README.md | 45 +++++++++++++++++++++++---------------- composer.json | 2 +- src/TaskBalancer/Task.php | 9 ++++---- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index ca8fde0..1e8bbef 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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' ], ... ] @@ -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 @@ -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); ``` diff --git a/composer.json b/composer.json index 55c4880..77b2d2e 100644 --- a/composer.json +++ b/composer.json @@ -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": [ { diff --git a/src/TaskBalancer/Task.php b/src/TaskBalancer/Task.php index f570281..0cfd4ab 100644 --- a/src/TaskBalancer/Task.php +++ b/src/TaskBalancer/Task.php @@ -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) {