Skip to content

Commit 26f0431

Browse files
committed
Merge pull request #2 from toplan/v0.4
V0.4
2 parents c7b7071 + efbab64 commit 26f0431

File tree

5 files changed

+11
-34
lines changed

5 files changed

+11
-34
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ lightweight and powerful task load balancing for php
1313
# Install
1414

1515
```php
16-
composer require 'toplan/task-balancer:~0.3.0'
16+
composer require 'toplan/task-balancer:~0.4.0'
1717
```
1818

1919
# Usage
@@ -232,7 +232,7 @@ $task->beforeRun(function($task, $preReturn, $index, $handlers){
232232
- [x] remember every driver`s start time and end time.
233233
- [x] smart parse arguments of method `driver()`.
234234
- [x] task lifecycle and hooks
235-
- [ ] remove driver and add new driver.
235+
- [ ] hot remove/add a driver.
236236
- [ ] pause/resume task
237237

238238
# Dependents

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "toplan/task-balancer",
33
"description": "lightweight and powerful task load balancing for php (like the nginx load balancing)",
44
"license": "MIT",
5-
"version": "0.3.0",
5+
"version": "0.4.0",
66
"keywords": ["task", "balance", "load balancing", "balancer"],
77
"authors": [
88
{

src/TaskBalancer/Balancer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,5 @@ public static function getTask($name)
9797
if (self::hasTask($name)) {
9898
return self::$tasks[$name];
9999
}
100-
101-
return;
102100
}
103101
}

src/TaskBalancer/Driver.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,5 @@ public function __get($name)
266266
if (isset($this->$name)) {
267267
return $this->$name;
268268
}
269-
270-
return;
271269
}
272270
}

src/TaskBalancer/Task.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class Task
5454
/**
5555
* task status.
5656
*
57-
* @var string
57+
* @var string|null
5858
*/
59-
protected $status = '';
59+
protected $status = null;
6060

6161
/**
6262
* current use driver.
@@ -212,15 +212,13 @@ protected function afterRun($success)
212212
*
213213
* @param $name
214214
*
215-
* @throws TaskBalancerException
216-
*
217215
* @return bool
218216
*/
219217
public function runDriver($name)
220218
{
221219
$driver = $this->getDriver($name);
222220
if (!$driver) {
223-
throw new TaskBalancerException("Don`t found driver [$name] in task [$this->name], please define it for current task");
221+
return false;
224222
}
225223
$this->currentDriver = $driver;
226224
// before run a driver,
@@ -291,14 +289,10 @@ public function getNextBackupDriverName()
291289
if (($currentKey + 1) < count($drivers)) {
292290
return $drivers[$currentKey + 1];
293291
}
294-
295-
return;
296292
}
297293

298294
/**
299-
* get a driver`s name by drivers` weight.
300-
*
301-
* @throws TaskBalancerException
295+
* get a driver name by driver weight.
302296
*
303297
* @return mixed
304298
*/
@@ -318,25 +312,16 @@ public function getDriverNameByWeight()
318312
$base = $max;
319313
}
320314
}
321-
if ($count < 1) {
322-
return $this->driverNameRand();
315+
if ($count <= 0) {
316+
return;
323317
}
324318
$number = mt_rand(0, $count - 1);
325319
foreach ($map as $data) {
326320
if ($number >= $data['min'] && $number < $data['max']) {
327321
return $data['driver'];
328322
}
329323
}
330-
throw new TaskBalancerException('Get driver name by weight failed, something wrong');
331-
}
332324

333-
/**
334-
* get a driver name.
335-
*
336-
* @return mixed
337-
*/
338-
public function driverNameRand()
339-
{
340325
return array_rand(array_keys($this->drivers));
341326
}
342327

@@ -431,15 +416,13 @@ public function hasDriver($name)
431416
*
432417
* @param $name
433418
*
434-
* @return null
419+
* @return mixed
435420
*/
436421
public function getDriver($name)
437422
{
438423
if ($this->hasDriver($name)) {
439424
return $this->drivers[$name];
440425
}
441-
442-
return;
443426
}
444427

445428
/**
@@ -477,7 +460,7 @@ public function isRunning()
477460
*/
478461
public function reset()
479462
{
480-
$this->status = '';
463+
$this->status = null;
481464
$this->results = null;
482465

483466
return $this;
@@ -604,8 +587,6 @@ public function __get($name)
604587
if (array_key_exists($name, $this->drivers)) {
605588
return $this->drivers[$name];
606589
}
607-
608-
return;
609590
}
610591

611592
/**

0 commit comments

Comments
 (0)