Skip to content

Commit 4470596

Browse files
committed
Merge branch 'master' of github.com:toplan/task-balancer
2 parents 3d36b76 + 331d0db commit 4470596

File tree

4 files changed

+51
-47
lines changed

4 files changed

+51
-47
lines changed

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,37 @@ task load balancer for php (like the nginx load balancing)
66
```php
77
//define tas
88
Balancer::task('task1', function($task){
9-
//define drive
9+
//define driver
1010
$task->driver('driver1')
1111
->weight(3)->backUp()
1212
->data(['this is data 1'])
13-
->worker(function($driver, $data){
13+
->work(function($driver, $data){
1414
$driver->failed();
15-
print_r('run work! by '.$driver->name.'<br>');
16-
return ['test.driver1 working', $data];
15+
$msg = 'working! by '.$driver->name.'<br>';
16+
print_r($msg);
17+
return [$msg, $data];
1718
});
1819

19-
//define drive
20+
//define driver
2021
$task->driver('driver2')
2122
->weight(3)->backUp(false)
2223
->data(['this is data 2'])
23-
->worker(function($driver, $data){
24+
->work(function($driver, $data){
2425
$driver->success();
25-
print_r('run work! by '.$driver->name.'<br>');
26-
return ['test.driver2 working', $data];
26+
$msg = 'working! by '.$driver->name.'<br>';
27+
print_r($msg);
28+
return [$msg, $data];
2729
});
2830

29-
//define drive
31+
//define driver
3032
$task->driver('driver3')
3133
->weight(0)->backUp()
3234
->data(['this is data 3'])
33-
->worker(function($driver, $data){
35+
->work(function($driver, $data){
3436
$driver->failed();
35-
print_r('run work! by '.$driver->name.'<br>');
36-
return ['test.driver3 working', $data];
37+
$msg = 'working! by '.$driver->name.'<br>';
38+
print_r($msg);
39+
return [$msg, $data];
3740
});
3841
});
3942

@@ -42,6 +45,7 @@ $result = Balancer::run('task1');
4245
```
4346
# Todo
4447

48+
- [ ] remember every tasks` start time and end time.
4549
- [ ] remember every drivers` start time and end time.
4650
- [ ] driver`s create arguments in task class (smart parse arguments)
4751
- [ ] define task`s lifecycle

src/TaskBalancer/Driver.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ class Driver
3838
protected $isBackUp = false;
3939

4040
/**
41-
* driver worker
41+
* driver`s work
4242
* @var null
4343
*/
44-
protected $worker = null;
44+
protected $work = null;
4545

4646
/**
47-
* data for run worker
47+
* data for run work
4848
* @var null
4949
*/
5050
protected $data = null;
@@ -54,43 +54,43 @@ class Driver
5454
* @param $task
5555
* @param $name
5656
* @param int $weight
57-
* @param \Closure $worker
57+
* @param \Closure $work
5858
* @param bool|false $isBackUp
5959
*/
60-
public function __construct(Task $task, $name, $weight = 1, $isBackUp = false, \Closure $worker = null)
60+
public function __construct(Task $task, $name, $weight = 1, $isBackUp = false, \Closure $work = null)
6161
{
6262
$this->task = $task;
6363
$this->name = $name;
6464
$this->weight = intval($weight);
6565
$this->isBackUp = boolval($isBackUp);
66-
$this->worker = $worker;
66+
$this->work = $work;
6767
}
6868

6969
/**
7070
* create a driver instance
7171
* @param $task
7272
* @param $name
7373
* @param int $weight
74-
* @param \Closure $worker
74+
* @param \Closure $work
7575
* @param bool|false $isBackUp
7676
*
7777
* @return static
7878
*/
79-
public static function create(Task $task, $name, $weight = 1, $isBackUp = false, \Closure $worker = null)
79+
public static function create(Task $task, $name, $weight = 1, $isBackUp = false, \Closure $work = null)
8080
{
81-
$driver = new static($task, $name, $weight, $isBackUp, $worker);
81+
$driver = new static($task, $name, $weight, $isBackUp, $work);
8282
return $driver;
8383
}
8484

