]> git.mxchange.org Git - friendica.git/blobdiff - include/poller.php
Replace AND and OR in PHP conditions by && and ||
[friendica.git] / include / poller.php
index 1de9126d2d7b49e08dd8342212130c0874051aeb..cc8edce656969ba4e8743adac09d479724d2edee 100644 (file)
@@ -4,7 +4,7 @@ use Friendica\App;
 use Friendica\Core\Config;
 use Friendica\Util\Lock;
 
-if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
+if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
        $directory = dirname($_SERVER["argv"][0]);
 
        if (substr($directory, 0, 1) != "/") {
@@ -75,7 +75,7 @@ function poller_run($argv, $argc){
        }
 
        // Now we start additional cron processes if we should do so
-       if (($argc <= 1) OR ($argv[1] != "no_cron")) {
+       if (($argc <= 1) || ($argv[1] != "no_cron")) {
                poller_run_cron();
        }
 
@@ -89,25 +89,20 @@ function poller_run($argv, $argc){
                        continue;
                }
 
-               // To avoid the quitting of multiple pollers we serialize the next check
-               if (!Lock::set('poller_worker')) {
-                       logger('Cannot get a lock, retrying.', LOGGER_DEBUG);
-                       poller_unclaim_process();
-                       continue;
-               }
-
-               // Count active workers and compare them with a maximum value that depends on the load
-               if (poller_too_much_workers()) {
-                       logger('Active worker limit reached, quitting.', LOGGER_DEBUG);
-                       return;
-               }
-
-               Lock::remove('poller_worker');
+               // To avoid the quitting of multiple pollers only one poller at a time will execute the check
+               if (Lock::set('poller_worker', 0)) {
+                       // Count active workers and compare them with a maximum value that depends on the load
+                       if (poller_too_much_workers()) {
+                               logger('Active worker limit reached, quitting.', LOGGER_DEBUG);
+                               return;
+                       }
 
-               // Check free memory
-               if ($a->min_memory_reached()) {
-                       logger('Memory limit reached, quitting.', LOGGER_DEBUG);
-                       return;
+                       // Check free memory
+                       if ($a->min_memory_reached()) {
+                               logger('Memory limit reached, quitting.', LOGGER_DEBUG);
+                               return;
+                       }
+                       Lock::remove('poller_worker');
                }
 
                // finally the work will be done
@@ -396,6 +391,7 @@ function poller_kill_stale_workers() {
                        if (!in_array($pid["priority"], array(PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE))) {
                                $pid["priority"] = PRIORITY_MEDIUM;
                        }
+
                        // Define the maximum durations
                        $max_duration_defaults = array(PRIORITY_CRITICAL => 360, PRIORITY_HIGH => 10, PRIORITY_MEDIUM => 60, PRIORITY_LOW => 180, PRIORITY_NEGLIGIBLE => 360);
                        $max_duration = $max_duration_defaults[$pid["priority"]];
@@ -466,13 +462,12 @@ function poller_too_much_workers() {
                        dba::close($processes);
                }
                dba::close($entries);
-
-               $processlist = implode(', ', $listitem);
+               $processlist = ' ('.implode(', ', $listitem).')';
 
                $s = q("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` <= '%s'", dbesc(NULL_DATE));
                $entries = $s[0]["total"];
 
-               if (Config::get("system", "worker_fastlane", false) AND ($queues > 0) AND ($entries > 0) AND ($active >= $queues)) {
+               if (Config::get("system", "worker_fastlane", false) && ($queues > 0) && ($entries > 0) && ($active >= $queues)) {
                        $s = q("SELECT `priority` FROM `workerqueue` WHERE `executed` <= '%s' ORDER BY `priority` LIMIT 1", dbesc(NULL_DATE));
                        $top_priority = $s[0]["priority"];
 
@@ -480,16 +475,16 @@ function poller_too_much_workers() {
                                intval($top_priority), dbesc(NULL_DATE));
                        $high_running = dbm::is_result($s);
 
-                       if (!$high_running AND ($top_priority > PRIORITY_UNDEFINED) AND ($top_priority < PRIORITY_NEGLIGIBLE)) {
+                       if (!$high_running && ($top_priority > PRIORITY_UNDEFINED) && ($top_priority < PRIORITY_NEGLIGIBLE)) {
                                logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
                                $queues = $active + 1;
                        }
                }
 
-               logger("Load: ".$load."/".$maxsysload." - processes: ".$active."/".$entries." (".$processlist.") - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
+               logger("Load: ".$load."/".$maxsysload." - processes: ".$active."/".$entries.$processlist." - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
 
                // Are there fewer workers running as possible? Then fork a new one.
-               if (!Config::get("system", "worker_dont_fork") AND ($queues > ($active + 1)) AND ($entries > 1)) {
+               if (!Config::get("system", "worker_dont_fork") && ($queues > ($active + 1)) && ($entries > 1)) {
                        logger("Active workers: ".$active."/".$queues." Fork a new worker.", LOGGER_DEBUG);
                        $args = array("include/poller.php", "no_cron");
                        $a = get_app();
@@ -637,7 +632,7 @@ function poller_claim_process($queue) {
        if (!$id) {
                logger("Queue item ".$queue["id"]." vanished - skip this execution", LOGGER_DEBUG);
                return false;
-       } elseif ((strtotime($id[0]["executed"]) <= 0) OR ($id[0]["pid"] == 0)) {
+       } elseif ((strtotime($id[0]["executed"]) <= 0) || ($id[0]["pid"] == 0)) {
                logger("Entry for queue item ".$queue["id"]." wasn't stored - skip this execution", LOGGER_DEBUG);
                return false;
        } elseif ($id[0]["pid"] != $mypid) {
@@ -752,11 +747,11 @@ function poller_run_cron() {
 if (array_search(__file__,get_included_files())===0){
        poller_run($_SERVER["argv"],$_SERVER["argc"]);
 
-       Lock::remove('poller_worker');
-
        poller_unclaim_process();
 
        get_app()->end_process();
 
+       Lock::remove('poller_worker');
+
        killme();
 }