]> git.mxchange.org Git - friendica.git/blob - include/poller.php
74d23a548232ce53dfd8004568cc73e9249f7a71
[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         // Run the cron job that calls all other jobs
30         proc_run("php","include/cron.php");
31
32         // Cleaning killed processes
33         $r = q("SELECT DISTINCT(`pid`) FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
34         foreach($r AS $pid)
35                 if (!posix_kill($pid["pid"], 0))
36                         q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
37                                 intval($pid["pid"]));
38
39         // Checking number of workers
40         $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
41
42         $queues = intval(get_config("system", "worker_queues"));
43
44         if ($queues == 0)
45                 $queues = 4;
46
47         if ($workers[0]["workers"] >= $queues)
48                 return;
49
50         while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
51                 q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d",
52                         dbesc(datetime_convert()),
53                         intval(getmypid()),
54                         intval($r[0]["id"]));
55
56                 $argv = json_decode($r[0]["parameter"]);
57
58                 $argc = count($argv);
59
60                 // To-Do: Check for existance
61                 require_once(basename($argv[0]));
62
63                 $funcname=str_replace(".php", "", basename($argv[0]))."_run";
64
65                 if (function_exists($funcname)) {
66                         logger("Process ".getmypid().": ".$funcname." ".$r[0]["parameter"]);
67                         $funcname($argv, $argc);
68
69                         logger("Process ".getmypid().": ".$funcname." - done");
70
71                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
72                 }
73         }
74
75 }
76
77 if (array_search(__file__,get_included_files())===0){
78   poller_run($_SERVER["argv"],$_SERVER["argc"]);
79   killme();
80 }
81 ?>