]> git.mxchange.org Git - friendica.git/blob - include/queue.php
Cron/Queue: Setting a limit to avoid endless PHP jobs.
[friendica.git] / include / queue.php
1 <?php
2 require_once("boot.php");
3 require_once('include/queue_fn.php');
4
5 function handle_pubsubhubbub() {
6         global $a, $db;
7
8         logger('queue [pubsubhubbub]: start');
9
10         // We'll push to each subscriber that has push > 0,
11         // i.e. there has been an update (set in notifier.php).
12
13         $r = q("SELECT * FROM `push_subscriber` WHERE `push` > 0");
14
15         foreach($r as $rr) {
16                 $params = get_feed_for($a, '', $rr['nickname'], $rr['last_update']);
17                 $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
18
19                 $headers = array("Content-type: application/atom+xml",
20                                                  sprintf("Link: <%s>;rel=hub," .
21                                                                  "<%s>;rel=self",
22                                                                  $a->get_baseurl() . '/pubsubhubbub',
23                                                                  $rr['topic']),
24                                                  "X-Hub-Signature: sha1=" . $hmac_sig);
25
26                 logger('queue [pubsubhubbub]: POST', $headers);
27
28                 post_url($rr['callback_url'], $params, $headers);
29                 $ret = $a->get_curl_code();
30
31                 if ($ret >= 200 && $ret <= 299) {
32                         logger('queue [pubsubhubbub]: successfully pushed to ' .
33                                    $rr['callback_url']);
34
35                         // set last_update to "now", and reset push=0
36                         $date_now = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
37                         q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' " .
38                           "WHERE id = %d",
39                           dbesc($date_now),
40                           intval($rr['id']));
41
42                 } else {
43                         logger('queue [pubsubhubbub]: error when pushing to ' .
44                                    $rr['callback_url'] . 'HTTP: ', $ret);
45
46                         // we use the push variable also as a counter, if we failed we
47                         // increment this until some upper limit where we give up
48                         $new_push = intval($rr['push']) + 1;
49
50                         if ($new_push > 30) // OK, let's give up
51                                 $new_push = 0;
52
53                         q("UPDATE `push_subscriber` SET `push` = %d, last_update = '%s' " .
54                           "WHERE id = %d",
55                           $new_push,
56                           dbesc($date_now),
57                           intval($rr['id']));
58                 }
59         }
60 }
61
62
63 function queue_run(&$argv, &$argc){
64         global $a, $db;
65
66         if(is_null($a)){
67                 $a = new App;
68         }
69
70         if(is_null($db)){
71                 @include(".htconfig.php");
72                 require_once("include/dba.php");
73                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
74                 unset($db_host, $db_user, $db_pass, $db_data);
75         };
76
77
78         require_once("include/session.php");
79         require_once("include/datetime.php");
80         require_once('include/items.php');
81         require_once('include/bbcode.php');
82         require_once('include/pidfile.php');
83
84         load_config('config');
85         load_config('system');
86
87         $lockpath = get_config('system','lockpath');
88         if ($lockpath != '') {
89                 $pidfile = new pidfile($lockpath, 'queue.lck');
90                 if($pidfile->is_already_running()) {
91                         logger("queue: Already running");
92                         return;
93                 }
94         }
95
96         $a->set_baseurl(get_config('system','url'));
97
98         load_hooks();
99
100         if($argc > 1)
101                 $queue_id = intval($argv[1]);
102         else
103                 $queue_id = 0;
104
105         $deadguys = array();
106
107         logger('queue: start');
108
109         set_time_limit(9*60*60); // Setting the maximum execution time for queue job to 9 minutes.
110
111         handle_pubsubhubbub();
112
113         $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
114
115         $r = q("select * from deliverq where 1");
116         if($r) {
117                 foreach($r as $rr) {
118                         logger('queue: deliverq');
119                         proc_run('php','include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
120                         if($interval)
121                                 @time_sleep_until(microtime(true) + (float) $interval);
122                 }
123         }
124
125         $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue` 
126                 INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id` 
127                 WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
128         if($r) {
129                 foreach($r as $rr) {
130                         logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
131                         logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
132                 }
133                 q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
134         }
135
136         if($queue_id) {
137                 $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
138                         intval($queue_id)
139                 );
140         }
141         else {
142
143                 // For the first 12 hours we'll try to deliver every 15 minutes
144                 // After that, we'll only attempt delivery once per hour. 
145
146                 $r = q("SELECT `id` FROM `queue` WHERE (( `created` > UTC_TIMESTAMP() - INTERVAL 12 HOUR && `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ) OR ( `last` < UTC_TIMESTAMP() - INTERVAL 1 HOUR ))");
147         }
148         if(! $r){
149                 return;
150         }
151
152         if(! $queue_id)
153                 call_hooks('queue_predeliver', $a, $r);
154
155
156         // delivery loop
157
158         require_once('include/salmon.php');
159         require_once('include/diaspora.php');
160
161         foreach($r as $q_item) {
162
163                 // queue_predeliver hooks may have changed the queue db details, 
164                 // so check again if this entry still needs processing
165
166                 if($queue_id) {
167                         $qi = q("select * from queue where `id` = %d limit 1",
168                                 intval($queue_id)
169                         );
170                 }
171                 else {
172                         $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
173                                 intval($q_item['id'])
174                         );
175                 }
176                 if(! count($qi))
177                         continue;
178
179
180                 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
181                         intval($qi[0]['cid'])
182                 );
183                 if(! count($c)) {
184                         remove_queue_item($q_item['id']);
185                         continue;
186                 }
187                 if(in_array($c[0]['notify'],$deadguys)) {
188                                 logger('queue: skipping known dead url: ' . $c[0]['notify']);
189                                 update_queue_time($q_item['id']);
190                                 continue;
191                 }
192
193                 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey` 
194                         FROM `user` WHERE `uid` = %d LIMIT 1",
195                         intval($c[0]['uid'])
196                 );
197                 if(! count($u)) {
198                         remove_queue_item($q_item['id']);
199                         continue;
200                 }
201
202                 $data      = $qi[0]['content'];
203                 $public    = $qi[0]['batch'];
204                 $contact   = $c[0];
205                 $owner     = $u[0];
206
207                 $deliver_status = 0;
208
209                 switch($contact['network']) {
210                         case NETWORK_DFRN:
211                                 logger('queue: dfrndelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
212                                 $deliver_status = dfrn_deliver($owner,$contact,$data);
213
214                                 if($deliver_status == (-1)) {
215                                         update_queue_time($q_item['id']);
216                                         $deadguys[] = $contact['notify'];
217                                 }
218                                 else {
219                                         remove_queue_item($q_item['id']);
220                                 }
221                                 break;
222                         case NETWORK_OSTATUS:
223                                 if($contact['notify']) {
224                                         logger('queue: slapdelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
225                                         $deliver_status = slapper($owner,$contact['notify'],$data);
226
227                                         if($deliver_status == (-1))
228                                                 update_queue_time($q_item['id']);
229                                         else
230                                                 remove_queue_item($q_item['id']);
231                                 }
232                                 break;
233                         case NETWORK_DIASPORA:
234                                 if($contact['notify']) {
235                                         logger('queue: diaspora_delivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
236                                         $deliver_status = diaspora_transmit($owner,$contact,$data,$public,true);
237
238                                         if($deliver_status == (-1))
239                                                 update_queue_time($q_item['id']);
240                                         else
241                                                 remove_queue_item($q_item['id']);
242                                 }
243                                 break;
244
245                         default:
246                                 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
247                                 call_hooks('queue_deliver', $a, $params);
248
249                                 if($params['result'])
250                                                 remove_queue_item($q_item['id']);
251                                 else
252                                                 update_queue_time($q_item['id']);
253
254                                 break;
255
256                 }
257         }
258
259         return;
260
261 }
262
263 if (array_search(__file__,get_included_files())===0){
264   queue_run($argv,$argc);
265   killme();
266 }