3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core;
25 use Friendica\Core\Config\IConfig;
27 use Psr\Log\LoggerInterface;
30 * Methods for interacting with the current process or create new process
32 * @todo 2019.12 Next release, this class holds all process relevant methods based on the big Worker class
33 * - Starting new processes (including checks)
34 * - Enabling multi-node processing (e.g. for docker service)
35 * - Using an process-id per node
36 * - Using memory locks for multi-node locking (redis, memcached, ..)
41 * @var LoggerInterface
60 /** @var Model\Process */
61 private $processModel;
64 * The Process ID of this process
70 public function __construct(LoggerInterface $logger, App\Mode $mode, IConfig $config, Model\Process $processModel, string $basepath, int $pid)
72 $this->logger = $logger;
74 $this->config = $config;
75 $this->basePath = $basepath;
76 $this->processModel = $processModel;
86 public function setPid(int $pid)
92 * Log active processes into the "process" table
94 public function start()
96 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
98 $command = basename($trace[0]['file']);
100 $this->processModel->deleteInactive();
101 $this->processModel->insert($command, $this->pid);
105 * Remove the active process from the "process" table
110 public function end()
112 return $this->processModel->deleteByPid($this->pid);
116 * Checks if the maximum number of database processes is reached
118 * @return bool Is the limit reached?
120 public function isMaxProcessesReached()
122 // Deactivated, needs more investigating if this check really makes sense
126 * Commented out to suppress static analyzer issues
128 if ($this->mode->isBackend()) {
129 $process = 'backend';
130 $max_processes = $this->config->get('system', 'max_processes_backend');
131 if (intval($max_processes) == 0) {
135 $process = 'frontend';
136 $max_processes = $this->config->get('system', 'max_processes_frontend');
137 if (intval($max_processes) == 0) {
142 $processlist = DBA::processlist();
143 if ($processlist['list'] != '') {
144 $this->logger->debug('Processcheck: Processes: ' . $processlist['amount'] . ' - Processlist: ' . $processlist['list']);
146 if ($processlist['amount'] > $max_processes) {
147 $this->logger->debug('Processcheck: Maximum number of processes for ' . $process . ' tasks (' . $max_processes . ') reached.');
156 * Checks if the minimal memory is reached
158 * @return bool Is the memory limit reached?
160 public function isMinMemoryReached()
162 $min_memory = $this->config->get('system', 'min_memory', 0);
163 if ($min_memory == 0) {
167 if (!is_readable('/proc/meminfo')) {
171 $memdata = explode("\n", file_get_contents('/proc/meminfo'));
174 foreach ($memdata as $line) {
175 $data = explode(':', $line);
176 if (count($data) != 2) {
179 list($key, $val) = $data;
180 $meminfo[$key] = (int)trim(str_replace('kB', '', $val));
181 $meminfo[$key] = (int)($meminfo[$key] / 1024);
184 if (!isset($meminfo['MemFree'])) {
188 $free = $meminfo['MemFree'];
190 $reached = ($free < $min_memory);
193 $this->logger->warning('Minimal memory reached.', ['free' => $free, 'memtotal' => $meminfo['MemTotal'], 'limit' => $min_memory]);
200 * Checks if the maximum load is reached
202 * @return bool Is the load reached?
204 public function isMaxLoadReached()
206 if ($this->mode->isBackend()) {
207 $process = 'backend';
208 $maxsysload = intval($this->config->get('system', 'maxloadavg'));
209 if ($maxsysload < 1) {
213 $process = 'frontend';
214 $maxsysload = intval($this->config->get('system', 'maxloadavg_frontend'));
215 if ($maxsysload < 1) {
220 $load = System::currentLoad();
222 if (intval($load) > $maxsysload) {
223 $this->logger->warning('system load for process too high.', ['load' => $load, 'process' => $process, 'maxsysload' => $maxsysload]);
231 * Executes a child process with 'proc_open'
233 * @param string $command The command to execute
234 * @param array $args Arguments to pass to the command ( [ 'key' => value, 'key2' => value2, ... ]
236 public function run($command, $args)
238 if (!function_exists('proc_open')) {
239 $this->logger->warning('"proc_open" not available - quitting');
243 $cmdline = $this->config->get('config', 'php_path', 'php') . ' ' . escapeshellarg($command);
245 foreach ($args as $key => $value) {
246 if (!is_null($value) && is_bool($value) && !$value) {
250 $cmdline .= ' --' . $key;
251 if (!is_null($value) && !is_bool($value)) {
252 $cmdline .= ' ' . $value;
256 if ($this->isMinMemoryReached()) {
257 $this->logger->warning('Memory limit reached - quitting');
261 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
262 $resource = proc_open('cmd /c start /b ' . $cmdline, [], $foo, $this->basePath);
264 $resource = proc_open($cmdline . ' &', [], $foo, $this->basePath);
266 if (!is_resource($resource)) {
267 $this->logger->warning('We got no resource for command.', ['command' => $cmdline]);
270 proc_close($resource);
272 $this->logger->info('Executed "proc_open"', ['command' => $cmdline, 'callstack' => System::callstack(10)]);