Skip to content

Commit 8434b3e

Browse files
committed
Merge pull request #5 from toplan/dev
Dev
2 parents 26f0431 + 6f5741a commit 8434b3e

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ lightweight and powerful task load balancing for php
1313
# Install
1414

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

1919
# Usage
@@ -179,7 +179,7 @@ get data value of task instance.
179179
| beforeCreateDriver | $task, $preReturn, $index, $handlers | no effect |
180180
| afterCreateDriver | $task, $preReturn, $index, $handlers | no effect |
181181
| beforeRun | $task, $preReturn, $index, $handlers | if `false` will stop run task and return `false` |
182-
| beforeDriverRun | $task, $driver, $preReturn, $index, $handlers | no effect |
182+
| beforeDriverRun | $task, $driver, $preReturn, $index, $handlers | if `false` will stop run current driver and try to use next backup driver |
183183
| afterDriverRun | $task, $driverResult, $preReturn, $index, $handlers | no effect |
184184
| afterRun | $task, $taskResult, $preReturn, $index, $handlers | if not boolean will override result value |
185185

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.4.0",
5+
"version": "0.4.1",
66
"keywords": ["task", "balance", "load balancing", "balancer"],
77
"authors": [
88
{

src/TaskBalancer/Task.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,37 +216,42 @@ protected function afterRun($success)
216216
*/
217217
public function runDriver($name)
218218
{
219+
// if not find driver by the name,
220+
// will stop and return false
219221
$driver = $this->getDriver($name);
220222
if (!$driver) {
221223
return false;
222224
}
223225
$this->currentDriver = $driver;
224-
// before run a driver,
226+
227+
// before run a driver, call 'beforeDriverRun' hooks,
225228
// but current driver value is already change to this driver.
226-
$this->callHookHandler('beforeDriverRun', $driver);
227-
// run driver
229+
// If 'beforeDriverRun' hook return false,
230+
// will stop use current driver and try to use next driver
231+
$currentDriverEnable = $this->callHookHandler('beforeDriverRun', $driver);
232+
if (!$currentDriverEnable) {
233+
return $this->tryNextDriver();
234+
}
235+
236+
// start run current driver,
237+
// and store result
228238
$result = $driver->run();
229-
// result data
230239
$success = $driver->success;
231240
$data = [
232241
'driver' => $driver->name,
233242
'time' => $driver->time,
234243
'success' => $success,
235244
'result' => $result,
236245
];
237-
// store data
238246
$this->storeDriverResult($data);
239-
// after run driver
247+
248+
// call 'afterDriverRun' hooks
240249
$this->callHookHandler('afterDriverRun', $data);
241-
// weather to use backup driver
250+
251+
// weather to use backup driver,
252+
// if failed will try to use next backup driver
242253
if (!$success) {
243-
$backUpDriverName = $this->getNextBackupDriverName();
244-
if ($backUpDriverName) {
245-
// try to run a backup driver
246-
return $this->runDriver($backUpDriverName);
247-
}
248-
// not find a backup driver, current driver must be run false.
249-
return false;
254+
return $this->tryNextDriver();
250255
}
251256

252257
return true;
@@ -267,6 +272,22 @@ public function storeDriverResult($data)
267272
}
268273
}
269274

275+
/**
276+
* try to use next backup driver.
277+
*
278+
* @return bool
279+
*/
280+
public function tryNextDriver()
281+
{
282+
$backUpDriverName = $this->getNextBackupDriverName();
283+
if ($backUpDriverName) {
284+
// try to run a backup driver
285+
return $this->runDriver($backUpDriverName);
286+
}
287+
// not find a backup driver, current driver must be run false.
288+
return false;
289+
}
290+
270291
/**
271292
* generator a back up driver`s name.
272293
*

0 commit comments

Comments
 (0)