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;
81 * Log active processes into the "process" table
83 public function start()
85 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
87 $command = basename($trace[0]['file']);
89 $this->processModel->deleteInactive();
90 $this->processModel->insert($command, $this->pid);
94 * Remove the active process from the "process" table
101 return $this->processModel->deleteByPid($this->pid);
105 * Checks if the maximum number of database processes is reached
107 * @return bool Is the limit reached?
109 public function isMaxProcessesReached()
111 // Deactivated, needs more investigating if this check really makes sense
115 * Commented out to suppress static analyzer issues
117 if ($this->mode->isBackend()) {
118 $process = 'backend';
119 $max_processes = $this->config->get('system', 'max_processes_backend');
120 if (intval($max_processes) == 0) {
124 $process = 'frontend';
125 $max_processes = $this->config->get('system', 'max_processes_frontend');
126 if (intval($max_processes) == 0) {
131 $processlist = DBA::processlist();
132 if ($processlist['list'] != '') {
133 $this->logger->debug('Processcheck: Processes: ' . $processlist['amount'] . ' - Processlist: ' . $processlist['list']);
135 if ($processlist['amount'] > $max_processes) {
136 $this->logger->debug('Processcheck: Maximum number of processes for ' . $process . ' tasks (' . $max_processes . ') reached.');
145 * Checks if the minimal memory is reached
147 * @return bool Is the memory limit reached?
149 public function isMinMemoryReached()
151 $min_memory = $this->config->get('system', 'min_memory', 0);
152 if ($min_memory == 0) {
156 if (!is_readable('/proc/meminfo')) {
160 $memdata = explode("\n", file_get_contents('/proc/meminfo'));
163 foreach ($memdata as $line) {
164 $data = explode(':', $line);
165 if (count($data) != 2) {
168 list($key, $val) = $data;
169 $meminfo[$key] = (int)trim(str_replace('kB', '', $val));
170 $meminfo[$key] = (int)($meminfo[$key] / 1024);
173 if (!isset($meminfo['MemFree'])) {
177 $free = $meminfo['MemFree'];
179 $reached = ($free < $min_memory);
182 $this->logger->debug('Minimal memory reached.', ['free' => $free, 'memtotal' => $meminfo['MemTotal'], 'limit' => $min_memory]);
189 * Checks if the maximum load is reached
191 * @return bool Is the load reached?
193 public function isMaxLoadReached()
195 if ($this->mode->isBackend()) {
196 $process = 'backend';
197 $maxsysload = intval($this->config->get('system', 'maxloadavg'));
198 if ($maxsysload < 1) {
202 $process = 'frontend';
203 $maxsysload = intval($this->config->get('system', 'maxloadavg_frontend'));
204 if ($maxsysload < 1) {
209 $load = System::currentLoad();
211 if (intval($load) > $maxsysload) {
212 $this->logger->info('system load for process too high.', ['load' => $load, 'process' => $process, 'maxsysload' => $maxsysload]);
220 * Executes a child process with 'proc_open'
222 * @param string $command The command to execute
223 * @param array $args Arguments to pass to the command ( [ 'key' => value, 'key2' => value2, ... ]
225 public function run($command, $args)
227 if (!function_exists('proc_open')) {
228 $this->logger->notice('"proc_open" not available - quitting');
232 $cmdline = $this->config->get('config', 'php_path', 'php') . ' ' . escapeshellarg($command);
234 foreach ($args as $key => $value) {
235 if (!is_null($value) && is_bool($value) && !$value) {
239 $cmdline .= ' --' . $key;
240 if (!is_null($value) && !is_bool($value)) {
241 $cmdline .= ' ' . $value;
245 if ($this->isMinMemoryReached()) {
246 $this->logger->notice('Memory limit reached - quitting');
250 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
251 $resource = proc_open('cmd /c start /b ' . $cmdline, [], $foo, $this->basePath);
253 $resource = proc_open($cmdline . ' &', [], $foo, $this->basePath);
255 if (!is_resource($resource)) {
256 $this->logger->notice('We got no resource for command.', ['command' => $cmdline]);
259 proc_close($resource);
261 $this->logger->info('Executed "proc_open"', ['command' => $cmdline, 'callstack' => System::callstack(10)]);