-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathMonitorExampleTask.php
More file actions
115 lines (97 loc) · 2.81 KB
/
MonitorExampleTask.php
File metadata and controls
115 lines (97 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
/**
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace Queue\Queue\Task;
use Cake\Log\LogTrait;
use Queue\Queue\AddFromBackendInterface;
use Queue\Queue\AddInterface;
use Queue\Queue\Task;
/**
* A Simple QueueTask example.
*/
class MonitorExampleTask extends Task implements AddInterface, AddFromBackendInterface {
use LogTrait;
/**
* Timeout for run, after which the Task is reassigned to a new worker.
*/
public ?int $timeout = 10;
/**
* MonitorExample add functionality.
* Will create one example job in the queue, which later will be executed using run();
*
* To invoke from CLI execute:
* - bin/cake queue add Queue.MonitorExample
*
* @param string|null $data
*
* @return void
*/
public function add(?string $data): void {
$this->io->out('CakePHP Queue MonitorExample task.');
$this->io->hr();
$this->io->out($this->description() ?? '');
$this->io->out('I will now add an example Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
$this->io->out(' bin/cake queue run');
$this->io->out(' ');
$this->io->out('You can find the sourcecode of this task in: ');
$this->io->out(__FILE__);
$this->io->out(' ');
$this->QueuedJobs->createJob('Queue.MonitorExample');
$this->io->success('OK, job created, now run the worker');
}
/**
* @return string|null
*/
public function description(): ?string {
return __d('queue', 'Logs server memory and PHP info');
}
/**
* MonitorExample run function.
* This function is executed, when a worker is executing a task.
* The return parameter will determine, if the task will be marked completed, or be requeued.
*
* @param array<string, mixed> $data The array passed to QueuedJobsTable::createJob()
* @param int $jobId The id of the QueuedJob entity
*
* @return void
*/
public function run(array $data, int $jobId): void {
$this->io->hr();
$this->io->out('CakePHP Queue MonitorExample task.');
$this->io->hr();
$this->doMonitoring();
$this->io->success(' -> Success, the MonitorExample Job was run. <-');
}
/**
* @return void
*/
protected function doMonitoring(): void {
$memory = $this->getSystemMemInfo();
$array = [
'[PHP] ' . PHP_VERSION,
'[PHP Memory Limit] ' . ini_get('memory_limit'),
'[Server Memory] Total: ' . $memory['MemTotal'] . ', Free: ' . $memory['MemFree'],
];
$message = implode(PHP_EOL, $array);
$this->log($message, 'info');
}
/**
* @return array<string>
*/
protected function getSystemMemInfo(): array {
$data = explode("\n", file_get_contents('/proc/meminfo') ?: '');
$meminfo = [];
foreach ($data as $line) {
if (!str_contains($line, ':')) {
continue;
}
[$key, $val] = explode(':', $line);
$meminfo[$key] = trim($val);
}
return $meminfo;
}
}