Skip to content

Commit 7c705d0

Browse files
committed
remember drivers`s run time
1 parent 4470596 commit 7c705d0

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/TaskBalancer/Driver.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ class Driver
4949
*/
5050
protected $data = null;
5151

52+
/**
53+
* run work time
54+
* @var array
55+
*/
56+
protected $time = [
57+
'started_at' => 0,
58+
'finished_at' => 0,
59+
];
60+
5261
/**
5362
* constructor
5463
* @param $task
@@ -82,16 +91,42 @@ public static function create(Task $task, $name, $weight = 1, $isBackUp = false,
8291
return $driver;
8392
}
8493

94+
/**
95+
* before run hook
96+
* @return bool
97+
*/
98+
public function beforeRun()
99+
{
100+
$this->time['started_at'] = microtime();
101+
return true;
102+
}
103+
85104
/**
86105
* run driver`s work
87106
* @return mixed|null
88107
*/
89108
public function run()
90109
{
110+
$this->beforeRun();
111+
if (!$this->beforeRun()) {
112+
return null;
113+
}
91114
$result = null;
92-
if ($this->work) {
115+
if (is_callable($this->work)) {
93116
$result = call_user_func_array($this->work, [$this, $this->data]);
94117
}
118+
return $this->afterRun($result);
119+
}
120+
121+
/**
122+
* after run hook
123+
* @param $result
124+
*
125+
* @return mixed
126+
*/
127+
public function afterRun($result)
128+
{
129+
$this->time['finished_at'] = microtime();
95130
return $result;
96131
}
97132

src/TaskBalancer/Task.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static function create($name, \Closure $work = null)
8787
*/
8888
public function runWork()
8989
{
90-
if ($this->work) {
90+
if (is_callable($this->work)) {
9191
call_user_func($this->work, $this);
9292
}
9393
}
@@ -119,6 +119,7 @@ public function runDriver($name)
119119
$success = $driver->success;
120120
$data = [
121121
'driver' => $driver->name,
122+
'time' => $driver->time,
122123
'success' => $success,
123124
'result' => $result,
124125
];

test/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
return ['test.driver1 working', $data];
1919
});
2020
$task->driver('driver2')
21-
->weight(80)
21+
->weight(100)
2222
->data(['this is data 2'])
2323
->work(function($driver, $data){
2424
$driver->failed();

0 commit comments

Comments
 (0)