diff --git a/README.md b/README.md index 2f02de9..f8f58b2 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ lightweight and powerful task load balancing for php # Install ```php - composer require 'toplan/task-balancer:~0.1.5' + composer require 'toplan/task-balancer:~0.2.0' ``` # Usage @@ -63,7 +63,8 @@ The `$result` structure: 'finished_at' => '' ], 'logs' => [ - 'driver_1' => [ + '0' => [ + 'driver' => 'Luosimao', ... ], ... @@ -163,27 +164,48 @@ get data value of task instance. | Hook name | handler arguments | handler return value | | --------- | :----------------: | :-----: | -| beforeCreateDriver | $task | no effect | -| afterCreateDriver | $task | no effect | -| beforeRun | $task | if `false` will stop run task and return `false` | -| beforeDriverRun | $task | no effect | -| afterDriverRun | $task | no effect | -| afterRun | $task, $results | override run task`s results data | +| 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 | -### $task->hook($hookName, $handler) +### $task->hook($hookName, $handler, $override) -### $task->beforeCreateDriver($handler) +### $task->beforeCreateDriver($handler, $override) -### $task->afterCreateDriver($handler) +### $task->afterCreateDriver($handler, $override) -### $task->beforeRun($handler) +### $task->beforeRun($handler, $override) -### $task->beforeDriverRun($handler) +### $task->beforeDriverRun($handler, $override) -### $task->afterDriverRun($handler) +### $task->afterDriverRun($handler, $override) -### $task->afterRun($handler) +### $task->afterRun($handler, $override) +> `$override` default value is `false`, if `true` will override hooks handler. + +```php +//example +$task->beforeRun(function($task, $preReturn, $index ){ + //what is $preReturn? + echo $preReturn == null; //true + //what is $index? + echo $index == 0; //true + //do something.. + return 'beforeRun_1'; +}, false); + +$task->beforeRun(function($task, $preReturn, $index ){ + //what is $preReturn? + echo $preReturn == 'beforeRun_1'; //true + //what is $index? + echo $index == 1; //true + //do other something.. +}, false); +``` # Todo diff --git a/composer.json b/composer.json index 6068ce5..55c4880 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.1.5", + "version": "0.2.0", "keywords": ["task", "balance", "load balancing", "balancer"], "authors": [ { diff --git a/src/TaskBalancer/Task.php b/src/TaskBalancer/Task.php index 43449fc..f570281 100644 --- a/src/TaskBalancer/Task.php +++ b/src/TaskBalancer/Task.php @@ -486,20 +486,28 @@ public function data($data) * set hook handler * @param $hookName * @param null $handler + * @param boolean $override * * @throws TaskBalancerException */ - public function hook($hookName, $handler = null) + public function hook($hookName, $handler = null, $override = false) { if ($handler && is_callable($handler) && is_string($hookName)) { if (in_array($hookName, self::$hooks)) { - $this->handlers[$hookName] = $handler; + if (!isset($this->handlers[$hookName])) { + $this->handlers[$hookName] = []; + } + if ($override) { + $this->handlers[$hookName] = [$handler]; + } else { + array_push($this->handlers[$hookName], $handler); + } } else { throw new TaskBalancerException("Don`t support the hook [$hookName]"); } } elseif (is_array($hookName)) { foreach ($hookName as $k => $h) { - $this->hook($k, $h); + $this->hook($k, $h, false); } } } @@ -514,8 +522,14 @@ public function hook($hookName, $handler = null) protected function callHookHandler($hookName, $data = null) { if (array_key_exists($hookName, $this->handlers)) { - $handler = $this->handlers[$hookName]; - $result = call_user_func_array($handler, [$this, $data]); + $handlers = $this->handlers[$hookName] ?: []; + $result = null; + foreach ($handlers as $key => $handler) { + $handlerArgs = $data == null ? + [$this, $result, $key]: + [$this, $data, $result, $key]; + $result = call_user_func_array($handler, $handlerArgs); + } if ($result === null) { return true; } @@ -552,7 +566,8 @@ public function __call($name, $args) { if (in_array($name, self::$hooks)) { if (isset($args[0]) && is_callable($args[0])) { - $this->hook($name, $args[0]); + $override = isset($args[1]) ? !!$args[1] : false; + $this->hook($name, $args[0], $override); } else { throw new TaskBalancerException("Please give the method [$name()] a callable argument"); } diff --git a/test/demo2.php b/test/demo2.php new file mode 100644 index 0000000..fa3eaab --- /dev/null +++ b/test/demo2.php @@ -0,0 +1,57 @@ +driver('driver1 10 backup', function($driver, $data){ + $driver->failed(); + print_r('run work! by '.$driver->name.'
'); + }); + + $task->beforeRun(function($task, $preReturn, $index){ + print_r("before run ---$preReturn-----$index
"); + return 11; + }); + + $task->beforeRun(function($task, $preReturn, $index){ + print_r("before run ---$preReturn-----$index
"); + return 22; + }, true); + + $task->beforeRun(function($task, $preReturn, $index){ + print_r("before run ---$preReturn-----$index
"); + }); + + $task->hook('beforeDriverRun', function($task, $preReturn, $index){ + print_r("before driver run ---$preReturn-----$index
"); + return [1]; + }); + + $task->hook('beforeDriverRun', function($task, $preReturn, $index){ + print_r("before driver run ---".implode(',', $preReturn)."-----$index
"); + return [1,2]; + }, true); + + $task->hook('beforeDriverRun', function($task, $preReturn, $index){ + print_r("before driver run ---".implode(',', $preReturn)."-----$index
"); + return [1,2,3]; + }); + + $task->afterRun(function($task, $results){ + print_r("after run --------!
"); + }); +}); + +//run task: +$result = Balancer::run('task1', $data); + +print_r('
'); +print_r($result);