hubprepqueuehandler.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, 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('STATUSNET')) {
  20. exit(1);
  21. }
  22. /**
  23. * When we have a large batch of WebSub consumers, we break the data set
  24. * into smaller chunks. Enqueue final destinations...
  25. *
  26. * @package Hub
  27. * @author Brion Vibber <brion@status.net>
  28. */
  29. class HubPrepQueueHandler extends QueueHandler
  30. {
  31. // Enqueue this many low-level distributions before re-queueing the rest
  32. // of the batch to be processed later. Helps to keep latency down for other
  33. // things happening during a particularly long OStatus delivery session.
  34. //
  35. // [Could probably ditch this if we had working message delivery priorities
  36. // for queueing, but this isn't supported in ActiveMQ 5.3.]
  37. const ROLLING_BATCH = 20;
  38. function transport()
  39. {
  40. return 'hubprep';
  41. }
  42. function handle($data)
  43. {
  44. $topic = $data['topic'];
  45. $atom = $data['atom'];
  46. $pushCallbacks = $data['pushCallbacks'];
  47. assert(is_string($atom));
  48. assert(is_string($topic));
  49. assert(is_array($pushCallbacks));
  50. // Set up distribution for the first n subscribing sites...
  51. // If we encounter an uncatchable error, queue handling should
  52. // automatically re-run the batch, which could lead to some dupe
  53. // distributions.
  54. //
  55. // Worst case is if one of these hubprep entries dies too many
  56. // times and gets dropped; the rest of the batch won't get processed.
  57. try {
  58. $n = 0;
  59. while (count($pushCallbacks) && $n < self::ROLLING_BATCH) {
  60. $n++;
  61. $callback = array_shift($pushCallbacks);
  62. $sub = HubSub::getByHashkey($topic, $callback);
  63. if (!$sub) {
  64. common_log(LOG_ERR, "Skipping WebSub delivery for deleted(?) consumer $callback on $topic");
  65. continue;
  66. }
  67. $sub->distribute($atom);
  68. }
  69. } catch (Exception $e) {
  70. common_log(LOG_ERR, "Exception during WebSub batch out: " .
  71. $e->getMessage() .
  72. " prepping $topic to $callback");
  73. }
  74. // And re-queue the rest of the batch!
  75. if (count($pushCallbacks) > 0) {
  76. $sub = new HubSub();
  77. $sub->topic = $topic;
  78. $sub->bulkDistribute($atom, $pushCallbacks);
  79. }
  80. return true;
  81. }
  82. }