Skip to content

Commit

Permalink
0.5.0 beta
Browse files Browse the repository at this point in the history
  • Loading branch information
toplan committed May 11, 2017
1 parent 93de96f commit 14f7021
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 107 deletions.
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Lightweight and powerful task load balancing.
# Install

```php
composer require 'toplan/task-balancer:~0.5'
composer require toplan/task-balancer:~0.5
```

# Usage
Expand All @@ -33,7 +33,7 @@ Balancer::task('task1', function($task){
$driver->failure();
}
//return some data you need
return 'some data here';
return 'some data you need';
});

//or like this:
Expand Down Expand Up @@ -70,7 +70,7 @@ The `$result` structure:
'started_at' => timestamp,
'finished_at' => timestamp
],
'result' => 'some data here'
'result' => 'some data you need'
],
...
]
Expand All @@ -84,10 +84,11 @@ The `$result` structure:
### Balancer::task($name[, $data], Closure $ready);

Create a task instance, and return it.
The closure `$ready` immediately called with argument `$task`.

```php
Balancer::task('taskName', $data, function($task){
//task`s ready work, such as create drivers.
//task's ready work, such as create drivers.
});
```

Expand All @@ -101,19 +102,23 @@ The keys of `$options`:
- `data`
- `driver`

### $task->name($name)

set the name of task.

### $task->data($data)

Set the data of task.

### $task->driver($config[, $weight][, 'backup'], Closure $work)

Create a driver for the task.
Create a driver for the task. The closure `$work` will been called with arguments `$driver` and `$data`.

> Expected `$weight` to be a integer, default `1`.
```php
$task->driver('driverName 80 backup', function($driver, $data){
//driver`s work
//driver's job content.
});
```

Expand All @@ -123,7 +128,7 @@ Set the weight value of driver.

### $driver->backup($is)

Whether the backup driver.
Set whether backup driver.

> Expected `$is` to be boolean.
Expand All @@ -133,27 +138,35 @@ Set the data of driver.

> `$data` will store in driver instance.
### $driver->work(Closure $work function($driver, $data){});
### $driver->work(Closure $work);

Set the work of driver, which will been called with two arguments: `$driver`, `$data`.
Set the job content of driver.

> `$data` equals to `$driver->getData()`
### $driver->reset($config[, $weight][, 'backup'], Closure $work)

Reset driver's weight value, job content and reset whether backup.

### $driver->destroy()

Remove the driver from task which belongs to.

### $driver->failure()

Set current driver run failed.
Set the driver running failure.

### $driver->success()

Set current driver run succeed.
Set the driver run successfully.

### $driver->getDriverData()

Get the data of driver.
Get the data which store in driver instance.

### $driver->getTaskData()

Get the data of task.
Get the data which store in task instance.


## 2. Lifecycle & Hooks
Expand Down
43 changes: 20 additions & 23 deletions demo/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,38 @@

use Toplan\TaskBalance\Balancer;

//define task:
$data = [
'name' => 'top lan',
'age' => '20',
];

//define task:
$t = Balancer::task('test1', $data, function ($task) {
$task->driver('driver_1 100', 'backup', function ($driver, $data) {
$person = new Person($data['name'], $data['age']);
$driver->failure();
print_r('run work! by '.$driver->name.'<br>');
$task->driver('driver_1 90', 'backup', function ($driver, $data) {
$person = new Person($data['name'], $data['age']);
$driver->failure();
print_r('run work! by '.$driver->name.'<br>');

return ['test.driver1 working', $person->toString()];
});
return ['test.driver1 working', $person->toString()];
});

$task->driver('driver_2', 90, function ($driver, $data) {
$driver->failure();
print_r('run work! by '.$driver->name.'<br>');
$driver->failure();
print_r('run work! by '.$driver->name.'<br>');

return ['test.driver2 working', $data];
})
->data(['this is data 2']);
return ['test.driver2 working', $data];
})
->data(['this is data 2']);

$task->driver('driver_3')
->weight(0)->backUp()
->data(['this is data 3'])
->work(function ($driver, $data) {
$driver->success();
print_r('run work! by '.$driver->name.'<br>');
->weight(0)->backUp()
->data(['this is data 3'])
->work(function ($driver, $data) {
$driver->success();
print_r('run work! by '.$driver->name.'<br>');

return ['test.driver3 working', $data];
});
return ['test.driver3 working', $data];
});

$task->beforeRun(function ($task) {
print_r('before run --------!<br>');
Expand All @@ -49,11 +50,7 @@
});
});

//run task:
$data['age'] = '25';
$result = Balancer::run('test1', $data);

print_r('<br>resuts data:<br>');
var_dump($result);

class Person
Expand Down
6 changes: 3 additions & 3 deletions demo/demo2.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
//define task:
Balancer::task('task1', $data, function ($task) {
$task->driver('driver1 10 backup', function ($driver, $data) {
$driver->failure();
print_r('run work! by '.$driver->name.'<br>');
});
$driver->failure();
print_r('run work! by '.$driver->name.'<br>');
});

$task->beforeRun(function ($task, $index, $handlers, $preReturn) {
print_r("before run ---$preReturn-----$index<br>");
Expand Down
6 changes: 3 additions & 3 deletions src/TaskBalancer/Balancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Balancer
/**
* task instances.
*
* @var array
* @var Task[]
*/
protected static $tasks = [];

Expand All @@ -21,7 +21,7 @@ class Balancer
* @param mixed $data
* @param \Closure|null $ready
*
* @return null|Task
* @return Task
*/
public static function task($name, $data = null, \Closure $ready = null)
{
Expand All @@ -39,7 +39,7 @@ public static function task($name, $data = null, \Closure $ready = null)
}

/**
* run a task instance.
* run task.
*
* @param string $name
* @param array $opts
Expand Down
14 changes: 7 additions & 7 deletions src/TaskBalancer/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Driver
*/
public function __construct(Task $task, $name, $weight = 1, $backup = false, \Closure $work = null)
{
if (!is_string($name) || !$name) {
if (!is_string($name) || empty($name)) {
throw new TaskBalancerException('Expected the driver name to be a non-empty string.');
}
if ($task->hasDriver($name)) {
Expand Down Expand Up @@ -125,7 +125,7 @@ protected function beforeRun()
/**
* run driver`s work.
*
* @return mixed|null
* @return mixed
*/
public function run()
{
Expand Down Expand Up @@ -216,7 +216,7 @@ public function backup($is = true)
return $this;
}
$this->backup = $is;
if ($is) {
if ($this->backup) {
$this->task->appendToBackupDrivers($this);
} else {
$this->task->removeFromBackupDrivers($this);
Expand All @@ -240,9 +240,9 @@ public function work(\Closure $work)
}

/**
* update driver's attributes.
* reset driver's properties.
*/
public function update()
public function reset()
{
$args = func_get_args();
extract(self::parseArgs($args));
Expand Down Expand Up @@ -300,7 +300,7 @@ public function destroy()
/**
* override.
*
* @param $name
* @param string $name
*
* @return mixed
*/
Expand All @@ -315,7 +315,7 @@ public function __get($name)
}

/**
* parse arguments.
* parse arguments to driver properties.
*
* @param array $args
*
Expand Down
Loading

0 comments on commit 14f7021

Please sign in to comment.