Skip to content

Commit 0d0e26d

Browse files
committed
override driver data
1 parent 8cc74f6 commit 0d0e26d

File tree

4 files changed

+112
-17
lines changed

4 files changed

+112
-17
lines changed

src/TaskBalancer/Balancer.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,21 @@ class Balancer {
2121

2222
/**
2323
* create a task instance
24-
* @param $name
25-
* @param \Closure|null $fn
24+
* @param $name
25+
* @param $data
26+
* @param \Closure|null $callback
2627
*
27-
* @return Task
28+
* @return null|Task
2829
*/
29-
public static function task($name, \Closure $fn = null)
30+
public static function task($name, $data, \Closure $callback = null)
3031
{
3132
$task = self::getTask($name);
3233
if (!$task) {
33-
$task = Task::create($name, $fn);
34+
if (is_callable($data)) {
35+
$callback = $data;
36+
$data = null;
37+
}
38+
$task = Task::create($name, $data, $callback);
3439
self::$tasks[$name] = $task;
3540
}
3641
return $task;
@@ -39,16 +44,20 @@ public static function task($name, \Closure $fn = null)
3944
/**
4045
* run a task instance
4146
* @param string $name
47+
* @param string $data
4248
*
4349
* @return mixed
4450
* @throws \Exception
4551
*/
46-
public static function run($name = '')
52+
public static function run($name = '', $data = null)
4753
{
4854
$task = self::getTask($name);
4955
if (!$task) {
5056
throw new \Exception("run task $name failed, not find this task");
5157
}
58+
if ($data) {
59+
$task->data($data);
60+
}
5261
$task->run();
5362
return $task->results;
5463
}

src/TaskBalancer/Driver.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function __construct(Task $task, $name, $weight = 1, $isBackUp = false, \
8787
*/
8888
public static function create(Task $task, $name, $weight = 1, $isBackUp = false, \Closure $work = null)
8989
{
90-
$driver = new static($task, $name, $weight, $isBackUp, $work);
90+
$driver = new self($task, $name, $weight, $isBackUp, $work);
9191
return $driver;
9292
}
9393

@@ -113,7 +113,7 @@ public function run()
113113
}
114114
$result = null;
115115
if (is_callable($this->work)) {
116-
$result = call_user_func_array($this->work, [$this, $this->data]);
116+
$result = call_user_func_array($this->work, [$this, $this->getData()]);
117117
}
118118
return $this->afterRun($result);
119119
}
@@ -198,8 +198,45 @@ public function work(\Closure $work)
198198
return $this;
199199
}
200200

201+
/**
202+
* get data
203+
* @return null
204+
*/
205+
public function getData()
206+
{
207+
return $this->getDriverData() ?:
208+
$this->getTaskData();
209+
}
210+
211+
/**
212+
* get driver data
213+
* @return null
214+
*/
215+
public function getDriverData()
216+
{
217+
return $this->data;
218+
}
219+
220+
/**
221+
* get task data
222+
* @return null
223+
*/
224+
public function getTaskData()
225+
{
226+
return $this->task->data;
227+
}
228+
229+
/**
230+
* override
231+
* @param $name
232+
*
233+
* @return null
234+
*/
201235
public function __get($name)
202236
{
237+
if ($name == "data") {
238+
return $this->getData();
239+
}
203240
if (isset($this->$name)) {
204241
return $this->$name;
205242
}

src/TaskBalancer/Task.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ class Task {
6161
'finished_at' => 0
6262
];
6363

64+
/**
65+
* data for driver
66+
* @var null
67+
*/
68+
protected $data = null;
69+
6470
/**
6571
* drivers` results
6672
* @var array
@@ -70,23 +76,26 @@ class Task {
7076
/**
7177
* constructor
7278
* @param $name
79+
* @param $data
7380
* @param \Closure|null $work
7481
*/
75-
public function __construct($name, \Closure $work = null)
82+
public function __construct($name, $data = null, \Closure $work = null)
7683
{
7784
$this->name = $name;
85+
$this->data = $data;
7886
$this->work = $work;
7987
}
8088

8189
/**
8290
* create a new task
8391
* @param $name
92+
* @param $data
8493
* @param \Closure|null $work
8594
* @return Task
8695
*/
87-
public static function create($name, \Closure $work = null)
96+
public static function create($name, $data = null, \Closure $work = null)
8897
{
89-
$task = new static($name, $work);
98+
$task = new self($name, $data, $work);
9099
$task->runWork();
91100
return $task;
92101
}
@@ -131,9 +140,12 @@ public function run($driverName = '')
131140
*/
132141
private function beforeRun()
133142
{
143+
$pass = true;
134144
$this->time['started_at'] = microtime();
135-
$this->status = static::RUNNING;
136-
return true;
145+
if ($pass) {
146+
$this->status = static::RUNNING;
147+
}
148+
return $pass;
137149
}
138150

139151
/**
@@ -377,6 +389,18 @@ public function removeFromBackupDrivers($driverName)
377389
}
378390
}
379391

392+
/**
393+
* set data
394+
* @param $data
395+
*
396+
* @return $this
397+
*/
398+
public function data($data)
399+
{
400+
$this->data = $data;
401+
return $this;
402+
}
403+
380404
/**
381405
* override
382406
* @param $name

test/index.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@
88
use Toplan\TaskBalance\Driver;
99

1010
//define task:
11-
$t = Balancer::task('test1', function($task){
11+
$data = [
12+
'name' => 'top$',
13+
'age' => '25!'
14+
];
15+
$t = Balancer::task('test1', $data, function($task){
1216
$task->driver('driver1')
1317
->weight(100)->backUp()
14-
->data(['this is data 1'])
1518
->work(function($driver, $data){
19+
$person = new Person($data['name'], $data['age']);
1620
$driver->failed();
1721
print_r('run work! by '.$driver->name.'<br>');
18-
return ['test.driver1 working', $data];
22+
return ['test.driver1 working', $person->toString()];
1923
});
24+
2025
$task->driver('driver2')
2126
->weight(100)
2227
->data(['this is data 2'])
@@ -25,6 +30,7 @@
2530
print_r('run work! by '.$driver->name.'<br>');
2631
return ['test.driver2 working', $data];
2732
});
33+
2834
$task->driver('driver3')
2935
->weight(0)->backUp()
3036
->data(['this is data 3'])
@@ -36,9 +42,28 @@
3642
});
3743

3844
//run task:
39-
$result = Balancer::run('test1');
45+
$data['age'] = '25###';
46+
$result = Balancer::run('test1', $data);
4047

4148
print_r('<br>resuts data:<br>');
4249
var_dump($result);
4350
print_r('<hr>task data:<br>');
4451
var_dump($t);
52+
53+
class Person {
54+
55+
protected $name;
56+
57+
protected $age;
58+
59+
public function __construct($name, $age)
60+
{
61+
$this->name = $name;
62+
$this->age = $age;
63+
}
64+
65+
public function toString()
66+
{
67+
return "hi, I am $this->name, and $this->age year old";
68+
}
69+
}

0 commit comments

Comments
 (0)