distribqueuehandler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /**
  34. * Return transport keyword which identifies items this queue handler
  35. * services; must be defined for all subclasses.
  36. *
  37. * Must be 8 characters or less to fit in the queue_item database.
  38. * ex "email", "jabber", "sms", "irc", ...
  39. *
  40. * @return string
  41. */
  42. public function transport()
  43. {
  44. return 'distrib';
  45. }
  46. /**
  47. * Handle distribution of a notice after we've saved it:
  48. * @li add to local recipient inboxes
  49. * @li send email notifications to local @-reply targets
  50. * @li run final EndNoticeSave plugin events
  51. * @li put any remaining post-processing into the queues
  52. *
  53. * If this function indicates failure, a warning will be logged
  54. * and the item is placed back in the queue to be re-run.
  55. *
  56. * @param Notice $notice
  57. * @return boolean true on success, false on failure
  58. */
  59. public function handle(Notice $notice)
  60. {
  61. // We have to manually add attentions to non-profile subs and non-mentions
  62. $ptAtts = $notice->getAttentionsFromProfileTags();
  63. foreach (array_keys($ptAtts) as $profile_id) {
  64. $profile = Profile::getKV('id', $profile_id);
  65. if ($profile instanceof Profile) {
  66. try {
  67. common_debug('Adding Attention for '.$notice->getID().' profile '.$profile->getID());
  68. Attention::saveNew($notice, $profile);
  69. } catch (Exception $e) {
  70. $this->logit($notice, $e);
  71. }
  72. }
  73. }
  74. try {
  75. $notice->sendReplyNotifications();
  76. } catch (Exception $e) {
  77. $this->logit($notice, $e);
  78. }
  79. try {
  80. Event::handle('EndNoticeDistribute', array($notice));
  81. } catch (Exception $e) {
  82. $this->logit($notice, $e);
  83. }
  84. try {
  85. Event::handle('EndNoticeSave', array($notice));
  86. } catch (Exception $e) {
  87. $this->logit($notice, $e);
  88. }
  89. try {
  90. // Enqueue for other handlers
  91. common_enqueue_notice($notice);
  92. } catch (Exception $e) {
  93. $this->logit($notice, $e);
  94. }
  95. return true;
  96. }
  97. protected function logit($notice, $e)
  98. {
  99. common_log(LOG_ERR, "Distrib queue exception saving notice $notice->id: " .
  100. $e->getMessage() . ' ' .
  101. str_replace("\n", " ", $e->getTraceAsString()));
  102. // We'll still return true so we don't get stuck in a loop
  103. // trying to run a bad insert over and over...
  104. }
  105. }