DBQueueManager.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /**
  30. * Saves an object reference into the queue item table.
  31. * @return bool true on success
  32. * @throws ServerException on failure
  33. */
  34. public function enqueue($object, $queue)
  35. {
  36. $qi = new Queue_item();
  37. $qi->frame = DB_DataObject_Cast::blob($this->encode($object));
  38. $qi->transport = $queue;
  39. $qi->created = common_sql_now();
  40. $result = $qi->insert();
  41. if ($result === false) {
  42. common_log_db_error($qi, 'INSERT', __FILE__);
  43. throw new ServerException('DB error inserting queue item');
  44. }
  45. $this->stats('enqueued', $queue);
  46. return true;
  47. }
  48. /**
  49. * Poll every 10 seconds for new events during idle periods.
  50. * We'll look in more often when there's data available.
  51. * Must be greater than 0 for the poll method to be called
  52. *
  53. * @return int seconds
  54. */
  55. public function pollInterval()
  56. {
  57. return 10;
  58. }
  59. /**
  60. * Run a polling cycle during idle processing in the input loop.
  61. * @return boolean true if we should poll again for more data immediately
  62. */
  63. public function poll(): bool
  64. {
  65. $this->_log(LOG_DEBUG, 'Checking for notices...');
  66. $qi = Queue_item::top($this->activeQueues(), $this->getIgnoredTransports());
  67. if (!$qi instanceof Queue_item) {
  68. //$this->_log(LOG_DEBUG, 'No notices waiting; idling.');
  69. return false;
  70. }
  71. try {
  72. $item = $this->decode($qi->frame);
  73. } catch (Exception $e) {
  74. $this->_log(LOG_INFO, "[{$qi->transport}] Discarding: "._ve($e->getMessage()));
  75. $this->_done($qi);
  76. return true;
  77. }
  78. $rep = $this->logrep($item);
  79. $this->_log(LOG_DEBUG, 'Got '._ve($rep).' for transport '._ve($qi->transport));
  80. try {
  81. $handler = $this->getHandler($qi->transport);
  82. $result = $handler->handle($item);
  83. } catch (NoQueueHandlerException $e) {
  84. $this->noHandlerFound($qi, $rep);
  85. return true;
  86. } catch (NoResultException $e) {
  87. $this->_log(LOG_ERR, "[{$qi->transport}:$rep] ".get_class($e).' thrown ('.
  88. _ve($e->getMessage()).'), ignoring queue_item '._ve($qi->getID()));
  89. $result = true;
  90. } catch (AlreadyFulfilledException $e) {
  91. $this->_log(LOG_ERR, "[{$qi->transport}:$rep] ".get_class($e).' thrown ('.
  92. _ve($e->getMessage()).'), ignoring queue_item '._ve($qi->getID()));
  93. $result = true;
  94. } catch (Exception $e) {
  95. $this->_log(LOG_ERR, "[{$qi->transport}:$rep] Exception (".
  96. get_class($e).') thrown: '._ve($e->getMessage()));
  97. $result = false;
  98. }
  99. if ($result) {
  100. $this->_log(LOG_INFO, "[{$qi->transport}:$rep] Successfully handled item");
  101. $this->_done($qi);
  102. } else {
  103. $this->_log(LOG_INFO, "[{$qi->transport}:$rep] Failed to handle item");
  104. $this->_fail($qi);
  105. }
  106. return true;
  107. }
  108. // What to do if no handler was found. For example, the OpportunisticQM
  109. // should avoid deleting items just because it can't reach XMPP queues etc.
  110. protected function noHandlerFound(Queue_item $qi, $rep = null)
  111. {
  112. $this->_log(LOG_INFO, "[{$qi->transport}:{$rep}] No handler for queue {$qi->transport}; discarding.");
  113. $this->_done($qi);
  114. }
  115. /**
  116. * Delete our claimed item from the queue after successful processing.
  117. *
  118. * @param QueueItem $qi
  119. */
  120. protected function _done(Queue_item $qi)
  121. {
  122. if (empty($qi->claimed)) {
  123. $this->_log(LOG_WARNING, "Reluctantly releasing unclaimed queue item {$qi->id} from {$qi->transport}");
  124. }
  125. $qi->delete();
  126. $this->stats('handled', $qi->transport);
  127. }
  128. /**
  129. * Free our claimed queue item for later reprocessing in case of
  130. * temporary failure.
  131. *
  132. * @param QueueItem $qi
  133. */
  134. protected function _fail(Queue_item $qi, $releaseOnly=false)
  135. {
  136. if (empty($qi->claimed)) {
  137. $this->_log(LOG_WARNING, "[{$qi->transport}:item {$qi->id}] Ignoring failure for unclaimed queue item");
  138. } else {
  139. $qi->releaseClaim();
  140. }
  141. if (!$releaseOnly) {
  142. $this->stats('error', $qi->transport);
  143. }
  144. }
  145. }