New options for cron uploader:
Logging level Including all logs in output Stop on error instead of continuing
This commit is contained in:
parent
e1aefb78ab
commit
86f7a06ed0
4 changed files with 60 additions and 21 deletions
|
@ -10,6 +10,15 @@ define("SCORE_LOG_INFO", 20);
|
|||
define("SCORE_LOG_DEBUG", 10);
|
||||
define("SCORE_LOG_NOTSET", 0);
|
||||
|
||||
const LOGGING_LEVEL_NAMES = [
|
||||
SCORE_LOG_NOTSET=>"Not Set",
|
||||
SCORE_LOG_DEBUG=>"Debug",
|
||||
SCORE_LOG_INFO=>"Info",
|
||||
SCORE_LOG_WARNING=>"Warning",
|
||||
SCORE_LOG_ERROR=>"Error",
|
||||
SCORE_LOG_CRITICAL=>"Critical",
|
||||
];
|
||||
|
||||
/**
|
||||
* A shorthand way to send a LogEvent
|
||||
*
|
||||
|
|
|
@ -3,17 +3,22 @@
|
|||
|
||||
abstract class CronUploaderConfig
|
||||
{
|
||||
const DEFAULT_PATH = "cron_uploader";
|
||||
public const DEFAULT_PATH = "cron_uploader";
|
||||
|
||||
const KEY = "cron_uploader_key";
|
||||
const DIR = "cron_uploader_dir";
|
||||
const USER = "cron_uploader_user";
|
||||
public const KEY = "cron_uploader_key";
|
||||
public const DIR = "cron_uploader_dir";
|
||||
public const USER = "cron_uploader_user";
|
||||
public const STOP_ON_ERROR = "cron_uploader_stop_on_error";
|
||||
public const INCLUDE_ALL_LOGS = "cron_uploader_include_all_logs";
|
||||
public const LOG_LEVEL = "cron_uploader_log_level";
|
||||
|
||||
public static function set_defaults(): void
|
||||
{
|
||||
global $config;
|
||||
$config->set_default_string(self::DIR, data_path(self::DEFAULT_PATH));
|
||||
|
||||
$config->set_default_bool(self::INCLUDE_ALL_LOGS, false);
|
||||
$config->set_default_bool(self::STOP_ON_ERROR, false);
|
||||
$config->set_default_int(self::LOG_LEVEL, SCORE_LOG_INFO);
|
||||
$upload_key = $config->get_string(self::KEY, "");
|
||||
if (empty($upload_key)) {
|
||||
$upload_key = self::generate_key();
|
||||
|
|
|
@ -15,6 +15,8 @@ class CronUploader extends Extension
|
|||
const UPLOADED_DIR = "uploaded";
|
||||
const FAILED_DIR = "failed_to_upload";
|
||||
|
||||
private static $IMPORT_RUNNING = false;
|
||||
|
||||
public function onInitExt(InitExtEvent $event)
|
||||
{
|
||||
// Set default values
|
||||
|
@ -58,6 +60,15 @@ class CronUploader extends Extension
|
|||
$sb->add_text_option(CronUploaderConfig::DIR, "Root dir", true);
|
||||
$sb->add_text_option(CronUploaderConfig::KEY, "Key", true);
|
||||
$sb->add_choice_option(CronUploaderConfig::USER, $users, "User", true);
|
||||
$sb->add_bool_option(CronUploaderConfig::STOP_ON_ERROR, "Stop On Error", true);
|
||||
$sb->add_choice_option(CronUploaderConfig::LOG_LEVEL, [
|
||||
LOGGING_LEVEL_NAMES[SCORE_LOG_DEBUG] => SCORE_LOG_DEBUG,
|
||||
LOGGING_LEVEL_NAMES[SCORE_LOG_INFO] => SCORE_LOG_INFO,
|
||||
LOGGING_LEVEL_NAMES[SCORE_LOG_WARNING] => SCORE_LOG_WARNING,
|
||||
LOGGING_LEVEL_NAMES[SCORE_LOG_ERROR] => SCORE_LOG_ERROR,
|
||||
LOGGING_LEVEL_NAMES[SCORE_LOG_CRITICAL] => SCORE_LOG_CRITICAL,
|
||||
], "Output Log Level: ", true);
|
||||
$sb->add_bool_option(CronUploaderConfig::INCLUDE_ALL_LOGS, "Include All Logs", true);
|
||||
$sb->end_table();
|
||||
$sb->add_label("<a href='$documentation_link'>Read the documentation</a> for cron setup instructions.");
|
||||
|
||||
|
@ -105,6 +116,23 @@ class CronUploader extends Extension
|
|||
}
|
||||
}
|
||||
|
||||
public function onLog(LogEvent $event)
|
||||
{
|
||||
global $config;
|
||||
if (self::$IMPORT_RUNNING &&
|
||||
$event->priority >= $config->get_int(CronUploaderConfig::LOG_LEVEL) &&
|
||||
($event->section==self::NAME || $config->get_bool(CronUploaderConfig::INCLUDE_ALL_LOGS))
|
||||
) {
|
||||
$output = "[" . date('Y-m-d H:i:s') . "] [" . LOGGING_LEVEL_NAMES[$event->priority] . "] " . $event->message ;
|
||||
|
||||
echo $output . "\r\n";
|
||||
flush_output();
|
||||
|
||||
$log_path = $this->get_log_file();
|
||||
file_put_contents($log_path, $output);
|
||||
}
|
||||
}
|
||||
|
||||
private function restage_folder(string $folder)
|
||||
{
|
||||
global $page;
|
||||
|
@ -275,7 +303,7 @@ class CronUploader extends Extension
|
|||
*/
|
||||
public function process_upload(string $key, ?int $upload_count = null): bool
|
||||
{
|
||||
global $database, $_shm_load_start;
|
||||
global $database, $config, $_shm_load_start;
|
||||
|
||||
$max_time = intval(ini_get('max_execution_time'))*.8;
|
||||
|
||||
|
@ -301,6 +329,7 @@ class CronUploader extends Extension
|
|||
throw new SCoreException("Cron upload process is already running");
|
||||
}
|
||||
|
||||
self::$IMPORT_RUNNING = true;
|
||||
try {
|
||||
//set_time_limit(0);
|
||||
|
||||
|
@ -338,9 +367,13 @@ class CronUploader extends Extension
|
|||
}
|
||||
|
||||
$failed++;
|
||||
$this->move_uploaded($img[0], $img[1], $output_subdir, true);
|
||||
$this->log_message(SCORE_LOG_ERROR, "(" . gettype($e) . ") " . $e->getMessage());
|
||||
$this->log_message(SCORE_LOG_ERROR, $e->getTraceAsString());
|
||||
if ($config->get_bool(CronUploaderConfig::STOP_ON_ERROR)) {
|
||||
break;
|
||||
} else {
|
||||
$this->move_uploaded($img[0], $img[1], $output_subdir, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -357,6 +390,7 @@ class CronUploader extends Extension
|
|||
|
||||
return true;
|
||||
} finally {
|
||||
self::$IMPORT_RUNNING = false;
|
||||
flock($lockfile, LOCK_UN);
|
||||
fclose($lockfile);
|
||||
}
|
||||
|
@ -494,14 +528,6 @@ class CronUploader extends Extension
|
|||
private function log_message(int $severity, string $message): void
|
||||
{
|
||||
log_msg(self::NAME, $severity, $message);
|
||||
|
||||
$time = "[" . date('Y-m-d H:i:s') . "]";
|
||||
|
||||
echo $time . " " . $message."\r\n";
|
||||
flush_output();
|
||||
|
||||
$log_path = $this->get_log_file();
|
||||
file_put_contents($log_path, $time . " " . $message);
|
||||
}
|
||||
|
||||
private function get_log_file(): string
|
||||
|
@ -517,5 +543,4 @@ class CronUploader extends Extension
|
|||
$page->set_type(MIME_TYPE_TEXT);
|
||||
$page->send_headers();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -234,11 +234,11 @@ class LogDatabase extends Extension
|
|||
{
|
||||
$sb = new SetupBlock("Logging (Database)");
|
||||
$sb->add_choice_option("log_db_priority", [
|
||||
"Debug" => SCORE_LOG_DEBUG,
|
||||
"Info" => SCORE_LOG_INFO,
|
||||
"Warning" => SCORE_LOG_WARNING,
|
||||
"Error" => SCORE_LOG_ERROR,
|
||||
"Critical" => SCORE_LOG_CRITICAL,
|
||||
LOGGING_LEVEL_NAMES[SCORE_LOG_DEBUG] => SCORE_LOG_DEBUG,
|
||||
LOGGING_LEVEL_NAMES[SCORE_LOG_INFO] => SCORE_LOG_INFO,
|
||||
LOGGING_LEVEL_NAMES[SCORE_LOG_WARNING] => SCORE_LOG_WARNING,
|
||||
LOGGING_LEVEL_NAMES[SCORE_LOG_ERROR] => SCORE_LOG_ERROR,
|
||||
LOGGING_LEVEL_NAMES[SCORE_LOG_CRITICAL] => SCORE_LOG_CRITICAL,
|
||||
], "Debug Level: ");
|
||||
$event->panel->add_block($sb);
|
||||
}
|
||||
|
|
Reference in a new issue