Skip to content

Commit 261eab6

Browse files
authored
Merge pull request #63 from ppavlovic/master
Fixed 'timestamp' on TaskerExecution.php to show time when the task i…
2 parents f317dd8 + f3c7da1 commit 261eab6

6 files changed

Lines changed: 85 additions & 18 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"php" : ">=7.3",
3636
"ext-json" : "*",
3737
"ext-curl" : "*",
38-
"g4/runner" : ">=0.22.0",
38+
"g4/runner" : ">=0.26.0",
3939
"g4/utility" : "*",
4040
"g4/value-object" : "*",
4141
"g4/version" : "^0.0.2",

src/Adapter/Redis.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public function save(array $data)
5858
} catch (\Exception $exception) {
5959
error_log ($exception->getMessage(), 0);
6060
}
61-
6261
}
6362

6463

@@ -88,4 +87,4 @@ public function doRPushBatch(array $data)
8887
$this->client->rPush((string) $this->key, ...$arguments);
8988
}
9089

91-
}
90+
}

src/AdapterAbstract.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ public function appendData(array $data)
2727
return $this;
2828
}
2929

30+
public function clearData()
31+
{
32+
// clear all log data except keys _index and _type
33+
foreach ($this->data as $key => $value) {
34+
if (!in_array($key, ['_index', '_type'])) {
35+
unset($this->data[$key]);
36+
}
37+
}
38+
return $this;
39+
}
40+
3041
public function beLazy()
3142
{
3243
$this->shouldBeLazy = true;
@@ -51,4 +62,4 @@ public function shouldSaveInOneCall()
5162
{
5263
return $this->shouldSaveInOneCall;
5364
}
54-
}
65+
}

src/Data/TaskerExecution.php

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class TaskerExecution extends LoggerAbstract
99
{
1010
const LOG_TYPE = 'execution';
11-
const CONTENT_LIMIT = 512;
11+
const CONTENT_LIMIT = 30000;
1212

1313
/**
1414
* @var \G4\Tasker\Model\Domain\Task
@@ -35,42 +35,78 @@ class TaskerExecution extends LoggerAbstract
3535
*/
3636
private $profiler;
3737

38+
private $taskFinished = false;
39+
40+
public function taskStarted()
41+
{
42+
$this->taskFinished = false;
43+
}
44+
45+
public function taskFinished()
46+
{
47+
$this->taskFinished = true;
48+
}
49+
3850
public function getRawData()
3951
{
40-
$rawData = [
52+
$rawData = $this->taskFinished
53+
? $this->getRawDataEnd()
54+
: $this->getRawDataStart();
55+
return $rawData;
56+
}
57+
58+
private function getRawDataStart()
59+
{
60+
return [
4161
'id' => $this->getId(),
4262
'timestamp' => $this->getJsTimestamp(),
4363
'hostname' => \gethostname(),
4464
'pid' => \getmypid(),
4565
'type' => $this->logType ?: self::LOG_TYPE,
46-
'memory_peak_usage' => memory_get_peak_usage(),
47-
'exception' => $this->exception === null ? null : \json_encode([
48-
'message' => $this->exception->getMessage(),
49-
'line' => $this->exception->getLine(),
50-
'code' => $this->exception->getCode(),
51-
'trace' => $this->exception->getTrace(),
52-
]
53-
),
54-
5566
'task_id' => $this->task->getTaskId(),
5667
'recu_id' => $this->task->getRecurringId(),
5768
'identifier' => $this->task->getIdentifier(),
5869
'task' => $this->task->getTask(),
5970
'data' => $this->task->getData(),
60-
'output' => $this->getOutput(),
6171
'request_uuid' => $this->task->getRequestUuid(),
6272
'priority' => $this->task->getPriority(),
6373
'status' => $this->task->getStatus(),
6474
'ts_created' => $this->task->getTsCreated(),
6575
'ts_started' => $this->task->getTsStarted(),
66-
'exec_time' => $this->task->getExecTime(),
67-
'exec_time_ms' => (int)($this->task->getExecTime() * 1000),
6876
'started_count' => $this->task->getStartedCount(),
6977
'php_version' => str_replace(PHP_EXTRA_VERSION, '', PHP_VERSION),
7078
'app_version' => $this->getAppVersionNumber(),
7179
'queue_source' => method_exists($this->task, 'getQueueSource')
7280
? $this->task->getQueueSource() : null,
7381
];
82+
}
83+
84+
private function getRawDataEnd()
85+
{
86+
$profilerOutput = $this->profiler
87+
? $this->profiler->getTaskerProfilerOutput($this->task->getExecTime())
88+
: null;
89+
90+
$rawData = [
91+
'id' => $this->getId(),
92+
'ts_finished' => $this->task->getTsFinished(),
93+
'memory_peak_usage' => memory_get_peak_usage(),
94+
'exception' => $this->exception === null
95+
? null
96+
: \json_encode([
97+
'message' => $this->exception->getMessage(),
98+
'line' => $this->exception->getLine(),
99+
'code' => $this->exception->getCode(),
100+
'trace' => $this->exception->getTrace(),
101+
]
102+
),
103+
'output' => $this->getOutput(),
104+
'exec_time' => $this->task->getExecTime(),
105+
'exec_time_ms' => (int)($this->task->getExecTime() * 1000),
106+
'status' => $this->task->getStatus(),
107+
'started_count' => $this->task->getStartedCount(),
108+
'profiler' => $profilerOutput ? \json_encode($profilerOutput) : null,
109+
];
74110

75111
$rawData += $this->getCpuLoad();
76112

@@ -113,6 +149,11 @@ public function setProfiler(Profiler $profiler)
113149
return $this;
114150
}
115151

152+
public function hasProfiler()
153+
{
154+
return $this->profiler !== null;
155+
}
156+
116157
/**
117158
* @param string
118159
*/

src/Data/TaskerStart.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ class TaskerStart extends LoggerAbstract
1010
*/
1111
private $options;
1212

13+
/**
14+
* @var string
15+
*/
16+
private $type;
17+
1318
/**
1419
* @return array
1520
*/
@@ -18,6 +23,7 @@ public function getRawData()
1823
$rawData = [
1924
'id' => $this->getId(),
2025
'timestamp' => $this->getJsTimestamp(),
26+
'type' => $this->type,
2127
'datetime' => \date('Y-m-d H:i:s'),
2228
'options' => \json_encode($this->options),
2329
'hostname' => \gethostname(),
@@ -40,4 +46,9 @@ public function setOptions($options)
4046
$this->options = $options;
4147
return $this;
4248
}
49+
50+
public function setType($type)
51+
{
52+
$this->type = $type;
53+
}
4354
}

src/Logger.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public function logAppend(\G4\Log\Data\LoggerAbstract $data)
6666
$this->adapter->saveAppend($data->getRawData());
6767
}
6868

69+
public function clearData()
70+
{
71+
$this->adapter->clearData();
72+
}
73+
6974
/**
7075
* @param mixed $var
7176
* @param string $tag

0 commit comments

Comments
 (0)