]> git.mxchange.org Git - friendica.git/blob - bin/daemon.php
Create config keys if they not exists on set.
[friendica.git] / bin / daemon.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * @file bin/daemon.php
5  * @brief Run the worker from a daemon.
6  *
7  * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
8  */
9
10 use Friendica\App;
11 use Friendica\BaseObject;
12 use Friendica\Core\Config;
13 use Friendica\Core\Worker;
14
15 // Ensure that daemon.php is executed from the base path of the installation
16 if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
17         $directory = dirname($_SERVER["argv"][0]);
18
19         if (substr($directory, 0, 1) != "/") {
20                 $directory = $_SERVER["PWD"]."/".$directory;
21         }
22         $directory = realpath($directory."/..");
23
24         chdir($directory);
25 }
26
27 require_once "boot.php";
28 require_once "include/dba.php";
29
30 $a = new App(dirname(__DIR__));
31 BaseObject::setApp($a);
32
33 require_once ".htconfig.php";
34 dba::connect($db_host, $db_user, $db_pass, $db_data);
35
36 Config::load();
37
38 if (!isset($pidfile)) {
39         die('Please specify a pid file in the variable $pidfile in the .htconfig.php. For example:'."\n".
40                 '$pidfile = "/path/to/daemon.pid";'."\n");
41 }
42
43 if (in_array("start", $_SERVER["argv"])) {
44         $mode = "start";
45 }
46
47 if (in_array("stop", $_SERVER["argv"])) {
48         $mode = "stop";
49 }
50
51 if (in_array("status", $_SERVER["argv"])) {
52         $mode = "status";
53 }
54
55 if (!isset($mode)) {
56         die("Please use either 'start', 'stop' or 'status'.\n");
57 }
58
59 if (empty($_SERVER["argv"][0])) {
60         die("Unexpected script behaviour. This message should never occur.\n");
61 }
62
63 $pid = @file_get_contents($pidfile);
64
65 if (empty($pid) && in_array($mode, ["stop", "status"])) {
66         Config::set('system', 'worker_daemon_mode', false);
67         die("Pidfile wasn't found. Is the daemon running?\n");
68 }
69
70 if ($mode == "status") {
71         if (posix_kill($pid, 0)) {
72                 die("Daemon process $pid is running.\n");
73         }
74
75         unlink($pidfile);
76
77         Config::set('system', 'worker_daemon_mode', false);
78         die("Daemon process $pid isn't running.\n");
79 }
80
81 if ($mode == "stop") {
82         posix_kill($pid, SIGTERM);
83
84         unlink($pidfile);
85
86         logger("Worker daemon process $pid was killed.", LOGGER_DEBUG);
87
88         Config::set('system', 'worker_daemon_mode', false);
89         die("Worker daemon process $pid was killed.\n");
90 }
91
92 if (!empty($pid) && posix_kill($pid, 0)) {
93         die("Daemon process $pid is already running.\n");
94 }
95
96 logger('Starting worker daemon.', LOGGER_DEBUG);
97 echo "Starting worker daemon.\n";
98
99 // Switch over to daemon mode.
100 if ($pid = pcntl_fork()) {
101         return;     // Parent
102 }
103
104 fclose(STDIN);  // Close all of the standard
105 fclose(STDOUT); // file descriptors as we
106 fclose(STDERR); // are running as a daemon.
107
108 dba::disconnect();
109
110 register_shutdown_function('shutdown');
111
112 if (posix_setsid() < 0) {
113         return;
114 }
115
116 if ($pid = pcntl_fork()) {
117         return;     // Parent
118 }
119
120 // We lose the database connection upon forking
121 dba::connect($db_host, $db_user, $db_pass, $db_data);
122 unset($db_host, $db_user, $db_pass, $db_data);
123
124 Config::set('system', 'worker_daemon_mode', true);
125
126 // Just to be sure that this script really runs endlessly
127 set_time_limit(0);
128
129 $pid = getmypid();
130 file_put_contents($pidfile, $pid);
131
132 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
133
134 $do_cron = true;
135 $last_cron = 0;
136
137 // Now running as a daemon.
138 while (true) {
139         if (!$do_cron && ($last_cron + $wait_interval) < time()) {
140                 logger('Forcing cron worker call.', LOGGER_DEBUG);
141                 $do_cron = true;
142         }
143
144         Worker::spawnWorker($do_cron);
145
146         if ($do_cron) {
147                 // We force a reconnect of the database connection.
148                 // This is done to ensure that the connection don't get lost over time.
149                 dba::reconnect();
150
151                 $last_cron = time();
152         }
153
154         logger("Sleeping", LOGGER_DEBUG);
155         $i = 0;
156         do {
157                 sleep(1);
158         } while (($i++ < $wait_interval) && !Worker::IPCJobsExists());
159
160         if ($i >= $wait_interval) {
161                 $do_cron = true;
162                 logger("Woke up after $wait_interval seconds.", LOGGER_DEBUG);
163         } else {
164                 $do_cron = false;
165                 logger("Worker jobs are calling to be forked.", LOGGER_DEBUG);
166         }
167 }
168
169 function shutdown() {
170         posix_kill(posix_getpid(), SIGHUP);
171 }