Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.40% |
35 / 43 |
|
60.00% |
6 / 10 |
CRAP | |
0.00% |
0 / 1 |
| WorkerProcessManager | |
81.40% |
35 / 43 |
|
60.00% |
6 / 10 |
14.09 | |
0.00% |
0 / 1 |
| addProcess | |
68.75% |
11 / 16 |
|
0.00% |
0 / 1 |
1.03 | |||
| canRun | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| getCommand | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMemoryUsage | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
| getCpuUsage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getLimitOfCpu | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setLimitOfCpu | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLimitOfMemory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setLimitOfMemory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getProcesses | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoTaskQueue\model\Worker; |
| 23 | |
| 24 | use Exception; |
| 25 | use oat\oatbox\log\LoggerAwareTrait; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use React\ChildProcess\Process; |
| 28 | |
| 29 | class WorkerProcessManager extends ConfigurableService |
| 30 | { |
| 31 | use LoggerAwareTrait; |
| 32 | |
| 33 | public const SERVICE_ID = 'taoTaskQueue/WorkerProcessManager'; |
| 34 | |
| 35 | public const OPTION_TASK_COMMAND = 'task_command'; |
| 36 | |
| 37 | /** @var Process[] */ |
| 38 | private $processes = []; |
| 39 | |
| 40 | /** @var integer */ |
| 41 | private $limitOfCpu; |
| 42 | |
| 43 | /** @var integer */ |
| 44 | private $limitOfMemory; |
| 45 | |
| 46 | /** |
| 47 | * @param Process $process |
| 48 | */ |
| 49 | public function addProcess(Process $process) |
| 50 | { |
| 51 | $pid = $process->getPid(); |
| 52 | |
| 53 | $process->stdout->on('data', function ($status) use ($pid) { |
| 54 | $this->logInfo('Process: ' . $pid . ' status:' . $status); |
| 55 | }); |
| 56 | |
| 57 | $process->stdout->on('end', function () use ($pid) { |
| 58 | $this->logInfo('Process: ' . $pid . ' ended'); |
| 59 | }); |
| 60 | |
| 61 | $process->stdout->on('error', function (Exception $e) use ($pid) { |
| 62 | $this->logError('Process: ' . $pid . ' error. ' . $e->getMessage()); |
| 63 | }); |
| 64 | |
| 65 | $process->stdout->on('close', function () use ($pid) { |
| 66 | $this->logInfo('Process: ' . $pid . ' closed.'); |
| 67 | |
| 68 | unset($this->processes[$pid]); |
| 69 | }); |
| 70 | |
| 71 | $process->stdin->end($data = null); |
| 72 | |
| 73 | $this->processes[$pid] = $process; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return bool |
| 78 | */ |
| 79 | public function canRun() |
| 80 | { |
| 81 | $this->logInfo('No of process workers running: ' . count($this->processes)); |
| 82 | |
| 83 | $memoryUsage = $this->getMemoryUsage(); |
| 84 | $cpuUsage = $this->getCpuUsage(); |
| 85 | |
| 86 | if ( |
| 87 | $memoryUsage < $this->limitOfMemory |
| 88 | && $cpuUsage < $this->limitOfCpu |
| 89 | ) { |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | $this->logInfo('Limit Of memory and Cpu exceeded waiting for task to finish. |
| 94 | Current memory usage:' . $memoryUsage . ' Cpu usage:' . $cpuUsage); |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return string |
| 101 | */ |
| 102 | public function getCommand() |
| 103 | { |
| 104 | return $this->getOption(static::OPTION_TASK_COMMAND); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return float|int |
| 109 | */ |
| 110 | public function getMemoryUsage() |
| 111 | { |
| 112 | $free = shell_exec('free'); |
| 113 | $free = (string)trim($free); |
| 114 | $freeArray = explode("\n", $free); |
| 115 | $memory = explode(" ", $freeArray[1]); |
| 116 | $memory = array_filter($memory); |
| 117 | $memory = array_merge($memory); |
| 118 | $memoryUsage = $memory[1] > 0 |
| 119 | ? $memory[2] / $memory[1] * 100 |
| 120 | : 0; |
| 121 | |
| 122 | return $memoryUsage; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return mixed |
| 127 | */ |
| 128 | public function getCpuUsage() |
| 129 | { |
| 130 | $load = sys_getloadavg(); |
| 131 | |
| 132 | return $load[0]; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return int |
| 137 | */ |
| 138 | public function getLimitOfCpu() |
| 139 | { |
| 140 | return $this->limitOfCpu; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param int $limitOfCpu |
| 145 | */ |
| 146 | public function setLimitOfCpu($limitOfCpu) |
| 147 | { |
| 148 | $this->limitOfCpu = $limitOfCpu; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @return int |
| 153 | */ |
| 154 | public function getLimitOfMemory() |
| 155 | { |
| 156 | return $this->limitOfMemory; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param int $limitOfMemory |
| 161 | */ |
| 162 | public function setLimitOfMemory($limitOfMemory) |
| 163 | { |
| 164 | $this->limitOfMemory = $limitOfMemory; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @return Process[] |
| 169 | */ |
| 170 | public function getProcesses() |
| 171 | { |
| 172 | return $this->processes; |
| 173 | } |
| 174 | } |