+ if ($schema_version != SCHEMA_VERSION) {
+ die("Schema version is wrong, please upgrade the database.\n");
+ }
+ }
+
+ Debug::set_enabled(true);
+ Debug::set_quiet(isset($options['quiet']));
+
+ if (isset($options["log-level"])) {
+ Debug::set_loglevel((int)$options["log-level"]);
+ }
+
+ if (isset($options["log"])) {
+ Debug::set_logfile($options["log"]);
+ Debug::log("Logging to " . $options["log"]);
+ }
+
+ if (!isset($options["daemon"])) {
+ $lock_filename = "update.lock";
+ } else {
+ $lock_filename = "update_daemon.lock";
+ }
+
+ if (isset($options["task"])) {
+ Debug::log("Using task id " . $options["task"]);
+ $lock_filename = $lock_filename . "-task_" . $options["task"];
+ }
+
+ if (isset($options["pidlock"])) {
+ $my_pid = $options["pidlock"];
+ $lock_filename = "update_daemon-$my_pid.lock";
+
+ }
+
+ Debug::log("Lock: $lock_filename");
+
+ $lock_handle = make_lockfile($lock_filename);
+ $must_exit = false;
+
+ if (isset($options["task"]) && isset($options["pidlock"])) {
+ $waits = $options["task"] * 5;
+ Debug::log("Waiting before update ($waits)");
+ sleep($waits);
+ }
+
+ // Try to lock a file in order to avoid concurrent update.
+ if (!$lock_handle) {
+ die("error: Can't create lockfile ($lock_filename). ".
+ "Maybe another update process is already running.\n");
+ }
+
+ if (isset($options["force-update"])) {
+ Debug::log("marking all feeds as needing update...");
+
+ $pdo->query( "UPDATE ttrss_feeds SET
+ last_update_started = '1970-01-01', last_updated = '1970-01-01'");
+ }
+
+ if (isset($options["feeds"])) {
+ RSSUtils::update_daemon_common();
+ RSSUtils::housekeeping_common(true);
+
+ PluginHost::getInstance()->run_hooks(PluginHost::HOOK_UPDATE_TASK, "hook_update_task", $op);
+ }
+
+ if (isset($options["feedbrowser"])) {
+ $count = RSSUtils::update_feedbrowser_cache();
+ print "Finished, $count feeds processed.\n";
+ }
+
+ if (isset($options["daemon"])) {
+ while (true) {
+ $quiet = (isset($options["quiet"])) ? "--quiet" : "";
+ $log = isset($options['log']) ? '--log '.$options['log'] : '';
+ $log_level = isset($options['log-level']) ? '--log-level '.$options['log-level'] : '';
+
+ passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop $quiet $log $log_level");
+
+ // let's enforce a minimum spawn interval as to not forkbomb the host
+ $spawn_interval = max(60, DAEMON_SLEEP_INTERVAL);
+
+ Debug::log("Sleeping for $spawn_interval seconds...");
+ sleep($spawn_interval);