+ /**
+ * Returns the next retrial level for worker jobs.
+ * This function will skip levels when jobs are older.
+ *
+ * @param array $queue Worker queue entry
+ * @param integer $max_level maximum retrial level
+ * @return integer the next retrial level value
+ */
+ private static function getNextRetrial($queue, $max_level)
+ {
+ $created = strtotime($queue['created']);
+ $retrial_time = time() - $created;
+
+ $new_retrial = $queue['retrial'] + 1;
+ $total = 0;
+ for ($retrial = 0; $retrial <= $max_level + 1; ++$retrial) {
+ $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
+ $total += $delay;
+ if (($total < $retrial_time) && ($retrial > $queue['retrial'])) {
+ $new_retrial = $retrial;
+ }
+ }
+ Logger::info('New retrial for task', ['id' => $queue['id'], 'created' => $queue['created'], 'old' => $queue['retrial'], 'new' => $new_retrial]);
+ return $new_retrial;
+ }
+
+ /**
+ * 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'];
+ $priority = $queue['priority'];
+
+ $max_level = Config::get('system', 'worker_defer_limit');
+
+ $new_retrial = self::getNextRetrial($queue, $max_level);
+
+ if ($new_retrial > $max_level) {
+ Logger::info('The task exceeded the maximum retry count', ['id' => $id, 'max_level' => $max_level, 'retrial' => $new_retrial]);
+ return;
+ }
+
+ // Calculate the delay until the next trial
+ $delay = (($new_retrial + 2) ** 4) + (rand(1, 30) * ($new_retrial));
+ $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
+
+ if (($priority < PRIORITY_MEDIUM) && ($new_retrial > 3)) {
+ $priority = PRIORITY_MEDIUM;
+ } elseif (($priority < PRIORITY_LOW) && ($new_retrial > 6)) {
+ $priority = PRIORITY_LOW;
+ } elseif (($priority < PRIORITY_NEGLIGIBLE) && ($new_retrial > 8)) {
+ $priority = PRIORITY_NEGLIGIBLE;
+ }
+
+ Logger::info('Deferred task', ['id' => $id, 'retrial' => $new_retrial, 'next_execution' => $next, 'old_prio' => $queue['priority'], 'new_prio' => $priority]);
+
+ $stamp = (float)microtime(true);
+ $fields = ['retrial' => $new_retrial, 'next_try' => $next, 'executed' => DBA::NULL_DATETIME, 'pid' => 0, 'priority' => $priority];
+ DBA::update('workerqueue', $fields, ['id' => $id]);
+ self::$db_duration += (microtime(true) - $stamp);
+ self::$db_duration_write += (microtime(true) - $stamp);
+ }
+