From 764555ff8af2ca8f7aad5e882bbf837c6e65cbc7 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 21 Mar 2013 14:48:47 +0400 Subject: rework update.php to use getopt; allow --task parameter --- update.php | 91 +++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 34 deletions(-) (limited to 'update.php') diff --git a/update.php b/update.php index ba4cadbcb..a92b125e4 100755 --- a/update.php +++ b/update.php @@ -22,9 +22,26 @@ init_connection($link); - $op = $argv; + $longopts = array("feeds", + "feedbrowser", + "daemon", + "daemon-loop", + "task:", + "cleanup-tags", + "quiet", + "indexes", + "convert-filters", + "force-update", + "list-plugins", + "help"); + + foreach ($pluginhost->get_commands() as $command => $data) { + array_push($longopts, $command); + } + + $options = getopt("", $longopts); - if (count($argv) == 0 && !defined('STDIN')) { + if (count($options) == 0 && !defined('STDIN')) { ?> Tiny Tiny RSS data update script. @@ -43,36 +60,43 @@ exit; } - if (count($argv) == 1 || in_array("-help", $op) ) { + + if (count($options) == 0 || isset($options["help"]) ) { print "Tiny Tiny RSS data update script.\n\n"; print "Options:\n"; - print " -feeds - update feeds\n"; - print " -feedbrowser - update feedbrowser\n"; - print " -daemon - start single-process update daemon\n"; - print " -cleanup-tags - perform tags table maintenance\n"; - print " -quiet - don't show messages\n"; - print " -indexes - recreate missing schema indexes\n"; - print " -convert-filters - convert type1 filters to type2\n"; - print " -force-update - force update of all feeds\n"; - print " -list-plugins - list all available plugins\n"; - print " -help - show this help\n"; + print " --feeds - update feeds\n"; + print " --feedbrowser - update feedbrowser\n"; + print " --daemon - start single-process update daemon\n"; + print " --task N - create lockfile using this task id\n"; + print " --cleanup-tags - perform tags table maintenance\n"; + print " --quiet - don't show messages\n"; + print " --indexes - recreate missing schema indexes\n"; + print " --convert-filters - convert type1 filters to type2\n"; + print " --force-update - force update of all feeds\n"; + print " --list-plugins - list all available plugins\n"; + print " --help - show this help\n"; print "Plugin options:\n"; foreach ($pluginhost->get_commands() as $command => $data) { - printf(" %-19s - %s\n", "$command", $data["description"]); + printf(" --%-19s - %s\n", "$command", $data["description"]); } return; } - define('QUIET', in_array("-quiet", $op)); + define('QUIET', isset($options['quiet'])); - if (!in_array("-daemon", $op)) { + if (!isset($options["daemon"])) { $lock_filename = "update.lock"; } else { $lock_filename = "update_daemon.lock"; } + if (isset($options["task"])) { + _debug("Using task id " . $options["task"]); + $lock_filename = $lock_filename . "-task_" . $options["task"]; + } + $lock_handle = make_lockfile($lock_filename); $must_exit = false; @@ -82,7 +106,14 @@ "Maybe another update process is already running.\n"); } - if (in_array("-feeds", $op)) { + if (isset($options["force-update"])) { + _debug("marking all feeds as needing update..."); + + db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01', + last_updated = '1970-01-01'"); + } + + if (isset($options["feeds"])) { // Update all feeds needing a update. update_daemon_common($link); @@ -100,21 +131,20 @@ $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op); } - if (in_array("-feedbrowser", $op)) { + if (isset($options["feedbrowser"])) { $count = update_feedbrowser_cache($link); print "Finished, $count feeds processed.\n"; } - if (in_array("-daemon", $op)) { - $op = array_diff($op, array("-daemon")); + if (isset($options["daemon"])) { while (true) { - passthru(PHP_EXECUTABLE . " " . implode(' ', $op) . " -daemon-loop"); + passthru(PHP_EXECUTABLE . " " . $argv[0] ." --daemon-loop"); _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds..."); sleep(DAEMON_SLEEP_INTERVAL); } } - if (in_array("-daemon-loop", $op)) { + if (isset($options["daemon-loop"])) { if (!make_stampfile('update_daemon.stamp')) { die("error: unable to create stampfile\n"); } @@ -140,12 +170,12 @@ } - if (in_array("-cleanup-tags", $op)) { + if (isset($options["cleanup-tags"])) { $rc = cleanup_tags($link, 14, 50000); _debug("$rc tags deleted.\n"); } - if (in_array("-indexes", $op)) { + if (isset($options["indexes"])) { _debug("PLEASE BACKUP YOUR DATABASE BEFORE PROCEEDING!"); _debug("Type 'yes' to continue."); @@ -200,7 +230,7 @@ _debug("all done."); } - if (in_array("-convert-filters", $op)) { + if (isset($options["convert-filters"])) { _debug("WARNING: this will remove all existing type2 filters."); _debug("Type 'yes' to continue."); @@ -251,14 +281,7 @@ } - if (in_array("-force-update", $op)) { - _debug("marking all feeds as needing update..."); - - db_query($link, "UPDATE ttrss_feeds SET last_update_started = '1970-01-01', - last_updated = '1970-01-01'"); - } - - if (in_array("-list-plugins", $op)) { + if (isset($options["list-plugins"])) { $tmppluginhost = new PluginHost($link); $tmppluginhost->load_all($tmppluginhost::KIND_ALL); $enabled = array_map("trim", explode(",", PLUGINS)); @@ -280,7 +303,7 @@ } - $pluginhost->run_commands($op); + $pluginhost->run_commands($options); db_close($link); -- cgit v1.2.3-54-g00ecf From 2191eb7aab7686df01bee88ed18068897ce2912f Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 21 Mar 2013 14:56:04 +0400 Subject: update.php: add support for output logging --- include/functions.php | 18 ++++++++++++++---- update.php | 8 +++++++- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'update.php') diff --git a/include/functions.php b/include/functions.php index ac42a5d77..f259a7189 100644 --- a/include/functions.php +++ b/include/functions.php @@ -122,14 +122,24 @@ * @return void */ function _debug($msg) { - if (defined('QUIET') && QUIET) { - return; - } $ts = strftime("%H:%M:%S", time()); if (function_exists('posix_getpid')) { $ts = "$ts/" . posix_getpid(); } - print "[$ts] $msg\n"; + + if (!(defined('QUIET') && QUIET)) { + print "[$ts] $msg\n"; + } + + if (defined('LOGFILE')) { + $fp = fopen(LOGFILE, 'a+'); + + if ($fp) { + fputs($fp, "[$ts] $msg\n"); + fclose($fp); + } + } + } // function _debug /** diff --git a/update.php b/update.php index a92b125e4..9862e1ff2 100755 --- a/update.php +++ b/update.php @@ -29,6 +29,7 @@ "task:", "cleanup-tags", "quiet", + "log:", "indexes", "convert-filters", "force-update", @@ -60,7 +61,6 @@ exit; } - if (count($options) == 0 || isset($options["help"]) ) { print "Tiny Tiny RSS data update script.\n\n"; print "Options:\n"; @@ -70,6 +70,7 @@ print " --task N - create lockfile using this task id\n"; print " --cleanup-tags - perform tags table maintenance\n"; print " --quiet - don't show messages\n"; + print " --log FILE - log messages to FILE\n"; print " --indexes - recreate missing schema indexes\n"; print " --convert-filters - convert type1 filters to type2\n"; print " --force-update - force update of all feeds\n"; @@ -84,6 +85,11 @@ return; } + if (isset($options["log"])) { + _debug("Logging to " . $options["log"]); + define('LOGFILE', $options["log"]); + } + define('QUIET', isset($options['quiet'])); if (!isset($options["daemon"])) { -- cgit v1.2.3-54-g00ecf From dc24b520ccea0f092ea44f97352de20a796f4954 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 21 Mar 2013 15:05:57 +0400 Subject: update_daemon: use getopt; make things a bit more configurable, add help --- update.php | 6 +++--- update_daemon2.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 11 deletions(-) (limited to 'update.php') diff --git a/update.php b/update.php index 9862e1ff2..6850f1fb2 100755 --- a/update.php +++ b/update.php @@ -69,7 +69,7 @@ print " --daemon - start single-process update daemon\n"; print " --task N - create lockfile using this task id\n"; print " --cleanup-tags - perform tags table maintenance\n"; - print " --quiet - don't show messages\n"; + print " --quiet - don't output messages to stdout\n"; print " --log FILE - log messages to FILE\n"; print " --indexes - recreate missing schema indexes\n"; print " --convert-filters - convert type1 filters to type2\n"; @@ -85,13 +85,13 @@ return; } + define('QUIET', isset($options['quiet'])); + if (isset($options["log"])) { _debug("Logging to " . $options["log"]); define('LOGFILE', $options["log"]); } - define('QUIET', isset($options['quiet'])); - if (!isset($options["daemon"])) { $lock_filename = "update.lock"; } else { diff --git a/update_daemon2.php b/update_daemon2.php index 4b6a43999..7062e8e35 100755 --- a/update_daemon2.php +++ b/update_daemon2.php @@ -14,9 +14,6 @@ define('DAEMON_EXTENDED_DEBUG', true); } - define('PURGE_INTERVAL', 3600); // seconds - define('MAX_CHILD_RUNTIME', 600); // seconds - require_once "functions.php"; require_once "rssfuncs.php"; require_once "sanity_check.php"; @@ -24,8 +21,11 @@ require_once "db.php"; require_once "db-prefs.php"; + // defaults + define('PURGE_INTERVAL', 3600); // seconds + define('MAX_CHILD_RUNTIME', 600); // seconds define('MAX_JOBS', 2); - define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); + define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds if (!function_exists('pcntl_fork')) { die("error: This script requires PHP compiled with PCNTL module.\n"); @@ -118,6 +118,46 @@ pcntl_signal(SIGCHLD, 'sigchld_handler'); + $longopts = array("log:", + "tasks:", + "interval", + "help"); + + $options = getopt("", $longopts); + + if (isset($options["help"]) ) { + print "Tiny Tiny RSS update daemon.\n\n"; + print "Options:\n"; + print " --log FILE - log messages to FILE\n"; + print " --tasks N - amount of update tasks to spawn\n"; + print " default: " . MAX_JOBS . "\n"; + print " --interval N - task spawn interval\n"; + print " default: " . SPAWN_INTERVAL . " seconds.\n"; + print " --quiet - don't output messages to stdout\n"; + return; + } + + define('QUIET', isset($options['quiet'])); + + if (isset($options["tasks"])) { + _debug("Set to spawn " . $options["tasks"] . " children."); + $max_jobs = $option["tasks"]; + } else { + $max_jobs = MAX_JOBS; + } + + if (isset($options["interval"])) { + _debug("Spawn interval: " . $options["interval"] . " seconds."); + $spawn_interval = $option["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"); @@ -142,20 +182,20 @@ while (true) { // Since sleep is interupted by SIGCHLD, we need another way to - // respect the SPAWN_INTERVAL - $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time(); + // respect the spawn interval + $next_spawn = $last_checkpoint + $spawn_interval - time(); if ($next_spawn % 10 == 0) { $running_jobs = count($children); _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec."); } - if ($last_checkpoint + SPAWN_INTERVAL < time()) { + if ($last_checkpoint + $spawn_interval < time()) { check_ctimes(); reap_children(); - for ($j = count($children); $j < MAX_JOBS; $j++) { + for ($j = count($children); $j < $max_jobs; $j++) { $pid = pcntl_fork(); if ($pid == -1) { die("fork failed!\n"); -- cgit v1.2.3-54-g00ecf