From 83c115e55e77b3e776c1cb195ecb70ce6b92bf87 Mon Sep 17 00:00:00 2001 From: toplan Date: Tue, 12 Apr 2016 17:53:58 +0800 Subject: [PATCH 1/2] can stop current driver in beforeDriverRun hooks --- README.md | 4 ++-- composer.json | 2 +- src/TaskBalancer/Task.php | 48 +++++++++++++++++++++++++++------------ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ab6390e..795e368 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.4.0' + composer require 'toplan/task-balancer:~0.4.1' ``` # Usage @@ -179,7 +179,7 @@ get data value of task instance. | beforeCreateDriver | $task, $preReturn, $index, $handlers | no effect | | afterCreateDriver | $task, $preReturn, $index, $handlers | no effect | | beforeRun | $task, $preReturn, $index, $handlers | if `false` will stop run task and return `false` | -| beforeDriverRun | $task, $driver, $preReturn, $index, $handlers | no effect | +| beforeDriverRun | $task, $driver, $preReturn, $index, $handlers | if `false` will stop run current driver and try to use next backup driver | | afterDriverRun | $task, $driverResult, $preReturn, $index, $handlers | no effect | | afterRun | $task, $taskResult, $preReturn, $index, $handlers | if not boolean will override result value | diff --git a/composer.json b/composer.json index 26a77bb..8d33f8f 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.4.0", + "version": "0.4.1", "keywords": ["task", "balance", "load balancing", "balancer"], "authors": [ { diff --git a/src/TaskBalancer/Task.php b/src/TaskBalancer/Task.php index 5efe004..c8e9af4 100644 --- a/src/TaskBalancer/Task.php +++ b/src/TaskBalancer/Task.php @@ -216,17 +216,26 @@ protected function afterRun($success) */ public function runDriver($name) { + // if not find driver by the name, + // will stop and return false $driver = $this->getDriver($name); if (!$driver) { return false; } $this->currentDriver = $driver; - // before run a driver, + + // before run a driver, call 'beforeDriverRun' hooks, // but current driver value is already change to this driver. - $this->callHookHandler('beforeDriverRun', $driver); - // run driver + // If 'beforeDriverRun' hook return false, + // will stop use current driver and try to use next driver + $currentDriverEnable = $this->callHookHandler('beforeDriverRun', $driver); + if (!$currentDriverEnable) { + return $this->tryNextDriver(); + } + + // start run current driver, + // and store result $result = $driver->run(); - // result data $success = $driver->success; $data = [ 'driver' => $driver->name, @@ -234,19 +243,15 @@ public function runDriver($name) 'success' => $success, 'result' => $result, ]; - // store data $this->storeDriverResult($data); - // after run driver + + // call 'afterDriverRun' hooks $this->callHookHandler('afterDriverRun', $data); - // weather to use backup driver + + // weather to use backup driver, + // if failed will try to use next backup driver if (!$success) { - $backUpDriverName = $this->getNextBackupDriverName(); - if ($backUpDriverName) { - // try to run a backup driver - return $this->runDriver($backUpDriverName); - } - // not find a backup driver, current driver must be run false. - return false; + return $this->tryNextDriver(); } return true; @@ -267,6 +272,21 @@ public function storeDriverResult($data) } } + /** + * try to use next backup driver + * @return bool + */ + public function tryNextDriver() + { + $backUpDriverName = $this->getNextBackupDriverName(); + if ($backUpDriverName) { + // try to run a backup driver + return $this->runDriver($backUpDriverName); + } + // not find a backup driver, current driver must be run false. + return false; + } + /** * generator a back up driver`s name. * From d419bbef7ad91f2d560ef062affff5a89721e18e Mon Sep 17 00:00:00 2001 From: lan tian peng Date: Tue, 12 Apr 2016 05:54:22 -0400 Subject: [PATCH 2/2] Applied fixes from StyleCI --- src/TaskBalancer/Task.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/TaskBalancer/Task.php b/src/TaskBalancer/Task.php index c8e9af4..d3ac1a9 100644 --- a/src/TaskBalancer/Task.php +++ b/src/TaskBalancer/Task.php @@ -273,7 +273,8 @@ public function storeDriverResult($data) } /** - * try to use next backup driver + * try to use next backup driver. + * * @return bool */ public function tryNextDriver()