dbqueuemanager.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 boolean 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. *
  52. * @return int seconds
  53. */
  54. public function pollInterval()
  55. {
  56. return 10;
  57. }
  58. /**
  59. * Run a polling cycle during idle processing in the input loop.
  60. * @return boolean true if we should poll again for more data immediately
  61. */
  62. public function poll()
  63. {
  64. //$this->_log(LOG_DEBUG, 'Checking for notices...');
  65. $qi = Queue_item::top($this->activeQueues(), $this->getIgnoredTransports());
  66. if (!$qi instanceof Queue_item) {
  67. //$this->_log(LOG_DEBUG, 'No notices waiting; idling.');
  68. return false;
  69. }
  70. try {
  71. $item = $this->decode($qi->frame);
  72. } catch (Exception $e) {
  73. $this->_log(LOG_INFO, "[{$qi->transport}] Discarding: "._ve($e->getMessage()));
  74. $this->_done($qi);
  75. return true;
  76. }
  77. $rep = $this->logrep($item);
  78. $this->_log(LOG_DEBUG, 'Got '._ve($rep).' for transport '._ve($qi->transport));
  79. try {
  80. $handler = $this->getHandler($qi->transport);
  81. $result = $handler->handle($item);
  82. } catch (NoQueueHandlerException $e) {
  83. $this->noHandlerFound($qi, $rep);
  84. return true;
  85. } catch (NoResultException $e) {
  86. $this->_log(LOG_ERR, "[{$qi->transport}:$rep] ".get_class($e).' thrown ('._ve($e->getMessage()).'), ignoring queue_item '._ve($qi->getID()));
  87. $result = true;
  88. } catch (AlreadyFulfilledException $e) {
  89. $this->_log(LOG_ERR, "[{$qi->transport}:$rep] ".get_class($e).' thrown ('._ve($e->getMessage()).'), ignoring queue_item '._ve($qi->getID()));
  90. $result = true;
  91. } catch (Exception $e) {
  92. $this->_log(LOG_ERR, "[{$qi->transport}:$rep] Exception (".get_class($e).') thrown: '._ve($e->getMessage()));
  93. $result = false;
  94. }
  95. if ($result) {
  96. $this->_log(LOG_INFO, "[{$qi->transport}:$rep] Successfully handled item");
  97. $this->_done($qi);
  98. } else {
  99. $this->_log(LOG_INFO, "[{$qi->transport}:$rep] Failed to handle item");
  100. $this->_fail($qi);
  101. }
  102. return true;
  103. }
  104. // What to do if no handler was found. For example, the OpportunisticQM
  105. // should avoid deleting items just because it can't reach XMPP queues etc.
  106. protected function noHandlerFound(Queue_item $qi, $rep=null)
  107. {
  108. $this->_log(LOG_INFO, "[{$qi->transport}:{$rep}] No handler for queue {$qi->transport}; discarding.");
  109. $this->_done($qi);
  110. }
  111. /**
  112. * Delete our claimed item from the queue after successful processing.
  113. *
  114. * @param QueueItem $qi
  115. */
  116. protected function _done(Queue_item $qi)
  117. {
  118. if (empty($qi->claimed)) {
  119. $this->_log(LOG_WARNING, "Reluctantly releasing unclaimed queue item {$qi->id} from {$qi->transport}");
  120. }
  121. $qi->delete();
  122. $this->stats('handled', $qi->transport);
  123. }
  124. /**
  125. * Free our claimed queue item for later reprocessing in case of
  126. * temporary failure.
  127. *
  128. * @param QueueItem $qi
  129. */
  130. protected function _fail(Queue_item $qi, $releaseOnly=false)
  131. {
  132. if (empty($qi->claimed)) {
  133. $this->_log(LOG_WARNING, "[{$qi->transport}:item {$qi->id}] Ignoring failure for unclaimed queue item");
  134. } else {
  135. $qi->releaseClaim();
  136. }
  137. if (!$releaseOnly) {
  138. $this->stats('error', $qi->transport);
  139. }
  140. }
  141. }