]> git.mxchange.org Git - friendica.git/commitdiff
Split cronhook call to several single calls
authorMichael Vogel <icarus@dabo.de>
Tue, 2 Aug 2016 04:28:34 +0000 (06:28 +0200)
committerMichael Vogel <icarus@dabo.de>
Tue, 2 Aug 2016 04:28:34 +0000 (06:28 +0200)
include/cron.php
include/cronhooks.php
include/plugin.php
include/poller.php

index c54fef8a9ce59563d4fb900d9aaacf0df5b17fa7..28d6713f5428a0018f1a0ff9009f5e6bce869101 100644 (file)
@@ -142,28 +142,44 @@ function cron_run(&$argv, &$argc){
        // Repair entries in the database
        cron_repair_database();
 
+       // Poll contacts
+       cron_poll_contacts($argc, $argv);
+
+       logger('cron: end');
+
+       set_config('system','last_cron', time());
+
+       return;
+}
+
+/**
+ * @brief Clear cache entries
+ *
+ * @param App $a
+ */
+function cron_poll_contacts($argc, $argv) {
        $manual_id  = 0;
        $generation = 0;
        $force      = false;
        $restart    = false;
 
-       if(($argc > 1) && ($argv[1] == 'force'))
+       if (($argc > 1) && ($argv[1] == 'force'))
                $force = true;
 
-       if(($argc > 1) && ($argv[1] == 'restart')) {
+       if (($argc > 1) && ($argv[1] == 'restart')) {
                $restart = true;
                $generation = intval($argv[2]);
-               if(! $generation)
+               if (!$generation)
                        killme();
        }
 
-       if(($argc > 1) && intval($argv[1])) {
+       if (($argc > 1) && intval($argv[1])) {
                $manual_id = intval($argv[1]);
                $force     = true;
        }
 
        $interval = intval(get_config('system','poll_interval'));
-       if(! $interval)
+       if (!$interval)
                $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
 
        // If we are using the worker we don't need a delivery interval
@@ -200,11 +216,11 @@ function cron_run(&$argv, &$argc){
                dbesc(NETWORK_MAIL2)
        );
 
-       if(! count($contacts)) {
+       if (!count($contacts)) {
                return;
        }
 
-       foreach($contacts as $c) {
+       foreach ($contacts as $c) {
 
                $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
                        intval($c['id'])
@@ -266,7 +282,7 @@ function cron_run(&$argv, &$argc){
                                                        $update = true;
                                                break;
                                }
-                               if(!$update)
+                               if (!$update)
                                        continue;
                        }
 
@@ -278,12 +294,6 @@ function cron_run(&$argv, &$argc){
                                @time_sleep_until(microtime(true) + (float) $interval);
                }
        }
-
-       logger('cron: end');
-
-       set_config('system','last_cron', time());
-
-       return;
 }
 
 /**
index b6cf0e72378763d6b3e5ac697672dce67438ea02..4bb1e5f65960b3b3c1bbad1077d34628388040b0 100644 (file)
@@ -31,6 +31,17 @@ function cronhooks_run(&$argv, &$argc){
                        return;
        }
 
+       load_hooks();
+
+       if (($argc == 2) AND is_array($a->hooks) AND array_key_exists("cron", $a->hooks)) {
+                foreach ($a->hooks["cron"] as $hook)
+                       if ($hook[1] == $argv[1]) {
+                               logger("Calling cron hook '".$hook[1]."'", LOGGER_DEBUG);
+                               call_single_hook($a, $name, $hook, $data);
+                       }
+               return;
+       }
+
        $last = get_config('system','last_cronhook');
 
        $poll_interval = intval(get_config('system','cronhook_interval'));
@@ -47,13 +58,17 @@ function cronhooks_run(&$argv, &$argc){
 
        $a->set_baseurl(get_config('system','url'));
 
-       load_hooks();
-
        logger('cronhooks: start');
 
        $d = datetime_convert();
 
-       call_hooks('cron', $d);
+       if (get_config("system", "worker") AND is_array($a->hooks) AND array_key_exists("cron", $a->hooks)) {
+                foreach ($a->hooks["cron"] as $hook) {
+                       logger("Calling cronhooks for '".$hook[1]."'", LOGGER_DEBUG);
+                       proc_run(PRIORITY_MEDIUM, "include/cronhooks.php", $hook[1]);
+               }
+       } else
+               call_hooks('cron', $d);
 
        logger('cronhooks: end');
 
index e25e9e85b1c367529743f859893968031465b430..f6e4a7a88223e651ad5c7ec1131891338571459d 100644 (file)
@@ -205,37 +205,41 @@ function load_hooks() {
  * @param string $name of the hook to call
  * @param string|array &$data to transmit to the callback handler
  */
-if(! function_exists('call_hooks')) {
 function call_hooks($name, &$data = null) {
        $stamp1 = microtime(true);
 
        $a = get_app();
 
-       #logger($name, LOGGER_ALL);
-
-       if((is_array($a->hooks)) && (array_key_exists($name,$a->hooks))) {
-               foreach($a->hooks[$name] as $hook) {
-                       // Don't run a theme's hook if the user isn't using the theme
-                       if(strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
-                               continue;
+       if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
+               foreach ($a->hooks[$name] as $hook)
+                       call_single_hook($a, $name, $hook, $data);
+}
 
-                       @include_once($hook[0]);
-                       if(function_exists($hook[1])) {
-                               $func = $hook[1];
-                               //logger($name." => ".$hook[0].":".$func."()", LOGGER_DEBUG);
-                               $func($a,$data);
-                       }
-                       else {
-                               // remove orphan hooks
-                               q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
-                                       dbesc($name),
-                                       dbesc($hook[0]),
-                                       dbesc($hook[1])
-                               );
-                       }
-               }
+/**
+ * @brief Calls a single hook.
+ *
+ * @param string $name of the hook to call
+ * @param array $hook Hook data
+ * @param string|array &$data to transmit to the callback handler
+ */
+function call_single_hook($a, $name, $hook, &$data = null) {
+       // Don't run a theme's hook if the user isn't using the theme
+       if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
+               return;
+
+       @include_once($hook[0]);
+       if (function_exists($hook[1])) {
+               $func = $hook[1];
+               $func($a, $data);
+       } else {
+               // remove orphan hooks
+               q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
+                       dbesc($name),
+                       dbesc($hook[0]),
+                       dbesc($hook[1])
+               );
        }
-}}
+}
 
 //check if an app_menu hook exist for plugin $name.
 //Return true if the plugin is an app
index b54757b00bf1bb9f9b5aa7ffc9eb9fee3b4dd7e2..811b81e174bfcf53ddf2d01b9b71b0ee5dde0098 100644 (file)
@@ -46,10 +46,10 @@ function poller_run(&$argv, &$argc){
 
        if(($argc <= 1) OR ($argv[1] != "no_cron")) {
                // Run the cron job that calls all other jobs
-               proc_run("php","include/cron.php");
+               proc_run(PRIORITY_MEDIUM, "include/cron.php");
 
                // Run the cronhooks job separately from cron for being able to use a different timing
-               proc_run("php","include/cronhooks.php");
+               proc_run(PRIORITY_MEDIUM, "include/cronhooks.php");
 
                // Cleaning dead processes
                poller_kill_stale_workers();