Check for childf status
authorMichael <heluecht@pirati.ca>
Sun, 3 Jan 2021 22:57:25 +0000 (22:57 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 3 Jan 2021 22:57:25 +0000 (22:57 +0000)
bin/daemon.php
src/Core/Worker.php

index ec50730..cc86a9f 100755 (executable)
@@ -223,6 +223,9 @@ while (true) {
                $sleep = min(1000000, round(log10($arg) * 1000000, 0));
                usleep($sleep);
 
+               $pid = pcntl_waitpid(-1, $status, WNOHANG);
+               Logger::info('Checked children status via pcntl_waitpid', ['pid' => $pid, 'status' => $status]);
+       
                $timeout = ($seconds >= $wait_interval);
        } while (!$timeout && !Worker::IPCJobsExists());
 
index ec317a6..559ae68 100644 (file)
@@ -1230,6 +1230,17 @@ class Worker
 
                DI::process()->end();
                Logger::info('Worker ended', ['cron' => $do_cron, 'pid' => getmypid()]);
+
+               DBA::disconnect();
+/*
+               $php = '/usr/bin/php';
+               $param = ['bin/worker.php'];
+               if ($do_cron) {
+                       $param[] = 'no_cron';
+               }
+               pcntl_exec($php, $param);
+               Logger::warning('Error calling worker', ['cron' => $do_cron, 'pid' => getmypid()]);
+*/
                exit();
        }
 
@@ -1242,21 +1253,14 @@ class Worker
         */
        public static function spawnWorker($do_cron = false)
        {
-               Logger::notice("Spawn", ['do_cron' => $do_cron, 'callstack' => System::callstack(20)]);
-               // Worker and daemon are started from the command line.
-               // This means that this is executed by a PHP interpreter without runtime limitations
-               if (function_exists('pcntl_fork') && in_array(DI::mode()->getExecutor(), [Mode::DAEMON, Mode::WORKER])) {
+               if (self::isDaemonMode()) {
                        self::forkProcess($do_cron);
+                       self::IPCSetJobState(false);
                } else {
                        $process = new Core\Process(DI::logger(), DI::mode(), DI::config(),
                                DI::modelProcess(), DI::app()->getBasePath(), getmypid());
                        $process->run('bin/worker.php', ['no_cron' => !$do_cron]);
                }
-
-               // after spawning we have to remove the flag.
-               if (self::isDaemonMode()) {
-                       self::IPCSetJobState(false);
-               }
        }
 
        /**
@@ -1515,6 +1519,11 @@ class Worker
                        return $daemon_mode;
                }
 
+               if (!function_exists('pcntl_fork')) {
+                       self::$daemon_mode = false;
+                       return false;
+               }
+
                $pidfile = DI::config()->get('system', 'pidfile');
                if (empty($pidfile)) {
                        // No pid file, no daemon