Move TimerBase's g_extra_wait out to the settings file
This commit is contained in:
parent
93cfdd5586
commit
a3153bcc66
9 changed files with 52 additions and 15 deletions
|
@ -1,3 +1,6 @@
|
|||
[system]
|
||||
db_path=originsro.db3
|
||||
worker_threads=3
|
||||
|
||||
[rules]
|
||||
fetch_extra_delay=30
|
||||
|
|
|
@ -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<std::size_t>(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<std::size_t>(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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<oro::DBOperation::Shops> TimerShops;
|
||||
typedef TimerOroApi<oro::DBOperation::Items> TimerItems;
|
||||
typedef TimerOroApi<oro::DBOperation::Icons> 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<double>(extra_delay);
|
||||
|
||||
auto sig_int = worker.make_event<SignalInt>(&worker);
|
||||
|
||||
auto timer_items = worker.make_event<TimerItems>(0.0, &pool, api, db);
|
||||
auto timer_icons = worker.make_event<TimerIcons>(5.0, &pool, api, db);
|
||||
auto timer_shops = worker.make_event<TimerShops>(10.0, &pool, api, db);
|
||||
auto timer_creat = worker.make_event<TimerCreators>(15.0, &pool, api, db);
|
||||
auto timer_items = worker.make_event<TimerItems>(0.0, ed, &pool, api, db);
|
||||
auto timer_icons = worker.make_event<TimerIcons>(5.0, ed, &pool, api, db);
|
||||
auto timer_shops = worker.make_event<TimerShops>(10.0, ed, &pool, api, db);
|
||||
auto timer_creat = worker.make_event<TimerCreators>(15.0, ed, &pool, api, db);
|
||||
|
||||
worker.wait();
|
||||
#if !defined(NDEBUG)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
);
|
||||
|
||||
/*
|
||||
{
|
||||
|
|
|
@ -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<double>(next_timer));
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<oro::DBOperation Op>
|
|||
inline TimerOroApi<Op>::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)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue