]> git.mxchange.org Git - friendica.git/blobdiff - include/notifier.php
Merge remote-tracking branch 'friendika/master' into newui
[friendica.git] / include / notifier.php
index b87aa95b1559c2f1e20d9ad31ea78e2efd95b50c..6ac882c1992cf96511901f3c6f54033444aaffc2 100644 (file)
@@ -2,6 +2,20 @@
 
 require_once("boot.php");
 
+/*
+ * This file was at one time responsible for doing all deliveries, but this caused
+ * big problems on shared hosting systems, where the process might get killed by the 
+ * hosting provider and nothing would get delivered. 
+ * It now only delivers one message under certain cases, and invokes a queued
+ * delivery mechanism (include/deliver.php) to deliver individual contacts at 
+ * controlled intervals.
+ * This has a much better chance of surviving random processes getting killed
+ * by the hosting provider. 
+ * A lot of this code is duplicated in include/deliver.php until we have time to go back
+ * and re-structure the delivery procedure based on the obstacles that have been thrown at 
+ * us by hosting providers. 
+ */
+
 function notifier_run($argv, $argc){
        global $a, $db;
 
@@ -125,8 +139,11 @@ function notifier_run($argv, $argc){
                                $item['deleted'] = 1;
                }
 
-               if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
+               if((count($items) == 1) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
+                       logger('notifier: top level post');
                        $top_level = true;
+               }
+
        }
 
        $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`, 
@@ -142,6 +159,8 @@ function notifier_run($argv, $argc){
 
        $owner = $r[0];
 
+       $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
+
        $hub = get_config('system','huburl');
 
        // If this is a public conversation, notify the feed hub
@@ -379,11 +398,25 @@ function notifier_run($argv, $argc){
                dbesc($recip_str)
        );
 
-       // delivery loop
 
        require_once('include/salmon.php');
 
+       $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
+
+       // delivery loop
+
        if(count($r)) {
+
+               foreach($r as $contact) {
+                       if((! $mail) && (! $fsuggest) && (! $followup) && (! $contact['self'])) {
+                               q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
+                                       dbesc($cmd),
+                                       intval($item_id),
+                                       intval($contact['id'])
+                               );
+                       }
+               }
+
                foreach($r as $contact) {
                        if($contact['self'])
                                continue;
@@ -392,12 +425,9 @@ function notifier_run($argv, $argc){
                        // we will deliver single recipient types of message and email receipients here. 
 
                        if((! $mail) && (! $fsuggest) && (! $followup)) {
-                               $interval = intval(get_config('system','delivery_interval'));
-                               if(! $interval)
-                                       $interval = 2;
-
                                proc_run('php','include/delivery.php',$cmd,$item_id,$contact['id']);
-                               sleep($interval);
+                               if($interval)
+                                       @time_sleep_until(microtime(true) + (float) $interval);
                                continue;
                        }
 
@@ -578,7 +608,8 @@ function notifier_run($argv, $argc){
                                                diaspora_send_relay($target_item,$owner,$contact);
                                                break;
                                        }               
-                                       elseif($top_level) {
+                                       elseif(($top_level) && (! $walltowall)) {
+                                               // currently no workable solution for sending walltowall
                                                diaspora_send_status($target_item,$owner,$contact);
                                                break;
                                        }
@@ -624,6 +655,18 @@ function notifier_run($argv, $argc){
                if(count($r)) {
                        logger('pubdeliver: ' . print_r($r,true));
 
+                       // throw everything into the queue in case we get killed
+
+                       foreach($r as $rr) {
+                               if((! $mail) && (! $fsuggest) && (! $followup)) {
+                                       q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )",
+                                               dbesc($cmd),
+                                               intval($item_id),
+                                               intval($rr['id'])
+                                       );
+                               }
+                       }
+
                        foreach($r as $rr) {
 
                                /* Don't deliver to folks who have already been delivered to */
@@ -634,13 +677,10 @@ function notifier_run($argv, $argc){
                                }
 
                                if((! $mail) && (! $fsuggest) && (! $followup)) {
-                                       $interval = intval(get_config('system','delivery_interval'));
-                                       if(! $interval)
-                                               $interval = 2;
-
+                                       logger('notifier: delivery agent: ' . $rr['name'] . ' ' . $rr['id']); 
                                        proc_run('php','include/delivery.php',$cmd,$item_id,$rr['id']);
-                                       sleep($interval);
-                                       continue;
+                                       if($interval)
+                                               @time_sleep_until(microtime(true) + (float) $interval);
                                }
                        }
                }