]> git.wh0rd.org - tt-rss.git/blame - update_daemon2.php
allow connections to pgsql without password
[tt-rss.git] / update_daemon2.php
CommitLineData
02008cb1
AD
1#!/usr/bin/php
2<?php
ffa7cbae
AD
3 // This is an experimental multiprocess update daemon.
4 // Some configurable variable may be found below.
5
6 // define('DEFAULT_ERROR_LEVEL', E_ALL);
7 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
02008cb1
AD
8
9 declare(ticks = 1);
10
ffa7cbae 11 define('DISABLE_SESSIONS', true);
02008cb1 12
ffa7cbae
AD
13 require_once "version.php";
14
010c16f1 15 if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) {
ffa7cbae
AD
16 define('DAEMON_EXTENDED_DEBUG', true);
17 }
18
19 define('PURGE_INTERVAL', 3600); // seconds
20
21 require_once "sanity_check.php";
22 require_once "config.php";
23
98f70418
AD
24 define('MAX_JOBS', 2);
25
02008cb1 26 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL);
ffa7cbae 27
16956646
AD
28 if (!function_exists('pcntl_fork')) {
29 die("error: This script requires PHP compiled with PCNTL module.\n");
30 }
31
ffa7cbae 32 if (!ENABLE_UPDATE_DAEMON) {
16956646 33 die("error: Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
ffa7cbae
AD
34 }
35
36 require_once "db.php";
37 require_once "db-prefs.php";
38 require_once "functions.php";
816cdfb7 39 require_once "lib/magpierss/rss_fetch.inc";
ffa7cbae
AD
40
41 error_reporting(DEFAULT_ERROR_LEVEL);
02008cb1 42
e9338405
AD
43 $children = array();
44
02008cb1
AD
45 $last_checkpoint = -1;
46
5a613536 47 function reap_children() {
e9338405
AD
48 global $children;
49
50 $tmp = array();
51
52 foreach ($children as $pid) {
53 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
54 array_push($tmp, $pid);
55 } else {
56 _debug("[SIGCHLD] child $pid reaped.");
57 }
58 }
59
60 $children = $tmp;
61
5a613536
AD
62 return count($tmp);
63 }
64
65 function sigalrm_handler() {
a65a05a7 66 die("[SIGALRM] hang in feed update?\n");
5a613536
AD
67 }
68
69 function sigchld_handler($signal) {
70 $running_jobs = reap_children();
e9338405
AD
71
72 _debug("[SIGCHLD] jobs left: $running_jobs");
5a613536 73
02008cb1
AD
74 pcntl_waitpid(-1, $status, WNOHANG);
75 }
76
6a69e61f
AD
77 function sigint_handler() {
78 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
a65a05a7 79 die("[SIGINT] removing lockfile and exiting.\n");
6a69e61f
AD
80 }
81
ffa7cbae 82 pcntl_signal(SIGALRM, 'sigalrm_handler');
02008cb1 83 pcntl_signal(SIGCHLD, 'sigchld_handler');
6a69e61f 84
884c0a36
AD
85 if (file_is_locked("update_daemon.lock")) {
86 die("error: Can't create lockfile. ".
6a69e61f
AD
87 "Maybe another daemon is already running.\n");
88 }
02008cb1 89
884c0a36 90 if (!pcntl_fork()) {
0d6a7147
AD
91 pcntl_signal(SIGINT, 'sigint_handler');
92
45004d43 93 // Try to lock a file in order to avoid concurrent update.
884c0a36
AD
94 $lock_handle = make_lockfile("update_daemon.lock");
95
96 if (!$lock_handle) {
97 die("error: Can't create lockfile. ".
98 "Maybe another daemon is already running.\n");
99 }
100
101 while (true) { sleep(100); }
102 }
103
ffa7cbae
AD
104 // Testing database connection.
105 // It is unnecessary to start the fork loop if database is not ok.
106 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
107
108 if (!$link) {
109 if (DB_TYPE == "mysql") {
110 print mysql_error();
111 }
112 // PG seems to display its own errors just fine by default.
113 return;
114 }
115
116 db_close($link);
117
02008cb1
AD
118 while (true) {
119
45004d43
AD
120 // Since sleep is interupted by SIGCHLD, we need another way to
121 // respect the SPAWN_INTERVAL
02008cb1
AD
122 $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
123
ce1aa9b7 124 if ($next_spawn % 10 == 0) {
e9338405
AD
125 $running_jobs = count($children);
126 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
ce1aa9b7 127 }
02008cb1
AD
128
129 if ($last_checkpoint + SPAWN_INTERVAL < time()) {
130
5a613536
AD
131 reap_children();
132
e9338405 133 for ($j = count($children); $j < MAX_JOBS; $j++) {
02008cb1
AD
134 $pid = pcntl_fork();
135 if ($pid == -1) {
136 die("fork failed!\n");
137 } else if ($pid) {
e9338405
AD
138 _debug("[MASTER] spawned client $j [PID:$pid]...");
139 array_push($children, $pid);
02008cb1
AD
140 } else {
141 pcntl_signal(SIGCHLD, SIG_IGN);
6a69e61f 142 pcntl_signal(SIGINT, SIG_DFL);
ffa7cbae
AD
143
144 // ****** Updating RSS code *******
145 // Only run in fork process.
146
147 $start_timestamp = time();
148
149 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
150
151 if (!$link) {
152 if (DB_TYPE == "mysql") {
153 print mysql_error();
154 }
155 // PG seems to display its own errors just fine by default.
156 return;
157 }
158
f29ba148 159 init_connection($link);
ffa7cbae
AD
160
161 // We disable stamp file, since it is of no use in a multiprocess update.
162 // not really, tho for the time being -fox
163 if (!make_stampfile('update_daemon.stamp')) {
164 print "warning: unable to create stampfile";
165 }
166
ffa7cbae
AD
167 // FIXME : $last_purge is of no use in a multiprocess update.
168 // FIXME : We ALWAYS purge old posts.
3907ef71
AD
169 //_debug("Purging old posts (random 30 feeds)...");
170 //global_purge_old_posts($link, true, 30);
ffa7cbae 171
e3b54693
AD
172 // Call to the feed batch update function
173 // or regenerate feedbrowser cache
174
175 if (rand(0,100) > 50) {
176 update_daemon_common($link);
177 } else {
178 $count = update_feedbrowser_cache($link);
179 _debug("Finished, $count feeds processed.");
180 }
ffa7cbae 181
0d6a7147 182 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
abfa57fd 183
ffa7cbae
AD
184 db_close($link);
185
186 // We are in a fork.
187 // We wait a little before exiting to avoid to be faster than our parent process.
188 sleep(1);
189 // We exit in order to avoid fork bombing.
02008cb1
AD
190 exit(0);
191 }
0d6a7147
AD
192
193 // We wait a little time before the next fork, in order to let the first fork
194 // mark the feeds it update :
195 sleep(1);
02008cb1
AD
196 }
197 $last_checkpoint = time();
198 }
199 sleep(1);
200 }
201
202?>