Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.1KB

  1. <?php
  2. /**
  3. * @file src/Model/Queue.php
  4. */
  5. namespace Friendica\Model;
  6. use Friendica\Core\Config;
  7. use Friendica\Core\Logger;
  8. use Friendica\Database\DBA;
  9. use Friendica\Util\DateTimeFormat;
  10. class Queue
  11. {
  12. /**
  13. * @param string $id id
  14. */
  15. public static function updateTime($id)
  16. {
  17. Logger::log('queue: requeue item ' . $id);
  18. $queue = DBA::selectFirst('queue', ['retrial'], ['id' => $id]);
  19. if (!DBA::isResult($queue)) {
  20. return;
  21. }
  22. $retrial = $queue['retrial'];
  23. if ($retrial > 14) {
  24. self::removeItem($id);
  25. }
  26. // Calculate the delay until the next trial
  27. $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
  28. $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
  29. DBA::update('queue', ['last' => DateTimeFormat::utcNow(), 'retrial' => $retrial + 1, 'next' => $next], ['id' => $id]);
  30. }
  31. /**
  32. * @param string $id id
  33. */
  34. public static function removeItem($id)
  35. {
  36. Logger::log('queue: remove queue item ' . $id);
  37. DBA::delete('queue', ['id' => $id]);
  38. }
  39. /**
  40. * @brief Checks if the communication with a given contact had problems recently
  41. *
  42. * @param int $cid Contact id
  43. *
  44. * @return bool The communication with this contact has currently problems
  45. */
  46. public static function wasDelayed($cid)
  47. {
  48. // Are there queue entries that were recently added?
  49. $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d
  50. AND `last` > UTC_TIMESTAMP() - INTERVAL 15 MINUTE LIMIT 1",
  51. intval($cid)
  52. );
  53. $was_delayed = DBA::isResult($r);
  54. // We set "term-date" to a current date if the communication has problems.
  55. // If the communication works again we reset this value.
  56. if ($was_delayed) {
  57. $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` <= '1000-01-01' LIMIT 1",
  58. intval($cid)
  59. );
  60. $was_delayed = !DBA::isResult($r);
  61. }
  62. return $was_delayed;
  63. }
  64. /**
  65. * @param string $cid cid
  66. * @param string $network network
  67. * @param string $msg message
  68. * @param boolean $batch batch, default false
  69. */
  70. public static function add($cid, $network, $msg, $batch = false, $guid = '')
  71. {
  72. $max_queue = Config::get('system', 'max_contact_queue');
  73. if ($max_queue < 1) {
  74. $max_queue = 500;
  75. }
  76. $batch_queue = Config::get('system', 'max_batch_queue');
  77. if ($batch_queue < 1) {
  78. $batch_queue = 1000;
  79. }
  80. $r = q("SELECT COUNT(*) AS `total` FROM `queue` INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
  81. WHERE `queue`.`cid` = %d AND `contact`.`self` = 0 ",
  82. intval($cid)
  83. );
  84. if (DBA::isResult($r)) {
  85. if ($batch && ($r[0]['total'] > $batch_queue)) {
  86. Logger::log('too many queued items for batch server ' . $cid . ' - discarding message');
  87. return;
  88. } elseif ((! $batch) && ($r[0]['total'] > $max_queue)) {
  89. Logger::log('too many queued items for contact ' . $cid . ' - discarding message');
  90. return;
  91. }
  92. }
  93. DBA::insert('queue', [
  94. 'cid' => $cid,
  95. 'network' => $network,
  96. 'guid' => $guid,
  97. 'created' => DateTimeFormat::utcNow(),
  98. 'last' => DateTimeFormat::utcNow(),
  99. 'content' => $msg,
  100. 'batch' =>($batch) ? 1 : 0
  101. ]);
  102. Logger::log('Added item ' . $guid . ' for ' . $cid);
  103. }
  104. }