huboutqueuehandler.php 3.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
/*
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2010, StatusNet, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('GNUSOCIAL')) { exit(1); }
21

22
/**
mmn's avatar
mmn committed
23
 * Send a raw WebSub push atom update from our internal hub.
24 25 26 27 28 29 30 31 32 33 34 35
 * @package Hub
 * @author Brion Vibber <brion@status.net>
 */
class HubOutQueueHandler extends QueueHandler
{
    function transport()
    {
        return 'hubout';
    }

    function handle($data)
    {
36 37
        assert(array_key_exists('atom', $data));
        assert(is_string($data['atom']));
38 39
        $atom = $data['atom'];

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        assert(array_key_exists('retries', $data));
        $retries = intval($data['retries']);

        if (array_key_exists('topic', $data) && array_key_exists('callback', $data)) {
            assert(is_string($data['topic']));
            assert(is_string($data['callback']));

            $sub = HubSub::getByHashkey($data['topic'], $data['callback']);
        } elseif (array_key_exists('sub', $data)) {
            // queue behaviour changed 2017-07-09 to store topic/callback instead of sub object
            common_debug('Legacy behaviour of storing HubSub objects found, this should go away when all objects are handled...');
            $sub = $data['sub'];
        } else {
            throw new ServerException('No HubSub object available with queue item data.');
        }
55 56 57
        assert($sub instanceof HubSub);

        try {
58 59 60
            $success = $sub->push($atom);
            // The reason I split these up is because I want to see how the algorithm acts in practice.
            if ($success) {
61
                common_debug('WebSub push completed successfully!');
62
            } else {
63
                common_debug('WebSub push failed with an HTTP error.');
64 65
            }
            if ($sub->getErrors()>0) {
66
                common_debug('Resetting WebSub push error count following successful reset.');
67 68
                $sub->resetErrors();
            }
69
        } catch (AlreadyFulfilledException $e) {
70
            // Probably doesn't happen anymore since commit 74a60ab963b5ce1ed95bd81f935a44c573cd0264
mmn's avatar
mmn committed
71
            common_log(LOG_INFO, "Failed WebSub push to $sub->callback for $sub->topic (".get_class($e)."): " . $e->getMessage());
72
        } catch (Exception $e) {
Brion Vibber's avatar
Brion Vibber committed
73
            $retries--;
mmn's avatar
mmn committed
74
            $msg = "Failed WebSub push to $sub->callback for $sub->topic (".get_class($e)."): " . $e->getMessage();
Brion Vibber's avatar
Brion Vibber committed
75
            if ($retries > 0) {
mmn's avatar
mmn committed
76
                common_log(LOG_INFO, "$msg; scheduling for $retries more tries");
Brion Vibber's avatar
Brion Vibber committed
77 78 79 80 81

                // @fixme when we have infrastructure to schedule a retry
                // after a delay, use it.
                $sub->distribute($atom, $retries);
            } else {
82
                $sub->incrementErrors($e->getMessage());
Brion Vibber's avatar
Brion Vibber committed
83 84
                common_log(LOG_ERR, "$msg; discarding");
            }
85 86 87 88 89
        }

        return true;
    }
}