diff --git a/orotool.conf b/orotool.conf index f348eb8..af94dd4 100644 --- a/orotool.conf +++ b/orotool.conf @@ -7,3 +7,9 @@ backend=sqlite fetch_extra_delay=30 api_key=my_key_here store_raw_json=true + +[timing] +items=604800 +icons=604800 +shops=310 +creators=360 diff --git a/src/app_config.cpp b/src/app_config.cpp index b3e7e65..35296d8 100644 --- a/src/app_config.cpp +++ b/src/app_config.cpp @@ -17,6 +17,7 @@ #include "app_config.hpp" #include "orotool_config.hpp" +#include "duckhandy/int_conv.hpp" #include #include #include @@ -51,6 +52,22 @@ namespace { constexpr const char g_store_raw_json[] = "store_raw_json"; constexpr const char g_store_raw_json_def[] = "false"; + constexpr const char g_items_time_sect[] = "timing"; + constexpr const char g_items_time[] = "items"; + constexpr const char g_items_time_def[] = "604800"; + + constexpr const char g_icons_time_sect[] = "timing"; + constexpr const char g_icons_time[] = "icons"; + constexpr const char g_icons_time_def[] = "604800"; + + constexpr const char g_shops_time_sect[] = "timing"; + constexpr const char g_shops_time[] = "shops"; + constexpr const char g_shops_time_def[] = "600"; + + constexpr const char g_creators_time_sect[] = "timing"; + constexpr const char g_creators_time[] = "creators"; + constexpr const char g_creators_time_def[] = "600"; + bool equal (std::string_view a, std::string_view b) { return a.size() == b.size() and std::equal( a.begin(), a.end(), @@ -73,6 +90,11 @@ namespace { ); } + double to_double (std::string_view val) { + const auto seconds = dhandy::int_conv(val); + return static_cast(seconds); + } + std::string whole_ini() { std::ifstream input(g_config_file_path); input >> std::noskipws; @@ -166,4 +188,24 @@ bool AppConfig::store_raw_json() const { return to_bool(val); } +double AppConfig::items_timeout() const { + std::string_view val = value_ifp(m_ini, g_items_time_sect, g_items_time, g_items_time_def, false); + return to_double(val); +} + +double AppConfig::icons_timeout() const { + std::string_view val = value_ifp(m_ini, g_icons_time_sect, g_icons_time, g_icons_time_def, false); + return to_double(val); +} + +double AppConfig::shops_timeout() const { + std::string_view val = value_ifp(m_ini, g_shops_time_sect, g_shops_time, g_shops_time_def, false); + return to_double(val); +} + +double AppConfig::creators_timeout() const { + std::string_view val = value_ifp(m_ini, g_creators_time_sect, g_creators_time, g_creators_time_def, false); + return to_double(val); +} + } //namespace duck diff --git a/src/app_config.hpp b/src/app_config.hpp index 9b1f533..726f083 100644 --- a/src/app_config.hpp +++ b/src/app_config.hpp @@ -35,6 +35,11 @@ public: std::string_view backend() const; bool store_raw_json() const; + double items_timeout() const; + double icons_timeout() const; + double shops_timeout() const; + double creators_timeout() const; + private: kamokan::IniFile m_ini; }; diff --git a/src/evloop.cpp b/src/evloop.cpp index 7f7647b..0c2df0b 100644 --- a/src/evloop.cpp +++ b/src/evloop.cpp @@ -74,10 +74,15 @@ void test(oro::Api* api, oro::OriginsDB* db, const AppConfig& app_conf) { auto sig_int = worker.make_event(&worker); const bool rj = app_conf.store_raw_json(); - auto timer_items = worker.make_event(TSet{0.0, ed, rj}, &pool, api, db); - auto timer_icons = worker.make_event(TSet{5.0, ed, rj}, &pool, api, db); - auto timer_shops = worker.make_event(TSet{10.0, ed, rj}, &pool, api, db); - auto timer_creat = worker.make_event(TSet{15.0, ed, rj}, &pool, api, db); + const double w1 = app_conf.items_timeout(); + const double w2 = app_conf.icons_timeout(); + const double w3 = app_conf.shops_timeout(); + const double w4 = app_conf.creators_timeout(); + + auto timer_items = worker.make_event(TSet{w1, ed, rj}, &pool, api, db); + auto timer_icons = worker.make_event(TSet{w2, ed, rj}, &pool, api, db); + auto timer_shops = worker.make_event(TSet{w3, ed, rj}, &pool, api, db); + auto timer_creat = worker.make_event(TSet{w4, ed, rj}, &pool, api, db); worker.wait(); #if !defined(NDEBUG) diff --git a/src/timer_base.cpp b/src/timer_base.cpp index 0c51a25..27db452 100644 --- a/src/timer_base.cpp +++ b/src/timer_base.cpp @@ -27,15 +27,15 @@ namespace duck { namespace { [[gnu::pure]] - unsigned long time_interval (const oro::Header& header, double extra) { - return header.retry_after / header.rate_limit + extra; + unsigned long time_interval (const oro::Header& header, double min_wait, double extra) { + return std::max(header.retry_after / header.rate_limit + extra, min_wait); } - oro::Timestamp calc_next_update (const oro::Header& header, double extra) { + oro::Timestamp calc_next_update (const oro::Header& header, double min_wait, double extra) { oro::Timestamp ret; ret.ts = std::chrono::time_point_cast(std::chrono::system_clock::now()) + - std::chrono::seconds(time_interval(header, extra)); + std::chrono::seconds(time_interval(header, min_wait, extra)); return ret; } @@ -60,8 +60,9 @@ TimerBase::TimerBase ( oro::Api* oro_api, oro::OriginsDB* db ) : - eve::Timer(initial_timer(*db, type, settings.min_wait), ctx), + eve::Timer(initial_timer(*db, type, 2.0), ctx), m_extra_delay(settings.extra_delay), + m_min_wait(settings.min_wait), m_pool(pool), m_oro_api(oro_api), m_db(db), @@ -76,7 +77,7 @@ void TimerBase::on_timer() { } void TimerBase::set_next_timer (const oro::Header& header) { - const unsigned long next_timer = time_interval(header, m_extra_delay); + const unsigned long next_timer = time_interval(header, m_min_wait, m_extra_delay); std::cout << "Next timer in " << next_timer << " secs\n"; this->set_timer(static_cast(next_timer)); } @@ -97,19 +98,19 @@ oro::OriginsDB& TimerBase::db() { } void TimerBase::update_db (const oro::Shops& shops, const oro::Header& header, const oro::Source& source) { - db().update(shops, calc_next_update(header, m_extra_delay), source); + db().update(shops, calc_next_update(header, m_min_wait, m_extra_delay), source); } void TimerBase::update_db (const oro::Items& items, const oro::Header& header, const oro::Source& source) { - db().update(items, calc_next_update(header, m_extra_delay), source); + db().update(items, calc_next_update(header, m_min_wait, m_extra_delay), source); } void TimerBase::update_db (const oro::Icons& icons, const oro::Header& header, const oro::Source& source) { - db().update(icons, calc_next_update(header, m_extra_delay), source); + db().update(icons, calc_next_update(header, m_min_wait, m_extra_delay), source); } void TimerBase::update_db (const oro::Creators& creators, const oro::Header& header, const oro::Source& source) { - db().update(creators, calc_next_update(header, m_extra_delay), source); + db().update(creators, calc_next_update(header, m_min_wait, m_extra_delay), source); } } //namespace duck diff --git a/src/timer_base.hpp b/src/timer_base.hpp index 8cbbd41..f174a93 100644 --- a/src/timer_base.hpp +++ b/src/timer_base.hpp @@ -78,6 +78,7 @@ private: virtual void fetch_data(bool with_raw) = 0; double m_extra_delay; + double m_min_wait; roar11::ThreadPool* m_pool; oro::Api* m_oro_api; oro::OriginsDB* m_db;