Esempio n. 1
0
 for ($j = count($children); $j < MAX_JOBS; $j++) {
     $pid = pcntl_fork();
     if ($pid == -1) {
         die("fork failed!\n");
     } else {
         if ($pid) {
             _debug("[MASTER] spawned client {$j} [PID:{$pid}]...");
             array_push($children, $pid);
             $ctimes[$pid] = time();
         } else {
             pcntl_signal(SIGCHLD, SIG_IGN);
             pcntl_signal(SIGINT, 'task_sigint_handler');
             register_shutdown_function('task_shutdown');
             $my_pid = posix_getpid();
             $lock_filename = "update_daemon-{$my_pid}.lock";
             $lock_handle = make_lockfile($lock_filename);
             if (!$lock_handle) {
                 die("error: Can't create lockfile ({$lock_filename}). " . "Maybe another daemon is already running.\n");
             }
             // ****** Updating RSS code *******
             // Only run in fork process.
             $start_timestamp = time();
             $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
             if (!init_connection($link)) {
                 return;
             }
             // We disable stamp file, since it is of no use in a multiprocess update.
             // not really, tho for the time being -fox
             if (!make_stampfile('update_daemon.stamp')) {
                 print "warning: unable to create stampfile";
             }
}
if (isset($options["interval"])) {
    _debug("Spawn interval: " . $options["interval"] . " seconds.");
    $spawn_interval = $options["interval"];
} else {
    $spawn_interval = SPAWN_INTERVAL;
}
if (isset($options["log"])) {
    _debug("Logging to " . $options["log"]);
    define('LOGFILE', $options["log"]);
}
if (file_is_locked("update_daemon.lock")) {
    die("error: Can't create lockfile. " . "Maybe another daemon is already running.\n");
}
// Try to lock a file in order to avoid concurrent update.
$lock_handle = make_lockfile("update_daemon.lock");
if (!$lock_handle) {
    die("error: Can't create lockfile. " . "Maybe another daemon is already running.\n");
}
$schema_version = get_schema_version();
if ($schema_version != SCHEMA_VERSION) {
    die("Schema version is wrong, please upgrade the database.\n");
}
// Protip: children close shared database handle when terminating, it's a bad idea to
// do database stuff on main process from now on.
while (true) {
    // Since sleep is interupted by SIGCHLD, we need another way to
    // respect the spawn interval
    $next_spawn = $last_checkpoint + $spawn_interval - time();
    if ($next_spawn % 60 == 0) {
        $running_jobs = count($children);