]> git.mxchange.org Git - friendica.git/blobdiff - include/poller.php
Add basepath to App
[friendica.git] / include / poller.php
index 487c86ab3002670516b02297eba0b1d380607bf6..08e71fd486b69c23b68b52ee3e560b715e25c2e8 100644 (file)
@@ -1,4 +1,8 @@
 <?php
+
+use Friendica\App;
+use Friendica\Core\Config;
+
 if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
        $directory = dirname($_SERVER["argv"][0]);
 
@@ -10,15 +14,13 @@ if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
        chdir($directory);
 }
 
-use \Friendica\Core\Config;
-
 require_once("boot.php");
 
 function poller_run($argv, $argc){
        global $a, $db;
 
-       if(is_null($a)) {
-               $a = new App;
+       if (is_null($a)) {
+               $a = new App(dirname(__DIR__));
        }
 
        if(is_null($db)) {
@@ -41,6 +43,10 @@ function poller_run($argv, $argc){
 
        $a->start_process();
 
+       if ($a->min_memory_reached()) {
+               return;
+       }
+
        if (poller_max_connections_reached()) {
                return;
        }
@@ -67,6 +73,11 @@ function poller_run($argv, $argc){
 
        while ($r = poller_worker_process()) {
 
+               // Check free memory
+               if ($a->min_memory_reached()) {
+                       return;
+               }
+
                // Count active workers and compare them with a maximum value that depends on the load
                if (poller_too_much_workers()) {
                        return;
@@ -80,7 +91,6 @@ function poller_run($argv, $argc){
                if (time() > ($starttime + 3600))
                        return;
        }
-
 }
 
 /**
@@ -364,11 +374,11 @@ function poller_kill_stale_workers() {
                return;
        }
 
-       foreach($r AS $pid)
-               if (!posix_kill($pid["pid"], 0))
+       foreach ($r AS $pid) {
+               if (!posix_kill($pid["pid"], 0)) {
                        q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = 0 WHERE `pid` = %d",
                                dbesc(NULL_DATE), intval($pid["pid"]));
-               else {
+               else {
                        // Kill long running processes
 
                        // Check if the priority is in a valid range
@@ -397,9 +407,11 @@ function poller_kill_stale_workers() {
                                        dbesc(datetime_convert()),
                                        intval(PRIORITY_NEGLIGIBLE),
                                        intval($pid["pid"]));
-                       } else
+                       } else {
                                logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG);
+                       }
                }
+       }
 }
 
 /**
@@ -558,9 +570,9 @@ function poller_worker_process() {
                                ORDER BY `priority`, `created` LIMIT 1",
                                dbesc(NULL_DATE),
                                intval($highest_priority));
-               if (dbm::is_result($r))
+               if (dbm::is_result($r)) {
                        return $r;
-
+               }
                // Give slower processes some processing time
                $r = q("SELECT * FROM `workerqueue`
                                WHERE `executed` <= '%s' AND `priority` > %d
@@ -570,9 +582,9 @@ function poller_worker_process() {
        }
 
        // If there is no result (or we shouldn't pass lower processes) we check without priority limit
-       if (($highest_priority == 0) OR !dbm::is_result($r))
+       if (($highest_priority == 0) OR !dbm::is_result($r)) {
                $r = q("SELECT * FROM `workerqueue` WHERE `executed` <= '%s' ORDER BY `priority`, `created` LIMIT 1", dbesc(NULL_DATE));
-
+       }
        return $r;
 }
 
@@ -676,4 +688,3 @@ if (array_search(__file__,get_included_files())===0){
 
        killme();
 }
-?>