8585
/**
86-
* run driver`s worker
86+
* run driver`s work
8787
* @return mixed|null
8888
*/
8989
public function run()
9090
{
9191
$result = null;
92-
if ($this->worker) {
93-
$result = call_user_func_array($this->worker, [$this, $this->data]);
92+
if ($this->work) {
93+
$result = call_user_func_array($this->work, [$this, $this->data]);
9494
}
9595
return $result;
9696
}
@@ -153,13 +153,13 @@ public function backUp($is = true)
153153
}
154154

155155
/**
156-
* @param \Closure $worker
156+
* @param \Closure $work
157157
*
158158
* @return $this
159159
*/
160-
public function worker(\Closure $worker)
160+
public function work(\Closure $work)
161161
{
162-
$this->worker = $worker;
162+
$this->work = $work;
163163
return $this;
164164
}
165165

src/TaskBalancer/Task.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ class Task {
4747
protected $currentDriver = null;
4848

4949
/**
50-
* task worker
50+
* task work
5151
* @var null
5252
*/
53-
protected $worker = null;
53+
protected $work = null;
5454

5555
/**
5656
* drivers` results
@@ -61,34 +61,34 @@ class Task {
6161
/**
6262
* constructor
6363
* @param $name
64-
* @param \Closure|null $worker
64+
* @param \Closure|null $work
6565
*/
66-
public function __construct($name, \Closure $worker = null)
66+
public function __construct($name, \Closure $work = null)
6767
{
6868
$this->name = $name;
69-
$this->worker = $worker;
69+
$this->work = $work;
7070
}
7171

7272
/**
7373
* create a new task
7474
* @param $name
75-
* @param \Closure|null $worker
75+
* @param \Closure|null $work
7676
* @return Task
7777
*/
78-
public static function create($name, \Closure $worker = null)
78+
public static function create($name, \Closure $work = null)
7979
{
80-
$task = new static($name, $worker);
81-
$task->runWorker();
80+
$task = new static($name, $work);
81+
$task->runWork();
8282
return $task;
8383
}
8484

8585
/**
86-
* run worker
86+
* run work
8787
*/
88-
public function runWorker()
88+
public function runWork()
8989
{
90-
if ($this->worker) {
91-
call_user_func($this->worker, $this);
90+
if ($this->work) {
91+
call_user_func($this->work, $this);
9292
}
9393
}
9494

@@ -194,11 +194,11 @@ public function driverNameRand()
194194
return array_rand(array_keys($this->drivers));
195195
}
196196

197-
public function driver($name, $weight = 1, $isBackup = false, \Closure $worker = null)
197+
public function driver($name, $weight = 1, $isBackup = false, \Closure $work = null)
198198
{
199199
$driver = $this->getDriver($name);
200200
if (!$driver) {
201-
$driver = Driver::create($this, $name, $weight, $isBackup, $worker);
201+
$driver = Driver::create($this, $name, $weight, $isBackup, $work);
202202
$this->drivers[$name] = $driver;
203203
if ($isBackup) {
204204
$this->backupDrivers[] = $name;

test/index.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010
//define task:
1111
$t = Balancer::task('test1', function($task){
1212
$task->driver('driver1')
13-
->weight(8)->backUp()
13+
->weight(100)->backUp()
1414
->data(['this is data 1'])
15-
->worker(function($driver, $data){
15+
->work(function($driver, $data){
1616
$driver->failed();
1717
print_r('run work! by '.$driver->name.'<br>');
1818
return ['test.driver1 working', $data];
1919
});
2020
$task->driver('driver2')
21-
->weight(2)
21+
->weight(80)
2222
->data(['this is data 2'])
23-
->worker(function($driver, $data){
23+
->work(function($driver, $data){
2424
$driver->failed();
2525
print_r('run work! by '.$driver->name.'<br>');
2626
return ['test.driver2 working', $data];
2727
});
2828
$task->driver('driver3')
2929
->weight(0)->backUp()
3030
->data(['this is data 3'])
31-
->worker(function($driver, $data){
31+
->work(function($driver, $data){
3232
$driver->failed();
3333
print_r('run work! by '.$driver->name.'<br>');
3434
return ['test.driver3 working', $data];

0 commit comments

Comments
 (0)