DBQueueManager.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Simple-minded queue manager for storing items in the database
  18. *
  19. * @category QueueManager
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Brion Vibber <brion@status.net>
  23. * @copyright 2009-2010 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. class DBQueueManager extends QueueManager
  28. {
  29. public $groupsByTransport;
  30. /**
  31. * Saves an object reference into the queue item table.
  32. * @return bool true on success
  33. * @throws ServerException on failure
  34. */
  35. public function enqueue($object, $queue)
  36. {
  37. $qi = new Queue_item();
  38. $qi->frame = DB_DataObject_Cast::blob($this->encode($object));
  39. $qi->transport = $queue;
  40. $qi->created = common_sql_now();
  41. $result = $qi->insert();
  42. if ($result === false) {
  43. common_log_db_error($qi, 'INSERT', __FILE__);
  44. throw new ServerException('DB error inserting queue item');
  45. }
  46. $this->stats('enqueued', $queue);
  47. return true;
  48. }
  49. /**
  50. * Poll every 10 seconds for new events during idle periods.
  51. * We'll look in more often when there's data available.
  52. * Must be greater than 0 for the poll method to be called
  53. *
  54. * @return int seconds
  55. */
  56. public function pollInterval()
  57. {
  58. return 10;
  59. }
  60. /**
  61. * Run a polling cycle during idle processing in the input loop.
  62. * @return boolean true if we should poll again for more data immediately
  63. */
  64. public function poll(): bool
  65. {
  66. $this->_log(LOG_DEBUG, 'Checking for notices...');
  67. $qi = Queue_item::top($this->activeQueues(), $this->getIgnoredTransports());
  68. if (!$qi instanceof Queue_item) {
  69. //$this->_log(LOG_DEBUG, 'No notices waiting; idling.');
  70. return false;
  71. }
  72. try {
  73. $item = $this->decode($qi->frame);
  74. } catch (Exception $e) {
  75. $this->_log(LOG_INFO, "[{$qi->transport}] Discarding: "._ve($e->getMessage()));
  76. $this->_done($qi);
  77. return true;
  78. }
  79. $rep = $this->logrep($item);
  80. $this->_log(LOG_DEBUG, 'Got '._ve($rep).' for transport '._ve($qi->transport));
  81. try {
  82. $handler = $this->getHandler($qi->transport);
  83. $result = $handler->handle($item);
  84. } catch (NoQueueHandlerException $e) {
  85. $this->noHandlerFound($qi, $rep);
  86. return true;
  87. } catch (NoResultException $e) {
  88. $this->_log(LOG_ERR, "[{$qi->transport}:$rep] ".get_class($e).' thrown ('.
  89. _ve($e->getMessage()).'), ignoring queue_item '._ve($qi->getID()));
  90. $result = true;
  91. } catch (AlreadyFulfilledException $e) {
  92. $this->_log(LOG_ERR, "[{$qi->transport}:$rep] ".get_class($e).' thrown ('.
  93. _ve($e->getMessage()).'), ignoring queue_item '._ve($qi->getID()));
  94. $result = true;
  95. } catch (Exception $e) {
  96. $this->_log(LOG_ERR, "[{$qi->transport}:$rep] Exception (".
  97. get_class($e).') thrown: '._ve($e->getMessage()));
  98. $result = false;
  99. }
  100. if ($result) {
  101. $this->_log(LOG_INFO, "[{$qi->transport}:$rep] Successfully handled item");
  102. $this->_done($qi);
  103. } else {
  104. $this->_log(LOG_INFO, "[{$qi->transport}:$rep] Failed to handle item");
  105. $this->_fail($qi);
  106. }
  107. return true;
  108. }
  109. // What to do if no handler was found. For example, the OpportunisticQM
  110. // should avoid deleting items just because it can't reach XMPP queues etc.
  111. protected function noHandlerFound(Queue_item $qi, $rep = null)
  112. {
  113. $this->_log(LOG_INFO, "[{$qi->transport}:{$rep}] No handler for queue {$qi->transport}; discarding.");
  114. $this->_done($qi);
  115. }
  116. /**
  117. * Delete our claimed item from the queue after successful processing.
  118. *
  119. * @param QueueItem $qi
  120. */
  121. protected function _done(Queue_item $qi)
  122. {
  123. if (empty($qi->claimed)) {
  124. $this->_log(LOG_WARNING, "Reluctantly releasing unclaimed queue item {$qi->id} from {$qi->transport}");
  125. }
  126. $qi->delete();
  127. $this->stats('handled', $qi->transport);
  128. }
  129. /**
  130. * Free our claimed queue item for later reprocessing in case of
  131. * temporary failure.
  132. *
  133. * @param QueueItem $qi
  134. */
  135. protected function _fail(Queue_item $qi, $releaseOnly=false)
  136. {
  137. if (empty($qi->claimed)) {
  138. $this->_log(LOG_WARNING, "[{$qi->transport}:item {$qi->id}] Ignoring failure for unclaimed queue item");
  139. } else {
  140. $qi->releaseClaim();
  141. }
  142. if (!$releaseOnly) {
  143. $this->stats('error', $qi->transport);
  144. }
  145. }
  146. }