]> git.mxchange.org Git - friendica.git/blob - src/Worker/PubSubPublish.php
Fix to OStatus delivery to be not so blocking to other tasks
[friendica.git] / src / Worker / PubSubPublish.php
1 <?php
2 /**
3  * @file src/Worker/PubSubPublish.php
4  */
5
6 namespace Friendica\Worker;
7
8 use Friendica\App;
9 use Friendica\Core\System;
10 use Friendica\Core\Config;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBM;
13 use Friendica\Protocol\OStatus;
14 use Friendica\Util\Network;
15 use Friendica\Util\DateTimeFormat;
16 use dba;
17
18 require_once 'include/items.php';
19
20 class PubSubPublish {
21         public static function execute($pubsubpublish_id = 0)
22         {
23                 global $a;
24
25                 if ($pubsubpublish_id != 0) {
26                         self::publish($pubsubpublish_id);
27                         return;
28                 }
29
30                 // We'll push to each subscriber that has push > 0,
31                 // i.e. there has been an update (set in notifier.php).
32                 $subscribers = dba::select('push_subscriber', ['id', 'callback_url'], ["`push` > 0 AND `next_try` < UTC_TIMESTAMP()"]);
33
34                 while ($subscriber = dba::fetch($subscribers)) {
35                         logger("Publish feed to " . $subscriber["callback_url"], LOGGER_DEBUG);
36                         Worker::add(['priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true],
37                                         'PubSubPublish', (int)$subscriber["id"]);
38                 }
39
40                 dba::close($subscribers);
41         }
42
43         private static function publish($id) {
44                 global $a;
45
46                 $subscriber = dba::selectFirst('push_subscriber', [], ['id' => $id]);
47                 if (!DBM::is_result($subscriber)) {
48                         return;
49                 }
50
51                 /// @todo Check server status with PortableContact::checkServer()
52                 // Before this can be done we need a way to safely detect the server url.
53
54                 logger("Generate feed of user " . $subscriber['nickname']. " to " . $subscriber['callback_url']. " - last updated " . $subscriber['last_update'], LOGGER_DEBUG);
55
56                 $last_update = $subscriber['last_update'];
57                 $params = OStatus::feed($subscriber['nickname'], $last_update);
58
59                 if (!$params) {
60                         return;
61                 }
62
63                 $hmac_sig = hash_hmac("sha1", $params, $subscriber['secret']);
64
65                 $headers = ["Content-type: application/atom+xml",
66                                 sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
67                                         System::baseUrl() . '/pubsubhubbub/' . $subscriber['nickname'],
68                                         $subscriber['topic']),
69                                 "X-Hub-Signature: sha1=" . $hmac_sig];
70
71                 logger('POST ' . print_r($headers, true) . "\n" . $params, LOGGER_DATA);
72
73                 Network::post($subscriber['callback_url'], $params, $headers);
74                 $ret = $a->get_curl_code();
75
76                 $condition = ['id' => $subscriber['id']];
77
78                 if ($ret >= 200 && $ret <= 299) {
79                         logger('Successfully pushed to ' . $subscriber['callback_url']);
80
81                         // set last_update to the "created" date of the last item, and reset push=0
82                         $fields = ['push' => 0, 'next_try' => NULL_DATE, 'last_update' => $last_update];
83                         dba::update('push_subscriber', $fields, $condition);
84
85                 } else {
86                         logger('Delivery error when pushing to ' . $subscriber['callback_url'] . ' HTTP: ' . $ret);
87
88                         // we use the push variable also as a counter, if we failed we
89                         // increment this until some upper limit where we give up
90                         $retrial = $subscriber['push'];
91
92                         if ($retrial > 14) {
93                                 dba::update('push_subscriber', ['push' => 0, 'next_try' => NULL_DATE], $condition);
94                                 logger('Delivery error: Giving up for ' . $subscriber['callback_url'], LOGGER_DEBUG);
95                         } else {
96                                 // Calculate the delay until the next trial
97                                 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
98                                 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
99
100                                 $retrial = $retrial + 1;
101
102                                 dba::update('push_subscriber', ['push' => $retrial, 'next_try' => $next], $condition);
103                                 logger('Delivery error: Next try (' . $retrial . ') for ' . $subscriber['callback_url'] . ' at ' . $next, LOGGER_DEBUG);
104                         }
105                 }
106         }
107 }