distribqueuehandler.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
  20. /**
  21. * Base class for queue handlers.
  22. *
  23. * As extensions of the Daemon class, each queue handler has the ability
  24. * to launch itself in the background, at which point it'll pass control
  25. * to the configured QueueManager class to poll for updates.
  26. *
  27. * Subclasses must override at least the following methods:
  28. * - transport
  29. * - handle_notice
  30. */
  31. class DistribQueueHandler
  32. {
  33. public $widgetOpts;
  34. public $scoped;
  35. /**
  36. * Return transport keyword which identifies items this queue handler
  37. * services; must be defined for all subclasses.
  38. *
  39. * Must be 8 characters or less to fit in the queue_item database.
  40. * ex "email", "jabber", "sms", "irc", ...
  41. *
  42. * @return string
  43. */
  44. public function transport()
  45. {
  46. return 'distrib';
  47. }
  48. /**
  49. * Handle distribution of a notice after we've saved it:
  50. * @li add to local recipient inboxes
  51. * @li send email notifications to local @-reply targets
  52. * @li run final EndNoticeSave plugin events
  53. * @li put any remaining post-processing into the queues
  54. *
  55. * If this function indicates failure, a warning will be logged
  56. * and the item is placed back in the queue to be re-run.
  57. *
  58. * @param Notice $notice
  59. * @return boolean true on success, false on failure
  60. */
  61. public function handle(Notice $notice) : bool
  62. {
  63. // We have to manually add attentions to non-profile subs and non-mentions
  64. $ptAtts = $notice->getAttentionsFromProfileTags();
  65. foreach (array_keys($ptAtts) as $profile_id) {
  66. $profile = Profile::getKV('id', $profile_id);
  67. if ($profile instanceof Profile) {
  68. try {
  69. common_debug('Adding Attention for '.$notice->getID().' profile '.$profile->getID());
  70. Attention::saveNew($notice, $profile);
  71. } catch (Exception $e) {
  72. $this->logit($notice, $e);
  73. }
  74. }
  75. }
  76. try {
  77. $notice->sendReplyNotifications();
  78. } catch (Exception $e) {
  79. $this->logit($notice, $e);
  80. }
  81. try {
  82. Event::handle('EndNoticeDistribute', array($notice));
  83. } catch (Exception $e) {
  84. $this->logit($notice, $e);
  85. }
  86. try {
  87. Event::handle('EndNoticeSave', array($notice));
  88. } catch (Exception $e) {
  89. $this->logit($notice, $e);
  90. }
  91. try {
  92. // Enqueue for other handlers
  93. common_enqueue_notice($notice);
  94. } catch (Exception $e) {
  95. $this->logit($notice, $e);
  96. }
  97. return true;
  98. }
  99. protected function logit($notice, $e)
  100. {
  101. common_log(LOG_ERR, "Distrib queue exception saving notice $notice->id: " .
  102. $e->getMessage() . ' ' .
  103. str_replace("\n", " ", $e->getTraceAsString()));
  104. // We'll still return true so we don't get stuck in a loop
  105. // trying to run a bad insert over and over...
  106. }
  107. }