huboutqueuehandler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * Send a raw WebSub push atom update from our internal hub.
  22. * @package Hub
  23. * @author Brion Vibber <brion@status.net>
  24. */
  25. class HubOutQueueHandler extends QueueHandler
  26. {
  27. function transport()
  28. {
  29. return 'hubout';
  30. }
  31. function handle($data)
  32. {
  33. assert(array_key_exists('atom', $data));
  34. assert(is_string($data['atom']));
  35. $atom = $data['atom'];
  36. assert(array_key_exists('retries', $data));
  37. $retries = intval($data['retries']);
  38. if (array_key_exists('topic', $data) && array_key_exists('callback', $data)) {
  39. assert(is_string($data['topic']));
  40. assert(is_string($data['callback']));
  41. $sub = HubSub::getByHashkey($data['topic'], $data['callback']);
  42. } elseif (array_key_exists('sub', $data)) {
  43. // queue behaviour changed 2017-07-09 to store topic/callback instead of sub object
  44. common_debug('Legacy behaviour of storing HubSub objects found, this should go away when all objects are handled...');
  45. $sub = $data['sub'];
  46. } else {
  47. throw new ServerException('No HubSub object available with queue item data.');
  48. }
  49. assert($sub instanceof HubSub);
  50. try {
  51. $success = $sub->push($atom);
  52. // The reason I split these up is because I want to see how the algorithm acts in practice.
  53. if ($success) {
  54. common_debug('WebSub push completed successfully!');
  55. } else {
  56. common_debug('WebSub push failed with an HTTP error.');
  57. }
  58. if ($sub->getErrors()>0) {
  59. common_debug('Resetting WebSub push error count following successful reset.');
  60. $sub->resetErrors();
  61. }
  62. } catch (AlreadyFulfilledException $e) {
  63. // Probably doesn't happen anymore since commit 74a60ab963b5ce1ed95bd81f935a44c573cd0264
  64. common_log(LOG_INFO, "Failed WebSub push to $sub->callback for $sub->topic (".get_class($e)."): " . $e->getMessage());
  65. } catch (Exception $e) {
  66. $retries--;
  67. $msg = "Failed WebSub push to $sub->callback for $sub->topic (".get_class($e)."): " . $e->getMessage();
  68. if ($retries > 0) {
  69. common_log(LOG_INFO, "$msg; scheduling for $retries more tries");
  70. // @fixme when we have infrastructure to schedule a retry
  71. // after a delay, use it.
  72. $sub->distribute($atom, $retries);
  73. } else {
  74. $sub->incrementErrors($e->getMessage());
  75. common_log(LOG_ERR, "$msg; discarding");
  76. }
  77. }
  78. return true;
  79. }
  80. }