dbqueuemanager.php 5.6 KB

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