]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Worker.php
Move Temporal::convert() to DateTimeFormat::convert()
[friendica.git] / src / Core / Worker.php
index da7425a61682ae22d315e99f9567450baadf5798..fd5a0bf4c3a3db97dae2c1aff4926df552b355f7 100644 (file)
@@ -4,13 +4,14 @@
  */
 namespace Friendica\Core;
 
-use Friendica\App;
-use Friendica\Core\System;
+use Friendica\Core\Addon;
 use Friendica\Core\Config;
-use Friendica\Core\Worker;
+use Friendica\Core\System;
 use Friendica\Database\DBM;
+use Friendica\Model\Process;
+use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Lock;
-
+use Friendica\Util\Network;
 use dba;
 
 require_once 'include/dba.php';
@@ -50,7 +51,7 @@ class Worker
                }
 
                // We now start the process. This is done after the load check since this could increase the load.
-               $a->start_process();
+               self::startProcess();
 
                // Kill stale processes every 5 minutes
                $last_cleanup = Config::get('system', 'poller_last_cleaned', 0);
@@ -233,7 +234,7 @@ class Worker
 
                        if ($age > 1) {
                                $stamp = (float)microtime(true);
-                               dba::update('workerqueue', ['executed' => datetime_convert()], ['pid' => $mypid, 'done' => false]);
+                               dba::update('workerqueue', ['executed' => DateTimeFormat::utcNow()], ['pid' => $mypid, 'done' => false]);
                                self::$db_duration += (microtime(true) - $stamp);
                        }
 
@@ -243,7 +244,7 @@ class Worker
 
                        $stamp = (float)microtime(true);
                        if (dba::update('workerqueue', ['done' => true], ['id' => $queue["id"]])) {
-                               Config::set('system', 'last_poller_execution', datetime_convert());
+                               Config::set('system', 'last_poller_execution', DateTimeFormat::utcNow());
                        }
                        self::$db_duration = (microtime(true) - $stamp);
 
@@ -276,7 +277,7 @@ class Worker
 
                        if ($age > 1) {
                                $stamp = (float)microtime(true);
-                               dba::update('workerqueue', ['executed' => datetime_convert()], ['pid' => $mypid, 'done' => false]);
+                               dba::update('workerqueue', ['executed' => DateTimeFormat::utcNow()], ['pid' => $mypid, 'done' => false]);
                                self::$db_duration += (microtime(true) - $stamp);
                        }
 
@@ -284,7 +285,7 @@ class Worker
 
                        $stamp = (float)microtime(true);
                        if (dba::update('workerqueue', ['done' => true], ['id' => $queue["id"]])) {
-                               Config::set('system', 'last_poller_execution', datetime_convert());
+                               Config::set('system', 'last_poller_execution', DateTimeFormat::utcNow());
                        }
                        self::$db_duration = (microtime(true) - $stamp);
                } else {
@@ -572,7 +573,7 @@ class Worker
                                        }
                                        dba::update(
                                                'workerqueue',
-                                               ['executed' => NULL_DATE, 'created' => datetime_convert(), 'priority' => $new_priority, 'pid' => 0],
+                                               ['executed' => NULL_DATE, 'created' => DateTimeFormat::utcNow(), 'priority' => $new_priority, 'pid' => 0],
                                                ['id' => $entry["id"]]
                                        );
                                } else {
@@ -823,7 +824,7 @@ class Worker
                if ($found) {
                        $condition = "`id` IN (".substr(str_repeat("?, ", count($ids)), 0, -2).") AND `pid` = 0 AND NOT `done`";
                        array_unshift($ids, $condition);
-                       dba::update('workerqueue', ['executed' => datetime_convert(), 'pid' => $mypid], $ids);
+                       dba::update('workerqueue', ['executed' => DateTimeFormat::utcNow(), 'pid' => $mypid], $ids);
                }
 
                return $found;
@@ -888,7 +889,7 @@ class Worker
                }
 
                $url = System::baseUrl()."/worker";
-               fetch_url($url, false, $redirects, 1);
+               Network::fetchUrl($url, false, $redirects, 1);
        }
 
        /**
@@ -915,7 +916,7 @@ class Worker
                        if (self::tooMuchWorkers()) {
                                // Cleaning dead processes
                                self::killStaleWorkers();
-                               get_app()->remove_inactive_processes();
+                               Process::deleteInactive();
 
                                return;
                        }
@@ -951,7 +952,7 @@ class Worker
 
                /// @todo We should clean up the corresponding workerqueue entries as well
                $condition = ["`created` < ? AND `command` = 'worker.php'",
-                               datetime_convert('UTC', 'UTC', "now - ".$timeout." minutes")];
+                               DateTimeFormat::utc("now - ".$timeout." minutes")];
                dba::delete('process', $condition);
        }
 
@@ -1031,14 +1032,14 @@ class Worker
 
                $arr = ['args' => $args, 'run_cmd' => true];
 
-               call_hooks("proc_run", $arr);
+               Addon::callHooks("proc_run", $arr);
                if (!$arr['run_cmd'] || !count($args)) {
                        return true;
                }
 
                $priority = PRIORITY_MEDIUM;
                $dont_fork = Config::get("system", "worker_dont_fork");
-               $created = datetime_convert();
+               $created = DateTimeFormat::utcNow();
 
                if (is_int($run_parameter)) {
                        $priority = $run_parameter;
@@ -1092,4 +1093,31 @@ class Worker
 
                return true;
        }
+
+       /**
+        * Log active processes into the "process" table
+        *
+        * @brief Log active processes into the "process" table
+        */
+       public static function startProcess()
+       {
+               $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
+
+               $command = basename($trace[0]['file']);
+
+               Process::deleteInactive();
+
+               Process::insert($command);
+       }
+
+       /**
+        * Remove the active process from the "process" table
+        *
+        * @brief Remove the active process from the "process" table
+        * @return bool
+        */
+       public static function endProcess()
+       {
+               return Process::deleteByPid();
+       }
 }