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