Skip to content

Commit 8cc74f6

Browse files
committed
remember task run time
1 parent 7c705d0 commit 8cc74f6

File tree

4 files changed

+118
-9
lines changed

4 files changed

+118
-9
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ $result = Balancer::run('task1');
4545
```
4646
# Todo
4747

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

src/TaskBalancer/Driver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static function create(Task $task, $name, $weight = 1, $isBackUp = false,
9595
* before run hook
9696
* @return bool
9797
*/
98-
public function beforeRun()
98+
private function beforeRun()
9999
{
100100
$this->time['started_at'] = microtime();
101101
return true;
@@ -124,7 +124,7 @@ public function run()
124124
*
125125
* @return mixed
126126
*/
127-
public function afterRun($result)
127+
private function afterRun($result)
128128
{
129129
$this->time['finished_at'] = microtime();
130130
return $result;

src/TaskBalancer/Task.php

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ class Task {
5252
*/
5353
protected $work = null;
5454

55+
/**
56+
* task run time
57+
* @var array
58+
*/
59+
protected $time = [
60+
'started_at' => 0,
61+
'finished_at' => 0
62+
];
63+
5564
/**
5665
* drivers` results
5766
* @var array
@@ -92,22 +101,61 @@ public function runWork()
92101
}
93102
}
94103

104+
/**
105+
* run task
106+
* @param string $driverName
107+
*
108+
* @return bool
109+
* @throws \Exception
110+
*/
95111
public function run($driverName = '')
96112
{
97113
if ($this->isRunning()) {
98114
//stop run because current task is running
99115
return false;
100116
}
101-
$this->status = static::RUNNING;
117+
if (!$this->beforeRun()) {
118+
return false;
119+
}
102120
if (!$driverName) {
103121
$driverName = $this->getDriverNameByWeight();
104122
}
105123
$this->resortBackupDrivers($driverName);
106124
$success = $this->runDriver($driverName);
125+
return $this->afterRun($success);
126+
}
127+
128+
/**
129+
* before run task
130+
* @return bool
131+
*/
132+
private function beforeRun()
133+
{
134+
$this->time['started_at'] = microtime();
135+
$this->status = static::RUNNING;
136+
return true;
137+
}
138+
139+
/**
140+
* after run task
141+
* @param $result
142+
*
143+
* @return mixed
144+
*/
145+
private function afterRun($result)
146+
{
107147
$this->status = static::FINISHED;
108-
return $success;
148+
$this->time['finished_at'] = microtime();
149+
return $result;
109150
}
110151

152+
/**
153+
* run driver by name
154+
* @param $name
155+
*
156+
* @return bool
157+
* @throws \Exception
158+
*/
111159
public function runDriver($name)
112160
{
113161
$driver = $this->getDriver($name);
@@ -133,6 +181,11 @@ public function runDriver($name)
133181
return $success;
134182
}
135183

184+
/**
185+
* get a backup driver and run it
186+
* @return bool
187+
* @throws \Exception
188+
*/
136189
public function runBackupDriver()
137190
{
138191
$name = $this->getNextBackupDriverName();
@@ -142,6 +195,10 @@ public function runBackupDriver()
142195
return true;
143196
}
144197

198+
/**
199+
* generator a back up driver`s name
200+
* @return null
201+
*/
145202
public function getNextBackupDriverName()
146203
{
147204
$drivers = $this->backupDrivers;
@@ -162,6 +219,11 @@ public function getNextBackupDriverName()
162219
return null;
163220
}
164221

222+
/**
223+
* get a driver`s name by drivers` weight
224+
* @return mixed
225+
* @throws \Exception
226+
*/
165227
public function getDriverNameByWeight()
166228
{
167229
$count = $base = 0;
@@ -190,11 +252,24 @@ public function getDriverNameByWeight()
190252
throw new \Exception('get driver name by weight failed, something wrong');
191253
}
192254

255+
/**
256+
* get a driver name
257+
* @return mixed
258+
*/
193259
public function driverNameRand()
194260
{
195261
return array_rand(array_keys($this->drivers));
196262
}
197263

264+
/**
265+
* create a new driver instance for current task
266+
* @param $name
267+
* @param int $weight
268+
* @param bool|false $isBackup
269+
* @param \Closure|null $work
270+
*
271+
* @return mixed
272+
*/
198273
public function driver($name, $weight = 1, $isBackup = false, \Closure $work = null)
199274
{
200275
$driver = $this->getDriver($name);
@@ -205,9 +280,15 @@ public function driver($name, $weight = 1, $isBackup = false, \Closure $work = n
205280
$this->backupDrivers[] = $name;
206281
}
207282
}
208-
return $this->drivers[$name];
283+
return $driver;
209284
}
210285

286+
/**
287+
* current task has driver?
288+
* @param $name
289+
*
290+
* @return bool
291+
*/
211292
public function hasDriver($name)
212293
{
213294
if (!$this->drivers) {
@@ -216,6 +297,12 @@ public function hasDriver($name)
216297
return isset($this->drivers[$name]);
217298
}
218299

300+
/**
301+
* get a driver from current task drives pool
302+
* @param $name
303+
*
304+
* @return null
305+
*/
219306
public function getDriver($name)
220307
{
221308
if ($this->hasDriver($name)) {
@@ -224,6 +311,10 @@ public function getDriver($name)
224311
return null;
225312
}
226313

314+
/**
315+
* init back up drivers
316+
* @param $name
317+
*/
227318
public function resortBackupDrivers($name)
228319
{
229320
if (count($this->backupDrivers) < 2) {
@@ -237,17 +328,29 @@ public function resortBackupDrivers($name)
237328
}
238329
}
239330

331+
/**
332+
* task is running ?
333+
* @return bool
334+
*/
240335
public function isRunning()
241336
{
242337
return $this->status == static::RUNNING;
243338
}
244339

340+
/**
341+
* reset status
342+
* @return $this
343+
*/
245344
public function reset()
246345
{
247346
$this->status = '';
248347
return $this;
249348
}
250349

350+
/**
351+
* add a driver to backup drivers
352+
* @param $driverName
353+
*/
251354
public function addToBackupDrivers($driverName)
252355
{
253356
if ($driverName instanceof Driver) {
@@ -258,6 +361,10 @@ public function addToBackupDrivers($driverName)
258361
}
259362
}
260363

364+
/**
365+
* remove character driver from backup drivers
366+
* @param $driverName
367+
*/
261368
public function removeFromBackupDrivers($driverName)
262369
{
263370
if ($driverName instanceof Driver) {

test/index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@
3939
$result = Balancer::run('test1');
4040

4141
print_r('<br>resuts data:<br>');
42-
var_dump($result);
42+
var_dump($result);
43+
print_r('<hr>task data:<br>');
44+
var_dump($t);

0 commit comments

Comments
 (0)