]> git.wh0rd.org Git - tt-rss.git/blob - update_daemon2.php
daemon: die if unable to create a stampfile
[tt-rss.git] / update_daemon2.php
1 #!/usr/bin/env php
2 <?php
3         set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
4                 get_include_path());
5
6         declare(ticks = 1);
7         chdir(dirname(__FILE__));
8
9         define('DISABLE_SESSIONS', true);
10
11         require_once "version.php";
12
13         if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) {
14                 define('DAEMON_EXTENDED_DEBUG', true);
15         }
16
17         define('PURGE_INTERVAL', 3600); // seconds
18         define('MAX_CHILD_RUNTIME', 600); // seconds
19
20         require_once "functions.php";
21         require_once "rssfuncs.php";
22         require_once "sanity_check.php";
23         require_once "config.php";
24         require_once "db.php";
25         require_once "db-prefs.php";
26
27         define('MAX_JOBS', 2);
28         define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL);
29
30         if (!function_exists('pcntl_fork')) {
31                 die("error: This script requires PHP compiled with PCNTL module.\n");
32         }
33
34         $master_handlers_installed = false;
35
36         $children = array();
37         $ctimes = array();
38
39         $last_checkpoint = -1;
40
41         function reap_children() {
42                 global $children;
43                 global $ctimes;
44
45                 $tmp = array();
46
47                 foreach ($children as $pid) {
48                         if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
49
50                                 if (file_is_locked("update_daemon-$pid.lock")) {
51                                         array_push($tmp, $pid);
52                                 } else {
53                                         _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
54                                         unset($ctimes[$pid]);
55
56                                 }
57                         } else {
58                                 _debug("[reap_children] child $pid reaped.");
59                                 unset($ctimes[$pid]);
60                         }
61                 }
62
63                 $children = $tmp;
64
65                 return count($tmp);
66         }
67
68         function check_ctimes() {
69                 global $ctimes;
70
71                 foreach (array_keys($ctimes) as $pid) {
72                         $started = $ctimes[$pid];
73
74                         if (time() - $started > MAX_CHILD_RUNTIME) {
75                                 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
76                                 posix_kill($pid, SIGKILL);
77                         }
78                 }
79         }
80
81         function sigchld_handler($signal) {
82                 $running_jobs = reap_children();
83
84                 _debug("[SIGCHLD] jobs left: $running_jobs");
85
86                 pcntl_waitpid(-1, $status, WNOHANG);
87         }
88
89         function shutdown() {
90                 if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
91                         _debug("removing lockfile (master)...");
92                         unlink(LOCK_DIRECTORY . "/update_daemon.lock");
93                 }
94         }
95
96         function task_shutdown() {
97                 $pid = posix_getpid();
98
99                 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
100                         _debug("removing lockfile ($pid)...");
101                         unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
102                 }
103         }
104
105         function sigint_handler() {
106                 _debug("[MASTER] SIG_INT received.\n");
107                 shutdown();
108                 die;
109         }
110
111         function task_sigint_handler() {
112                 _debug("[TASK] SIG_INT received.\n");
113                 task_shutdown();
114                 die;
115         }
116
117         pcntl_signal(SIGCHLD, 'sigchld_handler');
118
119         if (file_is_locked("update_daemon.lock")) {
120                 die("error: Can't create lockfile. ".
121                         "Maybe another daemon is already running.\n");
122         }
123
124         // Try to lock a file in order to avoid concurrent update.
125         $lock_handle = make_lockfile("update_daemon.lock");
126
127         if (!$lock_handle) {
128                 die("error: Can't create lockfile. ".
129                         "Maybe another daemon is already running.\n");
130         }
131
132         // Testing database connection.
133         // It is unnecessary to start the fork loop if database is not ok.
134         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
135
136         if (!init_connection($link)) return;
137
138         db_close($link);
139
140         while (true) {
141
142                 // Since sleep is interupted by SIGCHLD, we need another way to
143                 // respect the SPAWN_INTERVAL
144                 $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
145
146                 if ($next_spawn % 10 == 0) {
147                         $running_jobs = count($children);
148                         _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
149                 }
150
151                 if ($last_checkpoint + SPAWN_INTERVAL < time()) {
152
153                         check_ctimes();
154                         reap_children();
155
156                         for ($j = count($children); $j < MAX_JOBS; $j++) {
157                                 $pid = pcntl_fork();
158                                 if ($pid == -1) {
159                                         die("fork failed!\n");
160                                 } else if ($pid) {
161
162                                         if (!$master_handlers_installed) {
163                                                 _debug("[MASTER] installing shutdown handlers");
164                                                 pcntl_signal(SIGINT, 'sigint_handler');
165                                                 register_shutdown_function('shutdown');
166                                                 $master_handlers_installed = true;
167                                         }
168
169                                         _debug("[MASTER] spawned client $j [PID:$pid]...");
170                                         array_push($children, $pid);
171                                         $ctimes[$pid] = time();
172                                 } else {
173                                         pcntl_signal(SIGCHLD, SIG_IGN);
174                                         pcntl_signal(SIGINT, 'task_sigint_handler');
175
176                                         register_shutdown_function('task_shutdown');
177
178                                         $my_pid = posix_getpid();
179                                         $lock_filename = "update_daemon-$my_pid.lock";
180
181                                         $lock_handle = make_lockfile($lock_filename);
182
183                                         if (!$lock_handle) {
184                                                 die("error: Can't create lockfile ($lock_filename). ".
185                                                 "Maybe another daemon is already running.\n");
186                                         }
187
188                                         // ****** Updating RSS code *******
189                                         // Only run in fork process.
190
191                                         $start_timestamp = time();
192
193                                         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
194
195                                         if (!init_connection($link)) return;
196
197                                         // We disable stamp file, since it is of no use in a multiprocess update.
198                                         // not really, tho for the time being -fox
199                                         if (!make_stampfile('update_daemon.stamp')) {
200                                                 die("error: unable to create stampfile\n");
201                                         }
202
203                                         // Call to the feed batch update function
204                                         // or regenerate feedbrowser cache
205
206                                         if (rand(0,100) > 30) {
207                                                 update_daemon_common($link);
208                                         } else {
209                                                 $count = update_feedbrowser_cache($link);
210                                                 _debug("Feedbrowser updated, $count feeds processed.");
211
212                                                 purge_orphans($link, true);
213
214                                                 $rc = cleanup_tags($link, 14, 50000);
215
216                                                 _debug("Cleaned $rc cached tags.");
217
218                                                 global $pluginhost;
219                                                 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
220                                         }
221
222                                         _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
223
224                                         db_close($link);
225
226                                         // We are in a fork.
227                                         // We wait a little before exiting to avoid to be faster than our parent process.
228                                         sleep(1);
229
230                                         unlink(LOCK_DIRECTORY . "/$lock_filename");
231
232                                         // We exit in order to avoid fork bombing.
233                                         exit(0);
234                                 }
235
236                                 // We wait a little time before the next fork, in order to let the first fork
237                                 // mark the feeds it update :
238                                 sleep(1);
239                         }
240                         $last_checkpoint = time();
241                 }
242                 sleep(1);
243         }
244
245 ?>