+ /**
+ * 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'];
+
+ 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');
+
+ if (($priority < PRIORITY_MEDIUM) && ($retrial > 2)) {
+ $priority = PRIORITY_MEDIUM;
+ } elseif (($priority < PRIORITY_LOW) && ($retrial > 5)) {
+ $priority = PRIORITY_LOW;
+ } elseif (($priority < PRIORITY_NEGLIGIBLE) && ($retrial > 7)) {
+ $priority = PRIORITY_NEGLIGIBLE;
+ }
+
+ Logger::log('Defer execution ' . $retrial . ' of id ' . $id . ' to ' . $next . ' - priority old/new: ' . $queue['priority'] . '/' . $priority, Logger::DEBUG);
+
+ $stamp = (float)microtime(true);
+ $fields = ['retrial' => $retrial + 1, '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);
+ }
+