Skip to content

Commit

Permalink
Merge pull request #5 from toplan/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
toplan committed Apr 12, 2016
2 parents 26f0431 + 6f5741a commit 8434b3e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 |

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
49 changes: 35 additions & 14 deletions src/TaskBalancer/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,37 +216,42 @@ 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,
'time' => $driver->time,
'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;
Expand All @@ -267,6 +272,22 @@ 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.
*
Expand Down

0 comments on commit 8434b3e

Please sign in to comment.