orotool/src/timer_base.cpp

114 lines
3.2 KiB
C++

/* Copyright 2020, Michele Santullo
* This file is part of orotool.
*
* Orotool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Orotool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orotool. If not, see <http://www.gnu.org/licenses/>.
*/
#include "timer_base.hpp"
#include "roar11/ThreadPool.hpp"
#include "oro/api.hpp"
#include "oro/originsdb.hpp"
#include <cassert>
#include <iostream>
#include <chrono>
namespace duck {
namespace {
[[gnu::pure]]
unsigned long time_interval (const oro::Header& header, double extra) {
return header.retry_after / header.rate_limit + extra;
}
oro::Timestamp calc_next_update (const oro::Header& header, double extra) {
oro::Timestamp ret;
ret.ts =
std::chrono::time_point_cast<oro::timestamp_t::duration>(std::chrono::system_clock::now()) +
std::chrono::seconds(time_interval(header, extra));
return ret;
}
double initial_timer (oro::OriginsDB& db, oro::DBOperation op, double min_wait) {
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::seconds;
auto now = std::chrono::system_clock::now();
auto next_time = db.next_access_time(op);
duration<double> diff = duration_cast<seconds>(next_time.ts - now);
const auto retval = std::max(min_wait, std::min(3600.0 * 24.0, diff.count()));
return retval;
}
} //unnamed namespace
TimerBase::TimerBase (
const eve::Context& ctx,
oro::DBOperation type,
const TimerSettings& settings,
roar11::ThreadPool* pool,
oro::Api* oro_api,
oro::OriginsDB* db
) :
eve::Timer(initial_timer(*db, type, settings.min_wait), ctx),
m_extra_delay(settings.extra_delay),
m_pool(pool),
m_oro_api(oro_api),
m_db(db)
{
assert(m_pool);
assert(m_oro_api);
}
void TimerBase::on_timer() {
m_pool->submit(&TimerBase::fetch_data, this);
}
void TimerBase::set_next_timer (const oro::Header& header) {
const unsigned long next_timer = time_interval(header, m_extra_delay);
std::cout << "Next timer in " << next_timer << " secs\n";
this->set_timer(static_cast<double>(next_timer));
}
roar11::ThreadPool& TimerBase::pool() {
assert(m_pool);
return *m_pool;
}
oro::Api& TimerBase::oro_api() {
assert(m_oro_api);
return *m_oro_api;
}
oro::OriginsDB& TimerBase::db() {
assert(m_db);
return *m_db;
}
void TimerBase::update_db (const oro::Shops& shops, const oro::Header& header) {
db().update(shops, calc_next_update(header, m_extra_delay));
}
void TimerBase::update_db (const oro::Items& items, const oro::Header& header) {
db().update(items, calc_next_update(header, m_extra_delay));
}
void TimerBase::update_db (const oro::Icons& icons, const oro::Header& header) {
db().update(icons, calc_next_update(header, m_extra_delay));
}
void TimerBase::update_db (const oro::Creators& creators, const oro::Header& header) {
db().update(creators, calc_next_update(header, m_extra_delay));
}
} //namespace duck