]> git.mxchange.org Git - friendica.git/commitdiff
allow polling to mostly survive minor memory shortages.
authorFriendika <info@friendika.com>
Fri, 1 Jul 2011 04:56:07 +0000 (21:56 -0700)
committerFriendika <info@friendika.com>
Fri, 1 Jul 2011 04:56:07 +0000 (21:56 -0700)
addon/facebook/facebook.php
boot.php
include/poller.php

index 49f193804325c95585e4aec38a35187a945bfe07..c54d5b5f09b116064680fccbac11871d413745c2 100644 (file)
@@ -376,9 +376,12 @@ function facebook_cron($a,$b) {
 
        logger('facebook_cron');
 
-       set_config('facebook','last_poll', time());
 
-       $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'facebook' AND `k` = 'post' AND `v` = '1' ");
+       // Find the FB users on this site and randomize in case one of them
+       // uses an obscene amount of memory. It may kill this queue run
+       // but hopefully we'll get a few others through on each run. 
+
+       $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'facebook' AND `k` = 'post' AND `v` = '1' ORDER BY RAND() ");
        if(count($r)) {
                foreach($r as $rr) {
                        // check for new friends once a day
@@ -392,6 +395,9 @@ function facebook_cron($a,$b) {
                        fb_consume_all($rr['uid']);
                }
        }       
+
+       set_config('facebook','last_poll', time());
+
 }
 
 
index 27c801923f1c256680e5873a884be98a6ffcae1c..b81bd9cfc7e1c23904f65a309fa51fc88cd3b5f3 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -2908,3 +2908,15 @@ function get_plugin_info($plugin){
        }
        return $info;
 }}
+
+if(! function_exists('return_bytes')) {
+function return_bytes ($size_str) {
+    switch (substr ($size_str, -1))
+    {
+        case 'M': case 'm': return (int)$size_str * 1048576;
+        case 'K': case 'k': return (int)$size_str * 1024;
+        case 'G': case 'g': return (int)$size_str * 1073741824;
+        default: return $size_str;
+    }
+}}
+
index 4cab5a3b83fb1238616ed26567af966a5b95b33d..666748cbc0be7683c3317437861ecef99617e89e 100644 (file)
@@ -2,6 +2,7 @@
 
 require_once("boot.php");
 
+
 function poller_run($argv, $argc){
        global $a, $db;
 
@@ -47,18 +48,26 @@ function poller_run($argv, $argc){
                proc_run('php','include/expire.php');
        }
 
-
        // clear old cache
        q("DELETE FROM `cache` WHERE `updated` < '%s'",
                dbesc(datetime_convert('UTC','UTC',"now - 30 days")));
 
        $manual_id  = 0;
+       $generation = 0;
        $hub_update = false;
        $force      = false;
+       $restart    = false;
 
        if(($argc > 1) && ($argv[1] == 'force'))
                $force = true;
 
+       if(($argc > 1) && ($argv[1] == 'restart')) {
+               $restart = true;
+               $generation = intval($argv[2]);
+               if(! $generation)
+                       killme();               
+       }
+
        if(($argc > 1) && intval($argv[1])) {
                $manual_id = intval($argv[1]);
                $force     = true;
@@ -70,7 +79,8 @@ function poller_run($argv, $argc){
 
        $d = datetime_convert();
 
-       call_hooks('cron', $d);
+       if(! $restart)
+               call_hooks('cron', $d);
 
 
        $contacts = q("SELECT `id` FROM `contact` 
@@ -95,7 +105,7 @@ function poller_run($argv, $argc){
                        continue;
 
                foreach($res as $contact) {
-
+                       logger('processing a contact');
                        $xml = false;
 
                        if($manual_id)
@@ -154,6 +164,22 @@ function poller_run($argv, $argc){
                                        continue;
                        }
 
+                       // Check to see if we are running out of memory - if so spawn a new process and kill this one
+
+                       $avail_memory = return_bytes(ini_get('memory_limit'));
+                       $memused = memory_get_peak_usage(true);
+                       if(intval($avail_memory)) {
+                               if(($memused / $avail_memory) > 0.95) {
+                                       if($generation + 1 > 10) {
+                                               logger('poller: maximum number of spawns exceeded. Terminating.');
+                                               killme();
+                                       }
+                                       logger('poller: memory exceeded. ' . $memused . ' bytes used. Spawning new poll.');
+                                       proc_run('php', 'include/poller.php', 'restart', (string) $generation + 1);
+                                       killme();
+                               }
+                       }
+
                        $importer_uid = $contact['uid'];
                
                        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",