#!/usr/bin/env php loadDatabase(); } Config::set('system', 'worker_daemon_mode', true); // Just to be sure that this script really runs endlessly set_time_limit(0); $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60; $do_cron = true; $last_cron = 0; // Now running as a daemon. while (true) { if (!$do_cron && ($last_cron + $wait_interval) < time()) { logger('Forcing cron worker call.', LOGGER_DEBUG); $do_cron = true; } Worker::spawnWorker($do_cron); if ($do_cron) { // We force a reconnect of the database connection. // This is done to ensure that the connection don't get lost over time. DBA::reconnect(); $last_cron = time(); } logger("Sleeping", LOGGER_DEBUG); $start = time(); do { $seconds = (time() - $start); // logarithmic wait time calculation. // Background: After jobs had been started, they often fork many workers. // To not waste too much time, the sleep period increases. $arg = (($seconds + 1) / ($wait_interval / 9)) + 1; $sleep = round(log10($arg) * 1000000, 0); usleep($sleep); $timeout = ($seconds >= $wait_interval); } while (!$timeout && !Worker::IPCJobsExists()); if ($timeout) { $do_cron = true; logger("Woke up after $wait_interval seconds.", LOGGER_DEBUG); } else { $do_cron = false; logger("Worker jobs are calling to be forked.", LOGGER_DEBUG); } } function shutdown() { posix_kill(posix_getpid(), SIGHUP); }