From a3153bcc66d9dbf245fe4147a56774f5c7b12eea Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 16 Aug 2020 13:31:08 +0100 Subject: [PATCH] Move TimerBase's g_extra_wait out to the settings file --- orotool.conf | 3 +++ src/app_config.cpp | 31 +++++++++++++++++++++++++++---- src/app_config.hpp | 1 + src/evloop.cpp | 11 ++++++----- src/evloop.hpp | 2 +- src/main.cpp | 7 ++++++- src/timer_base.cpp | 6 +++--- src/timer_base.hpp | 2 ++ src/timer_oro_api.hpp | 4 +++- 9 files changed, 52 insertions(+), 15 deletions(-) diff --git a/orotool.conf b/orotool.conf index afdec61..6d92d66 100644 --- a/orotool.conf +++ b/orotool.conf @@ -1,3 +1,6 @@ [system] db_path=originsro.db3 worker_threads=3 + +[rules] +fetch_extra_delay=30 diff --git a/src/app_config.cpp b/src/app_config.cpp index 4a79476..51197a2 100644 --- a/src/app_config.cpp +++ b/src/app_config.cpp @@ -34,6 +34,10 @@ namespace { constexpr const char g_worker_threads_sect[] = "system"; constexpr const char g_worker_threads[] = "worker_threads"; + constexpr const char g_fetch_extra_delay_sect[] = "rules"; + constexpr const char g_fetch_extra_delay[] = "fetch_extra_delay"; + constexpr const char g_fetch_extra_delay_def[] = "30"; + std::string whole_ini() { std::ifstream input(g_config_file_path); input >> std::noskipws; @@ -61,6 +65,15 @@ namespace { else return it_setting->second; } + + void print_setting_read_err ( + std::string_view section, + std::string_view name, + const std::logic_error& err + ) { + std::cerr << "Error reading setting [" << section << "] \"" << name + << "\": " << err.what() << '\n'; + } } //unnamed namespace AppConfig::AppConfig() : @@ -75,9 +88,7 @@ std::string_view AppConfig::db_path() const { } std::size_t AppConfig::worker_threads() const { - std::string_view opt_name(g_worker_threads); - - std::string_view val = value_ifp(m_ini, g_worker_threads_sect, opt_name, g_def_worker_threads, false); + std::string_view val = value_ifp(m_ini, g_worker_threads_sect, g_worker_threads, g_def_worker_threads, false); if (val == "max") { return std::max(3U, std::thread::hardware_concurrency()) - 1; } @@ -88,10 +99,22 @@ std::size_t AppConfig::worker_threads() const { return std::max(2U, std::min(num, hard_max)); } catch (const std::logic_error& err) { - std::cerr << "Error reading setting " << opt_name << ": " << err.what() << '\n'; + print_setting_read_err(g_worker_threads_sect, g_worker_threads, err); return std::stoul(g_def_worker_threads); } } } +std::size_t AppConfig::fetch_extra_delay() const { + std::string_view val = value_ifp(m_ini, g_fetch_extra_delay_sect, g_fetch_extra_delay, g_fetch_extra_delay_def, false); + try { + const std::size_t num = std::stoul(std::string(val.data(), val.size())); + return std::min(3600 * 6, num); + } + catch (const std::logic_error& err) { + print_setting_read_err(g_fetch_extra_delay_sect, g_fetch_extra_delay, err); + return std::stoul(g_fetch_extra_delay_def); + } +} + } //namespace duck diff --git a/src/app_config.hpp b/src/app_config.hpp index 6a235a0..f555f7b 100644 --- a/src/app_config.hpp +++ b/src/app_config.hpp @@ -30,6 +30,7 @@ public: std::string_view db_path() const; std::size_t worker_threads() const; + std::size_t fetch_extra_delay() const; private: kamokan::IniFile m_ini; diff --git a/src/evloop.cpp b/src/evloop.cpp index c2caf5e..c88bb42 100644 --- a/src/evloop.cpp +++ b/src/evloop.cpp @@ -56,7 +56,7 @@ namespace { }; } //unnamed namespace -void test(oro::Api* api, oro::OriginsDB* db, std::size_t thread_count) { +void test(oro::Api* api, oro::OriginsDB* db, std::size_t extra_delay, std::size_t thread_count) { typedef TimerOroApi TimerShops; typedef TimerOroApi TimerItems; typedef TimerOroApi TimerIcons; @@ -67,13 +67,14 @@ void test(oro::Api* api, oro::OriginsDB* db, std::size_t thread_count) { eve::Eventia worker; pool.submit(worker.event_functor()); + const double ed = static_cast(extra_delay); auto sig_int = worker.make_event(&worker); - auto timer_items = worker.make_event(0.0, &pool, api, db); - auto timer_icons = worker.make_event(5.0, &pool, api, db); - auto timer_shops = worker.make_event(10.0, &pool, api, db); - auto timer_creat = worker.make_event(15.0, &pool, api, db); + auto timer_items = worker.make_event(0.0, ed, &pool, api, db); + auto timer_icons = worker.make_event(5.0, ed, &pool, api, db); + auto timer_shops = worker.make_event(10.0, ed, &pool, api, db); + auto timer_creat = worker.make_event(15.0, ed, &pool, api, db); worker.wait(); #if !defined(NDEBUG) diff --git a/src/evloop.hpp b/src/evloop.hpp index 4b9d314..ad1e0a8 100644 --- a/src/evloop.hpp +++ b/src/evloop.hpp @@ -26,6 +26,6 @@ namespace oro { namespace duck { -void test(oro::Api* api, oro::OriginsDB* db, std::size_t thread_count); +void test(oro::Api* api, oro::OriginsDB* db, std::size_t extra_delay, std::size_t thread_count); } //namespace duck diff --git a/src/main.cpp b/src/main.cpp index 6ced761..d150d9d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,7 +44,12 @@ int main(int argc, char* argv[]) { duck::AppConfig app_conf; oro::OriginsDB db(app_conf.db_path()); - duck::test(&oro_api, &db, app_conf.worker_threads()); + duck::test( + &oro_api, + &db, + app_conf.fetch_extra_delay(), + app_conf.worker_threads() + ); /* { diff --git a/src/timer_base.cpp b/src/timer_base.cpp index 1e12014..cc78da6 100644 --- a/src/timer_base.cpp +++ b/src/timer_base.cpp @@ -26,8 +26,6 @@ namespace duck { namespace { - constexpr const unsigned long g_extra_wait = 30; - [[gnu::pure]] unsigned long time_interval (const oro::Header& header) { return header.retry_after / header.rate_limit; @@ -58,11 +56,13 @@ TimerBase::TimerBase ( const eve::Context& ctx, oro::DBOperation type, double min_wait, + double extra_wait, roar11::ThreadPool* pool, oro::Api* oro_api, oro::OriginsDB* db ) : eve::Timer(initial_timer(*db, type, min_wait), ctx), + m_extra_wait(extra_wait), m_pool(pool), m_oro_api(oro_api), m_db(db) @@ -76,7 +76,7 @@ void TimerBase::on_timer() { } void TimerBase::set_next_timer (const oro::Header& header) { - const unsigned long next_timer = time_interval(header) + g_extra_wait; + const unsigned long next_timer = time_interval(header) + m_extra_wait; std::cout << "Next timer in " << next_timer << " secs\n"; this->set_timer(static_cast(next_timer)); } diff --git a/src/timer_base.hpp b/src/timer_base.hpp index b861c72..2ea3f2b 100644 --- a/src/timer_base.hpp +++ b/src/timer_base.hpp @@ -43,6 +43,7 @@ public: const eve::Context& ctx, oro::DBOperation type, double min_wait, + double extra_wait, roar11::ThreadPool* pool, oro::Api* oro_api, oro::OriginsDB* db @@ -64,6 +65,7 @@ protected: private: virtual void fetch_data() = 0; + double m_extra_wait; roar11::ThreadPool* m_pool; oro::Api* m_oro_api; oro::OriginsDB* m_db; diff --git a/src/timer_oro_api.hpp b/src/timer_oro_api.hpp index a8d1391..359fa51 100644 --- a/src/timer_oro_api.hpp +++ b/src/timer_oro_api.hpp @@ -34,6 +34,7 @@ public: TimerOroApi ( const eve::Context& ctx, double min_wait, + double extra_wait, roar11::ThreadPool* pool, oro::Api* oro_api, oro::OriginsDB* db @@ -71,11 +72,12 @@ template inline TimerOroApi::TimerOroApi ( const eve::Context& ctx, double min_wait, + double extra_wait, roar11::ThreadPool* pool, oro::Api* oro_api, oro::OriginsDB* db ) : - TimerBase(ctx, Op, min_wait, pool, oro_api, db) + TimerBase(ctx, Op, min_wait, extra_wait, pool, oro_api, db) { }