+
+ /**
+ * Defers the current worker entry
+ */
+ public static function defer()
+ {
+ if (empty(BaseObject::getApp()->queue)) {
+ return;
+ }
+
+ $queue = BaseObject::getApp()->queue;
+
+ $retrial = $queue['retrial'];
+ $id = $queue['id'];
+
+ if ($retrial > 14) {
+ Logger::log('Id ' . $id . ' had been tried 14 times. We stop now.', Logger::DEBUG);
+ return;
+ }
+
+ // Calculate the delay until the next trial
+ $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
+ $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
+
+ Logger::log('Defer execution ' . $retrial . ' of id ' . $id . ' to ' . $next, Logger::DEBUG);
+
+ $fields = ['retrial' => $retrial + 1, 'next_try' => $next, 'executed' => DBA::NULL_DATETIME, 'pid' => 0];
+ DBA::update('workerqueue', $fields, ['id' => $id]);
+ }
+
+ /**
+ * Log active processes into the "process" table
+ *
+ * @brief Log active processes into the "process" table
+ */
+ public static function startProcess()
+ {
+ $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
+
+ $command = basename($trace[0]['file']);
+
+ Process::deleteInactive();
+
+ Process::insert($command);
+ }
+
+ /**
+ * Remove the active process from the "process" table
+ *
+ * @brief Remove the active process from the "process" table
+ * @return bool
+ * @throws \Exception
+ */
+ public static function endProcess()
+ {
+ return Process::deleteByPid();
+ }
+
+ /**
+ * Set the flag if some job is waiting
+ *
+ * @brief Set the flag if some job is waiting
+ * @param boolean $jobs Is there a waiting job?
+ * @throws \Exception
+ */
+ public static function IPCSetJobState($jobs)
+ {
+ DBA::update('worker-ipc', ['jobs' => $jobs], ['key' => 1], true);
+ }
+
+ /**
+ * Checks if some worker job waits to be executed
+ *
+ * @brief Checks if some worker job waits to be executed
+ * @return bool
+ * @throws \Exception
+ */
+ public static function IPCJobsExists()
+ {
+ $row = DBA::selectFirst('worker-ipc', ['jobs'], ['key' => 1]);
+
+ // When we don't have a row, no job is running
+ if (!DBA::isResult($row)) {
+ return false;
+ }
+
+ return (bool)$row['jobs'];
+ }