namespace Friendica\Model;
use Friendica\Core\Config;
-use Friendica\Database\DBM;
-use Friendica\Util\Temporal;
-use dba;
-
-require_once 'include/dba.php';
-require_once 'include/datetime.php';
+use Friendica\Core\Logger;
+use Friendica\Database\DBA;
+use Friendica\Util\DateTimeFormat;
class Queue
{
/**
* @param string $id id
+ * @throws \Exception
*/
public static function updateTime($id)
{
- logger('queue: requeue item ' . $id);
- dba::update('queue', ['last' => Temporal::convert()], ['id' => $id]);
+ Logger::log('queue: requeue item ' . $id);
+ $queue = DBA::selectFirst('queue', ['retrial'], ['id' => $id]);
+ if (!DBA::isResult($queue)) {
+ return;
+ }
+
+ $retrial = $queue['retrial'];
+
+ if ($retrial > 14) {
+ self::removeItem($id);
+ }
+
+ // Calculate the delay until the next trial
+ $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
+ $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
+
+ DBA::update('queue', ['last' => DateTimeFormat::utcNow(), 'retrial' => $retrial + 1, 'next' => $next], ['id' => $id]);
}
/**
* @param string $id id
+ * @throws \Exception
*/
public static function removeItem($id)
{
- logger('queue: remove queue item ' . $id);
- dba::delete('queue', ['id' => $id]);
+ Logger::log('queue: remove queue item ' . $id);
+ DBA::delete('queue', ['id' => $id]);
}
/**
* @param int $cid Contact id
*
* @return bool The communication with this contact has currently problems
+ * @throws \Exception
*/
public static function wasDelayed($cid)
{
intval($cid)
);
- $was_delayed = DBM::is_result($r);
+ $was_delayed = DBA::isResult($r);
// We set "term-date" to a current date if the communication has problems.
// If the communication works again we reset this value.
$r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` <= '1000-01-01' LIMIT 1",
intval($cid)
);
- $was_delayed = !DBM::is_result($r);
+ $was_delayed = !DBA::isResult($r);
}
return $was_delayed;
* @param string $network network
* @param string $msg message
* @param boolean $batch batch, default false
+ * @param string $guid
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- public static function add($cid, $network, $msg, $batch = false)
+ public static function add($cid, $network, $msg, $batch = false, $guid = '')
{
$max_queue = Config::get('system', 'max_contact_queue');
intval($cid)
);
- if (DBM::is_result($r)) {
+ if (DBA::isResult($r)) {
if ($batch && ($r[0]['total'] > $batch_queue)) {
- logger('add_to_queue: too many queued items for batch server ' . $cid . ' - discarding message');
+ Logger::log('too many queued items for batch server ' . $cid . ' - discarding message');
return;
} elseif ((! $batch) && ($r[0]['total'] > $max_queue)) {
- logger('add_to_queue: too many queued items for contact ' . $cid . ' - discarding message');
+ Logger::log('too many queued items for contact ' . $cid . ' - discarding message');
return;
}
}
- dba::insert('queue', ['cid' => $cid, 'network' => $network, 'created' => Temporal::convert(), 'last' => Temporal::convert(), 'content' => $msg, 'batch' =>($batch) ? 1 : 0]);
+ DBA::insert('queue', [
+ 'cid' => $cid,
+ 'network' => $network,
+ 'guid' => $guid,
+ 'created' => DateTimeFormat::utcNow(),
+ 'last' => DateTimeFormat::utcNow(),
+ 'content' => $msg,
+ 'batch' =>($batch) ? 1 : 0
+ ]);
+ Logger::log('Added item ' . $guid . ' for ' . $cid);
}
}