Skip to content

Commit 14f7021

Browse files
committed
0.5.0 beta
1 parent 93de96f commit 14f7021

File tree

6 files changed

+146
-107
lines changed

6 files changed

+146
-107
lines changed

README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Lightweight and powerful task load balancing.
1414
# Install
1515

1616
```php
17-
composer require 'toplan/task-balancer:~0.5'
17+
composer require toplan/task-balancer:~0.5
1818
```
1919

2020
# Usage
@@ -33,7 +33,7 @@ Balancer::task('task1', function($task){
3333
$driver->failure();
3434
}
3535
//return some data you need
36-
return 'some data here';
36+
return 'some data you need';
3737
});
3838

3939
//or like this:
@@ -70,7 +70,7 @@ The `$result` structure:
7070
'started_at' => timestamp,
7171
'finished_at' => timestamp
7272
],
73-
'result' => 'some data here'
73+
'result' => 'some data you need'
7474
],
7575
...
7676
]
@@ -84,10 +84,11 @@ The `$result` structure:
8484
### Balancer::task($name[, $data], Closure $ready);
8585

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

8889
```php
8990
Balancer::task('taskName', $data, function($task){
90-
//task`s ready work, such as create drivers.
91+
//task's ready work, such as create drivers.
9192
});
9293
```
9394

@@ -101,19 +102,23 @@ The keys of `$options`:
101102
- `data`
102103
- `driver`
103104

105+
### $task->name($name)
106+
107+
set the name of task.
108+
104109
### $task->data($data)
105110

106111
Set the data of task.
107112

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

110-
Create a driver for the task.
115+
Create a driver for the task. The closure `$work` will been called with arguments `$driver` and `$data`.
111116

112117
> Expected `$weight` to be a integer, default `1`.
113118
114119
```php
115120
$task->driver('driverName 80 backup', function($driver, $data){
116-
//driver`s work
121+
//driver's job content.
117122
});
118123
```
119124

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

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

126-
Whether the backup driver.
131+
Set whether backup driver.
127132

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

134139
> `$data` will store in driver instance.
135140
136-
### $driver->work(Closure $work function($driver, $data){});
141+
### $driver->work(Closure $work);
137142

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

140145
> `$data` equals to `$driver->getData()`
141146
147+
### $driver->reset($config[, $weight][, 'backup'], Closure $work)
148+
149+
Reset driver's weight value, job content and reset whether backup.
150+
151+
### $driver->destroy()
152+
153+
Remove the driver from task which belongs to.
154+
142155
### $driver->failure()
143156

144-
Set current driver run failed.
157+
Set the driver running failure.
145158

146159
### $driver->success()
147160

148-
Set current driver run succeed.
161+
Set the driver run successfully.
149162

150163
### $driver->getDriverData()
151164

152-
Get the data of driver.
165+
Get the data which store in driver instance.
153166

154167
### $driver->getTaskData()
155168

156-
Get the data of task.
169+
Get the data which store in task instance.
157170

158171

159172
## 2. Lifecycle & Hooks

demo/demo.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,38 @@
88

99
use Toplan\TaskBalance\Balancer;
1010

11-
//define task:
1211
$data = [
1312
'name' => 'top lan',
1413
'age' => '20',
1514
];
15+
16+
//define task:
1617
$t = Balancer::task('test1', $data, function ($task) {
17-
$task->driver('driver_1 100', 'backup', function ($driver, $data) {
18-
$person = new Person($data['name'], $data['age']);
19-
$driver->failure();
20-
print_r('run work! by '.$driver->name.'<br>');
18+
$task->driver('driver_1 90', 'backup', function ($driver, $data) {
19+
$person = new Person($data['name'], $data['age']);
20+
$driver->failure();
21+
print_r('run work! by '.$driver->name.'<br>');
2122

22-
return ['test.driver1 working', $person->toString()];
23-
});
23+
return ['test.driver1 working', $person->toString()];
24+
});
2425

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

29-
return ['test.driver2 working', $data];
30-
})
31-
->data(['this is data 2']);
30+
return ['test.driver2 working', $data];
31+
})
32+
->data(['this is data 2']);
3233

3334
$task->driver('driver_3')
34-
->weight(0)->backUp()
35-
->data(['this is data 3'])
36-
->work(function ($driver, $data) {
37-
$driver->success();
38-
print_r('run work! by '.$driver->name.'<br>');
35+
->weight(0)->backUp()
36+
->data(['this is data 3'])
37+
->work(function ($driver, $data) {
38+
$driver->success();
39+
print_r('run work! by '.$driver->name.'<br>');
3940

40-
return ['test.driver3 working', $data];
41-
});
41+
return ['test.driver3 working', $data];
42+
});
4243

4344
$task->beforeRun(function ($task) {
4445
print_r('before run --------!<br>');
@@ -49,11 +50,7 @@
4950
});
5051
});
5152

52-
//run task:
53-
$data['age'] = '25';
5453
$result = Balancer::run('test1', $data);
55-
56-
print_r('<br>resuts data:<br>');
5754
var_dump($result);
5855

5956
class Person

demo/demo2.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
//define task:
1616
Balancer::task('task1', $data, function ($task) {
1717
$task->driver('driver1 10 backup', function ($driver, $data) {
18-
$driver->failure();
19-
print_r('run work! by '.$driver->name.'<br>');
20-
});
18+
$driver->failure();
19+
print_r('run work! by '.$driver->name.'<br>');
20+
});
2121

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

src/TaskBalancer/Balancer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Balancer
1010
/**
1111
* task instances.
1212
*
13-
* @var array
13+
* @var Task[]
1414
*/
1515
protected static $tasks = [];
1616

@@ -21,7 +21,7 @@ class Balancer
2121
* @param mixed $data
2222
* @param \Closure|null $ready
2323
*
24-
* @return null|Task
24+
* @return Task
2525
*/
2626
public static function task($name, $data = null, \Closure $ready = null)
2727
{
@@ -39,7 +39,7 @@ public static function task($name, $data = null, \Closure $ready = null)
3939
}
4040

4141
/**
42-
* run a task instance.
42+
* run task.
4343
*
4444
* @param string $name
4545
* @param array $opts

src/TaskBalancer/Driver.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Driver
7979
*/
8080
public function __construct(Task $task, $name, $weight = 1, $backup = false, \Closure $work = null)
8181
{
82-
if (!is_string($name) || !$name) {
82+
if (!is_string($name) || empty($name)) {
8383
throw new TaskBalancerException('Expected the driver name to be a non-empty string.');
8484
}
8585
if ($task->hasDriver($name)) {
@@ -125,7 +125,7 @@ protected function beforeRun()
125125
/**
126126
* run driver`s work.
127127
*
128-
* @return mixed|null
128+
* @return mixed
129129
*/
130130
public function run()
131131
{
@@ -216,7 +216,7 @@ public function backup($is = true)
216216
return $this;
217217
}
218218
$this->backup = $is;
219-
if ($is) {
219+
if ($this->backup) {
220220
$this->task->appendToBackupDrivers($this);
221221
} else {
222222
$this->task->removeFromBackupDrivers($this);
@@ -240,9 +240,9 @@ public function work(\Closure $work)
240240
}
241241

242242
/**
243-
* update driver's attributes.
243+
* reset driver's properties.
244244
*/
245-
public function update()
245+
public function reset()
246246
{
247247
$args = func_get_args();
248248
extract(self::parseArgs($args));
@@ -300,7 +300,7 @@ public function destroy()
300300
/**
301301
* override.
302302
*
303-
* @param $name
303+
* @param string $name
304304
*
305305
* @return mixed
306306
*/
@@ -315,7 +315,7 @@ public function __get($name)
315315
}
316316

317317
/**
318-
* parse arguments.
318+
* parse arguments to driver properties.
319319
*
320320
* @param array $args
321321
*

0 commit comments

Comments
 (0)