Skip to content

Commit

Permalink
upgrade hooks system
Browse files Browse the repository at this point in the history
  • Loading branch information
toplan committed Dec 7, 2015
1 parent 6de830a commit 43966f3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 22 deletions.
52 changes: 37 additions & 15 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.1.5'
composer require 'toplan/task-balancer:~0.2.0'
```

# Usage
Expand Down Expand Up @@ -63,7 +63,8 @@ The `$result` structure:
'finished_at' => ''
],
'logs' => [
'driver_1' => [
'0' => [
'driver' => 'Luosimao',
...
],
...
Expand Down Expand Up @@ -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

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.1.5",
"version": "0.2.0",
"keywords": ["task", "balance", "load balancing", "balancer"],
"authors": [
{
Expand Down
27 changes: 21 additions & 6 deletions src/TaskBalancer/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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");
}
Expand Down
57 changes: 57 additions & 0 deletions test/demo2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
//test multiple handlers

//require('../vendor/autoload.php');
require('../src/TaskBalancer/Balancer.php');
require('../src/TaskBalancer/Driver.php');
require('../src/TaskBalancer/Task.php');
require('../src/TaskBalancer/TaskBalancerException.php');

use Toplan\TaskBalance\Balancer;

//define task:
Balancer::task('task1', $data, function($task){
$task->driver('driver1 10 backup', function($driver, $data){
$driver->failed();
print_r('run work! by '.$driver->name.'<br>');
});

$task->beforeRun(function($task, $preReturn, $index){
print_r("before run ---$preReturn-----$index<br>");
return 11;
});

$task->beforeRun(function($task, $preReturn, $index){
print_r("before run ---$preReturn-----$index<br>");
return 22;
}, true);

$task->beforeRun(function($task, $preReturn, $index){
print_r("before run ---$preReturn-----$index<br>");
});

$task->hook('beforeDriverRun', function($task, $preReturn, $index){
print_r("before driver run ---$preReturn-----$index<br>");
return [1];
});

$task->hook('beforeDriverRun', function($task, $preReturn, $index){
print_r("before driver run ---".implode(',', $preReturn)."-----$index<br>");
return [1,2];
}, true);

$task->hook('beforeDriverRun', function($task, $preReturn, $index){
print_r("before driver run ---".implode(',', $preReturn)."-----$index<br>");
return [1,2,3];
});

$task->afterRun(function($task, $results){
print_r("after run --------!<br>");
});
});

//run task:
$result = Balancer::run('task1', $data);

print_r('<br>');
print_r($result);

0 comments on commit 43966f3

Please sign in to comment.