Skip to content

Commit 54d4100

Browse files
authored
Merge pull request #55 from ppavlovic/master
Added ts_finished when task finished executing clearProfilers when execution finishes ts_started now in microseconds php7.3 as minimum requirements
2 parents 4726ced + d77a638 commit 54d4100

4 files changed

Lines changed: 46 additions & 12 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
"require": {
2929
"ext-json" : "*",
3030
"ext-pdo" : "*",
31-
"php" : ">=5.6",
31+
"php" : ">=7.3",
3232
"g4/cron" : "*",
33-
"g4/log" : ">=1.23.0",
33+
"g4/log" : ">=1.48.0",
3434
"g4/mcache" : "^1.4 || ^2.0"
3535
},
3636
"require-dev": {

src/Model/Domain/Task.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class Task
5656
*/
5757
private $tsStarted;
5858

59+
/**
60+
* @var int
61+
*/
62+
private $tsFinished;
63+
5964
/**
6065
* @var float
6166
*/
@@ -204,6 +209,14 @@ public function getTsStarted()
204209
return $this->tsStarted;
205210
}
206211

212+
/**
213+
* @return int
214+
*/
215+
public function getTsFinished()
216+
{
217+
return $this->tsFinished;
218+
}
219+
207220
/**
208221
* @return int
209222
*/
@@ -301,6 +314,15 @@ public function setTsStarted($value)
301314
return $this;
302315
}
303316

317+
/**
318+
* @return \G4\Tasker\Model\Domain\Task
319+
*/
320+
public function setTsFinished($value)
321+
{
322+
$this->tsFinished = $value;
323+
return $this;
324+
}
325+
304326
/**
305327
* @return \G4\Tasker\Model\Domain\Task
306328
*/
@@ -340,6 +362,7 @@ public function setStatusWorking()
340362
public function setStatusBroken($execTime=null)
341363
{
342364
$this->status = \G4\Tasker\Consts::STATUS_BROKEN;
365+
$this->tsFinished = microtime(true);
343366
if ($execTime) {
344367
$this->execTime = $execTime;
345368
}
@@ -349,6 +372,7 @@ public function setStatusBroken($execTime=null)
349372
public function setStatusRetryFailed($execTime=null)
350373
{
351374
$this->status = \G4\Tasker\Consts::STATUS_RETRY_FAILED;
375+
$this->tsFinished = microtime(true);
352376
if ($execTime) {
353377
$this->execTime = $execTime;
354378
}
@@ -357,6 +381,7 @@ public function setStatusRetryFailed($execTime=null)
357381
public function setStatusCompletedNotDone($execTime=null)
358382
{
359383
$this->status = \G4\Tasker\Consts::STATUS_COMPLETED_NOT_DONE;
384+
$this->tsFinished = microtime(true);
360385
if ($execTime) {
361386
$this->execTime = $execTime;
362387
}
@@ -366,6 +391,7 @@ public function setStatusCompletedNotDone($execTime=null)
366391
public function setStatusDone($execTime=null)
367392
{
368393
$this->status = \G4\Tasker\Consts::STATUS_DONE;
394+
$this->tsFinished = microtime(true);
369395
if ($execTime) {
370396
$this->execTime = $execTime;
371397
}
@@ -385,6 +411,7 @@ public function getQueueSource()
385411
public function setStatusWaitingForRetry($execTime=null)
386412
{
387413
$this->status = \G4\Tasker\Consts::STATUS_WAITING_FOR_RETRY;
414+
$this->tsFinished = microtime(true);
388415
if ($execTime) {
389416
$this->execTime = $execTime;
390417
}

src/Tasker2/Injector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ private function logStart()
6868
$this->startTime = microtime(true);
6969
$taskerStart = new \G4\Log\Data\TaskerStart();
7070
$taskerStart
71-
->setId($this->uniqueId);
71+
->setId($this->uniqueId)
72+
->setType(self::LOG_TYPE);
7273
$this->logger->log($taskerStart);
7374
}
7475

@@ -82,4 +83,4 @@ private function logEnd()
8283
$this->logger->logAppend($taskerEnd);
8384
}
8485

85-
}
86+
}

src/Tasker2/Runner.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function setProfiler(Profiler $profiler)
107107

108108
public function clearProfilerData()
109109
{
110-
$this->profiler->clearProfilers();
110+
$this->profiler && $this->profiler->clearProfilers();
111111
}
112112

113113
public function registerProfilerTicker(\G4\Profiler\Ticker\TickerAbstract $profiler)
@@ -126,9 +126,10 @@ public function execute()
126126
$this->taskDomain
127127
->setStatusWorking()
128128
->setIdentifier(gethostname())
129-
->setTsStarted(time());
129+
->setTsStarted(microtime(true));
130130

131131
$this->logTaskStart();
132+
$this->logTaskExecution(true);
132133
$this->logNewRelicStart();
133134

134135
$task = $this->getTaskInstance();
@@ -151,12 +152,14 @@ public function execute()
151152
} catch (\Exception $e) {
152153
$this->taskerExecution->setOutput(ob_get_flush());
153154
$this->handleException($e);
155+
$this->clearProfilerData();
154156
throw $e;
155157
}
156158

157159
$this->timerStop();
158160
$this->taskDomain->setStatusDone($this->getTotalTime());
159161
$this->logTaskExecution();
162+
$this->clearProfilerData();
160163
}
161164

162165
/**
@@ -209,21 +212,24 @@ private function logTaskStart()
209212
$this->taskerExecution
210213
->setId(md5(uniqid(microtime(), true)))
211214
->setTask($this->taskDomain);
212-
$this->logTaskExecution();
213215
return $this;
214216
}
215217

216-
private function logTaskExecution()
218+
private function logTaskExecution($start = false)
217219
{
218-
if ($this->profiler) {
220+
$start
221+
? $this->taskerExecution->taskStarted()
222+
: $this->taskerExecution->taskFinished();
223+
224+
if ($start && $this->profiler) {
219225
$this->taskerExecution->setProfiler($this->profiler);
220226
}
227+
221228
if ($this->logger !== null) {
222229
$this->logger->log($this->taskerExecution);
230+
$start === false && $this->logger->clearData();
223231
}
224-
if ($this->profiler) {
225-
$this->profiler->clearProfilers();
226-
}
232+
227233
return $this;
228234
}
229235

0 commit comments

Comments
 (0)