diff --git a/Tubio/DownloadManager.cpp b/Tubio/DownloadManager.cpp index b5942a7..92cc255 100644 --- a/Tubio/DownloadManager.cpp +++ b/Tubio/DownloadManager.cpp @@ -105,9 +105,10 @@ void DownloadManager::Update() DownloadNext(); } - // Check every second, non-blocking + // Update every 1-2 seconds, non-blocking, the download progresses if ((time(0) - lastProgressCheck > 2) && (cachedNumActiveDownloads > 0)) { + // Mutex gets reset in Save(); UpdateDownloadProgressPercentages(); } diff --git a/Tubio/Framework.cpp b/Tubio/Framework.cpp index 75ef4f0..bf434d8 100644 --- a/Tubio/Framework.cpp +++ b/Tubio/Framework.cpp @@ -42,8 +42,7 @@ void Framework::Run() { while (XGControl::keepServerRunning) { - httpServer->Update(); - DownloadManager::Update(); + Update(); } OnExit(); @@ -74,6 +73,15 @@ void Framework::PostInit() return; } +void Framework::Update() +{ + httpServer->Update(); + DownloadManager::Update(); + LogHistory::Update(); + + return; +} + void Framework::OnExit() { httpServer->OnExit(); @@ -86,9 +94,9 @@ void Framework::PostExit() { XGConfig::PostExit(); RestQueryHandler::PostExit(); - LogHistory::PostExit(); DownloadManager::PostExit(); ConsoleManager::PostExit(); + LogHistory::PostExit(); return; } diff --git a/Tubio/Framework.h b/Tubio/Framework.h index 7a6f3bf..20e26a1 100644 --- a/Tubio/Framework.h +++ b/Tubio/Framework.h @@ -15,9 +15,10 @@ public: void Run(); private: - void PostInit(); - void OnExit(); void PreInit(); + void PostInit(); + void Update(); + void OnExit(); void PostExit(); Rest::HttpServer* httpServer; diff --git a/Tubio/LogHistory.cpp b/Tubio/LogHistory.cpp index 3c3c1e7..a16efba 100644 --- a/Tubio/LogHistory.cpp +++ b/Tubio/LogHistory.cpp @@ -5,6 +5,8 @@ using namespace Logging; void LogHistory::PreInit() { history = new std::vector(); + lastSave = time(0); + didHistoryChangeSinceLastSave = false; return; } @@ -26,6 +28,17 @@ void LogHistory::PostExit() return; } +void LogHistory::Update() +{ + if ((time(0) - lastSave > XGConfig::logging.autosave_interval) && (didHistoryChangeSinceLastSave)) + { + // Mutex gets reset in Save(); + Save(); + } + + return; +} + void LogHistory::Save() { std::stringstream textfile; @@ -46,17 +59,23 @@ void LogHistory::Save() ofs << jsonFile.Render(); ofs.close(); + lastSave = time(0); + didHistoryChangeSinceLastSave = false; + return; } void LogHistory::AddLogToHistory(LogEntry* newEntry) { history->push_back(newEntry); + didHistoryChangeSinceLastSave = true; return; } std::vector* LogHistory::history; +time_t LogHistory::lastSave; +bool LogHistory::didHistoryChangeSinceLastSave; JasonPP::JsonBlock LogEntry::GetAsJson() { diff --git a/Tubio/LogHistory.h b/Tubio/LogHistory.h index ca2c166..cd4b000 100644 --- a/Tubio/LogHistory.h +++ b/Tubio/LogHistory.h @@ -29,6 +29,7 @@ namespace Logging public: static void PreInit(); static void PostExit(); + static void Update(); static void Save(); @@ -38,6 +39,8 @@ namespace Logging static void AddLogToHistory(LogEntry* newEntry); static std::vector* history; + static time_t lastSave; + static bool didHistoryChangeSinceLastSave; friend Logger; }; diff --git a/Tubio/XGConfig.cpp b/Tubio/XGConfig.cpp index 75ccb62..737469b 100644 --- a/Tubio/XGConfig.cpp +++ b/Tubio/XGConfig.cpp @@ -38,6 +38,7 @@ void XGConfig::InitializeDefaultValues() logging.logfile_text = "log.txt"; logging.logfile_json = "log.json"; + logging.autosave_interval = 60 * 30; // 30 mins downloader.cachedir = "dlcache"; downloader.max_dlrate_per_thread = "100M"; @@ -65,6 +66,11 @@ void XGConfig::LoadFromJson(const JasonPP::JsonBlock& json) logging.logfile_text = json.ShorthandGet("logging.logfile_text").AsString; } + if (IsJsonFieldValid(json, "logging.autosave_interval", JDType::INT)) + { + logging.autosave_interval = json.ShorthandGet("logging.autosave_interval").AsInt; + } + if (IsJsonFieldValid(json, "httpServer.port", JDType::STRING)) @@ -132,7 +138,8 @@ JsonBlock XGConfig::CreateJson() })), Ele("logging", JsonBlock({ Ele("logfile_text", logging.logfile_text), - Ele("logfile_json", logging.logfile_json) + Ele("logfile_json", logging.logfile_json), + Ele("autosave_interval", logging.autosave_interval) })), Ele("downloader", JsonBlock({ Ele("cachedir", downloader.cachedir), diff --git a/Tubio/XGConfig.h b/Tubio/XGConfig.h index a6ab945..d71567a 100644 --- a/Tubio/XGConfig.h +++ b/Tubio/XGConfig.h @@ -19,6 +19,7 @@ public: { std::string logfile_text; std::string logfile_json; + int autosave_interval; }; struct Downloader {