]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Merge pull request #2099 from rabuzarus/2611_new_user_addr
[friendica.git] / include / poller.php
1 <?php
2 if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
3         $directory = dirname($_SERVER["argv"][0]);
4
5         if (substr($directory, 0, 1) != "/")
6                 $directory = $_SERVER["PWD"]."/".$directory;
7
8         $directory = realpath($directory."/..");
9
10         chdir($directory);
11 }
12
13 require_once("boot.php");
14
15 function poller_run(&$argv, &$argc){
16         global $a, $db;
17
18         if(is_null($a)) {
19                 $a = new App;
20         }
21
22         if(is_null($db)) {
23                 @include(".htconfig.php");
24                 require_once("include/dba.php");
25                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
26                 unset($db_host, $db_user, $db_pass, $db_data);
27         };
28
29         if(function_exists('sys_getloadavg')) {
30                 $maxsysload = intval(get_config('system','maxloadavg'));
31                 if($maxsysload < 1)
32                         $maxsysload = 50;
33
34                 $load = sys_getloadavg();
35                 if(intval($load[0]) > $maxsysload) {
36                         logger('system: load ' . $load[0] . ' too high. poller deferred to next scheduled run.');
37                         return;
38                 }
39         }
40
41         if(($argc <= 1) OR ($argv[1] != "no_cron")) {
42                 // Run the cron job that calls all other jobs
43                 proc_run("php","include/cron.php");
44
45                 // Run the cronhooks job separately from cron for being able to use a different timing
46                 proc_run("php","include/cronhooks.php");
47
48                 // Cleaning dead processes
49                 $r = q("SELECT DISTINCT(`pid`) FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
50                 foreach($r AS $pid)
51                         if (!posix_kill($pid["pid"], 0))
52                                 q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
53                                         intval($pid["pid"]));
54                         else {
55                                 // To-Do: Kill long running processes
56                                 // But: Update processes (like the database update) mustn't be killed
57                         }
58
59         } else
60                 // Sleep two seconds before checking for running processes to avoid having too many workers
61                 sleep(4);
62
63         // Checking number of workers
64         if (poller_too_much_workers())
65                 return;
66
67         $starttime = time();
68
69         while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
70
71                 q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d AND `executed` = '0000-00-00 00:00:00'",
72                         dbesc(datetime_convert()),
73                         intval(getmypid()),
74                         intval($r[0]["id"]));
75
76                 // Assure that there are no tasks executed twice
77                 $id = q("SELECT `id` FROM `workerqueue` WHERE `id` = %d AND `pid` = %d",
78                         intval($r[0]["id"]),
79                         intval(getmypid()));
80                 if (!$id) {
81                         logger("Queue item ".$r[0]["id"]." was executed multiple times - skip this execution", LOGGER_DEBUG);
82                         continue;
83                 }
84
85                 $argv = json_decode($r[0]["parameter"]);
86
87                 $argc = count($argv);
88
89                 // Check for existance and validity of the include file
90                 $include = $argv[0];
91
92                 if (!validate_include($include)) {
93                         logger("Include file ".$argv[0]." is not valid!");
94                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
95                         continue;
96                 }
97
98                 require_once($include);
99
100                 $funcname=str_replace(".php", "", basename($argv[0]))."_run";
101
102                 if (function_exists($funcname)) {
103                         logger("Process ".getmypid().": ".$funcname." ".$r[0]["parameter"]);
104                         $funcname($argv, $argc);
105
106                         logger("Process ".getmypid().": ".$funcname." - done");
107
108                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
109                 } else
110                         logger("Function ".$funcname." does not exist");
111
112                 // Quit the poller once every hour
113                 if (time() > ($starttime + 3600))
114                         return;
115
116                 // Count active workers and compare them with a maximum value that depends on the load
117                 if (poller_too_much_workers())
118                         return;
119         }
120
121 }
122
123 function poller_too_much_workers() {
124
125         $queues = get_config("system", "worker_queues");
126
127         if ($queues == 0)
128                 $queues = 4;
129
130         $active = poller_active_workers();
131
132         // Decrease the number of workers at higher load
133         if(function_exists('sys_getloadavg')) {
134                 $load = max(sys_getloadavg());
135
136                 $maxsysload = intval(get_config('system','maxloadavg'));
137                 if($maxsysload < 1)
138                         $maxsysload = 50;
139
140                 $maxworkers = $queues;
141
142                 // Some magical mathemathics to reduce the workers
143                 $exponent = 3;
144                 $slope = $maxworkers / pow($maxsysload, $exponent);
145                 $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
146
147                 logger("Current load: ".$load." - maximum: ".$maxsysload." - current queues: ".$active." - maximum: ".$queues, LOGGER_DEBUG);
148
149         }
150
151         return($active >= $queues);
152 }
153
154 function poller_active_workers() {
155         $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
156
157         return($workers[0]["workers"]);
158 }
159
160 if (array_search(__file__,get_included_files())===0){
161   poller_run($_SERVER["argv"],$_SERVER["argc"]);
162   killme();
163 }
164 ?>