3 namespace Friendica\Core;
6 use Friendica\Core\Config\IConfig;
7 use Psr\Log\LoggerInterface;
10 * Methods for interacting with the current process or create new process
12 * @todo 2019.12 Next release, this class holds all process relevant methods based on the big Worker class
13 * - Starting new processes (including checks)
14 * - Enabling multi-node processing (e.g. for docker service)
15 * - Using an process-id per node
16 * - Using memory locks for multi-node locking (redis, memcached, ..)
21 * @var LoggerInterface
40 public function __construct(LoggerInterface $logger, App\Mode $mode, IConfig $config, string $basepath)
42 $this->logger = $logger;
44 $this->config = $config;
45 $this->basePath = $basepath;
49 * Checks if the maximum number of database processes is reached
51 * @return bool Is the limit reached?
53 public function isMaxProcessesReached()
55 // Deactivated, needs more investigating if this check really makes sense
59 * Commented out to suppress static analyzer issues
61 if ($this->mode->isBackend()) {
63 $max_processes = $this->config->get('system', 'max_processes_backend');
64 if (intval($max_processes) == 0) {
68 $process = 'frontend';
69 $max_processes = $this->config->get('system', 'max_processes_frontend');
70 if (intval($max_processes) == 0) {
75 $processlist = DBA::processlist();
76 if ($processlist['list'] != '') {
77 $this->logger->debug('Processcheck: Processes: ' . $processlist['amount'] . ' - Processlist: ' . $processlist['list']);
79 if ($processlist['amount'] > $max_processes) {
80 $this->logger->debug('Processcheck: Maximum number of processes for ' . $process . ' tasks (' . $max_processes . ') reached.');
89 * Checks if the minimal memory is reached
91 * @return bool Is the memory limit reached?
93 public function isMinMemoryReached()
95 $min_memory = $this->config->get('system', 'min_memory', 0);
96 if ($min_memory == 0) {
100 if (!is_readable('/proc/meminfo')) {
104 $memdata = explode("\n", file_get_contents('/proc/meminfo'));
107 foreach ($memdata as $line) {
108 $data = explode(':', $line);
109 if (count($data) != 2) {
112 list($key, $val) = $data;
113 $meminfo[$key] = (int)trim(str_replace('kB', '', $val));
114 $meminfo[$key] = (int)($meminfo[$key] / 1024);
117 if (!isset($meminfo['MemFree'])) {
121 $free = $meminfo['MemFree'];
123 $reached = ($free < $min_memory);
126 $this->logger->debug('Minimal memory reached.', ['free' => $free, 'memtotal' => $meminfo['MemTotal'], 'limit' => $min_memory]);
133 * Checks if the maximum load is reached
135 * @return bool Is the load reached?
137 public function isMaxLoadReached()
139 if ($this->mode->isBackend()) {
140 $process = 'backend';
141 $maxsysload = intval($this->config->get('system', 'maxloadavg'));
142 if ($maxsysload < 1) {
146 $process = 'frontend';
147 $maxsysload = intval($this->config->get('system', 'maxloadavg_frontend'));
148 if ($maxsysload < 1) {
153 $load = System::currentLoad();
155 if (intval($load) > $maxsysload) {
156 $this->logger->info('system load for process too high.', ['load' => $load, 'process' => $process, 'maxsysload' => $maxsysload]);
164 * Executes a child process with 'proc_open'
166 * @param string $command The command to execute
167 * @param array $args Arguments to pass to the command ( [ 'key' => value, 'key2' => value2, ... ]
169 public function run($command, $args)
171 if (!function_exists('proc_open')) {
175 $cmdline = $this->config->get('config', 'php_path', 'php') . ' ' . escapeshellarg($command);
177 foreach ($args as $key => $value) {
178 if (!is_null($value) && is_bool($value) && !$value) {
182 $cmdline .= ' --' . $key;
183 if (!is_null($value) && !is_bool($value)) {
184 $cmdline .= ' ' . $value;
188 if ($this->isMinMemoryReached()) {
192 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
193 $resource = proc_open('cmd /c start /b ' . $cmdline, [], $foo, $this->basePath);
195 $resource = proc_open($cmdline . ' &', [], $foo, $this->basePath);
197 if (!is_resource($resource)) {
198 $this->logger->debug('We got no resource for command.', ['cmd' => $cmdline]);
201 proc_close($resource);