Timer queries the remote api

This commit is contained in:
King_DuckZ 2020-08-09 21:07:54 +01:00
parent c0461f6b09
commit 8fa434e76e
5 changed files with 40 additions and 16 deletions

View file

@ -3,6 +3,7 @@
#include "eventia/eventia.hpp"
#include "eventia/timer.hpp"
#include "roar11/ThreadPool.hpp"
#include "oro/api.hpp"
#include <iostream>
namespace duck {
@ -26,14 +27,14 @@ void join(roar11::ThreadPool& pool) {
} //unnamed namespace
void test() {
void test(oro::Api* api) {
const std::size_t thread_count = 2U; //std::min(std::max(3U, std::thread::hardware_concurrency()) - 1, 4U);
std::cout << "Running with " << thread_count << " worker threads\n";
roar11::ThreadPool pool(thread_count);
eve::Eventia worker;
pool.submit(worker.event_functor());
auto fetcher = worker.make_timer<TimerItems>(3.0, &pool);
auto fetcher = worker.make_timer<TimerItems>(3.0, &pool, api);
join(pool);
}

View file

@ -1,7 +1,11 @@
#pragma once
namespace oro {
class Api;
} //namespace oro
namespace duck {
void test();
void test(oro::Api* api);
} //namespace duck

View file

@ -5,9 +5,6 @@
#include <iostream>
int main(int argc, char* argv[]) {
duck::test();
return 0;
/*
if (2 != argc) {
std::cerr << "Please provide your API key\n";
return 2;
@ -27,6 +24,9 @@ int main(int argc, char* argv[]) {
std::cout << "answer: " << ping.second.message << '\n';
std::cout << "version: " << ping.second.version << '\n';
duck::test(&oro_api);
/*
{
auto whoami = oro_api.who_am_i();
std::cout << "master id: " << whoami.second.master_id << '\n';
@ -69,7 +69,7 @@ int main(int argc, char* argv[]) {
}
std::cout << '\n';
}
*/
return 0;
*/
}

View file

@ -1,14 +1,23 @@
#include "timer_items.hpp"
#include "oro/api.hpp"
#include "eventia/private/context.hpp"
#include "roar11/ThreadPool.hpp"
#include <cassert>
#include <iostream>
namespace duck {
TimerItems::TimerItems (const eve::Context& ctx, double timeout, roar11::ThreadPool* pool) :
TimerItems::TimerItems (
const eve::Context& ctx,
double timeout,
roar11::ThreadPool* pool,
oro::Api* oro_api
) :
eve::Timer(timeout, ctx),
m_pool(pool)
m_pool(pool),
m_oro_api(oro_api)
{
assert(m_pool);
assert(m_oro_api);
}
void TimerItems::on_timer() {
@ -16,13 +25,13 @@ namespace duck {
}
void TimerItems::fetch_data() {
using namespace std::chrono_literals;
std::cout << "Timer elapsed! Doing fake work...\n";
std::this_thread::sleep_for(5s);
auto items = m_oro_api->items_list();
const double new_delay = 5.0;
std::cout << "Now starting next timer for " << new_delay << " seconds\n";
set_timer(new_delay);
{
const int next_timer = items.first.retry_after / items.first.rate_limit;
std::cout << "Next timer in " << next_timer << " secs\n";
set_timer(static_cast<double>(next_timer));
}
}
} //namespace duck

View file

@ -7,11 +7,20 @@ namespace roar11 {
class ThreadPool;
} //namespace roar11
namespace oro {
class Api;
} //namespace oro
namespace duck {
class TimerItems : eve::Timer {
public:
TimerItems (const eve::Context& ctx, double timeout, roar11::ThreadPool* pool);
TimerItems (
const eve::Context& ctx,
double timeout,
roar11::ThreadPool* pool,
oro::Api* oro_api
);
virtual void on_timer() override;
@ -19,6 +28,7 @@ private:
void fetch_data();
roar11::ThreadPool* m_pool;
oro::Api* m_oro_api;
};
} //namespace duck