]> git.mxchange.org Git - friendica.git/blob - include/worker.php
The worker is now working
[friendica.git] / include / worker.php
1 #!/usr/bin/php
2 <?php
3 if (sizeof($_SERVER["argv"]) == 0)
4         die();
5
6 $directory = dirname($_SERVER["argv"][0]);
7
8 if (substr($directory, 0, 1) != "/")
9         $directory = $_SERVER["PWD"]."/".$directory;
10
11 $directory = realpath($directory."/..");
12
13 chdir($directory);
14 require_once("boot.php");
15
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 queue delivery process in the background
30
31 proc_run('php',"include/queue.php");
32
33 // run diaspora photo queue process in the background
34
35 proc_run('php',"include/dsprphotoq.php");
36
37 // run the process to discover global contacts in the background
38
39 proc_run('php',"include/discover_poco.php");
40
41 // run the process to update locally stored global contacts in the background
42
43 proc_run('php',"include/discover_poco.php", "checkcontact");
44
45 // When everything else is done ...
46 proc_run("php","include/poller.php");
47
48 // Cleaning killed 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
55 // Checking number of workers
56 $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
57
58 $queues = intval(get_config("system", "worker_queues"));
59
60 if ($queues == 0)
61         $queues = 4;
62
63 if ($workers[0]["workers"] >= $queues)
64         return;
65
66 while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
67         q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d",
68                 dbesc(datetime_convert()),
69                 intval(getmypid()),
70                 intval($r[0]["id"]));
71
72         $argv = json_decode($r[0]["parameter"]);
73
74         $argc = count($argv);
75
76         // To-Do: Check for existance
77         require_once(basename($argv[0]));
78
79         $funcname=str_replace(".php", "", basename($argv[0]))."_run";
80
81         if (function_exists($funcname)) {
82                 logger("Process ".getmypid().": ".$funcname." ".$r[0]["parameter"]);
83                 $funcname($argv, $argc);
84                 //sleep(10);
85                 logger("Process ".getmypid().": ".$funcname." - done");
86
87                 q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
88         }
89 }
90
91 ?>