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/eventia.hpp"
#include "eventia/timer.hpp" #include "eventia/timer.hpp"
#include "roar11/ThreadPool.hpp" #include "roar11/ThreadPool.hpp"
#include "oro/api.hpp"
#include <iostream> #include <iostream>
namespace duck { namespace duck {
@ -26,14 +27,14 @@ void join(roar11::ThreadPool& pool) {
} //unnamed namespace } //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); 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"; std::cout << "Running with " << thread_count << " worker threads\n";
roar11::ThreadPool pool(thread_count); roar11::ThreadPool pool(thread_count);
eve::Eventia worker; eve::Eventia worker;
pool.submit(worker.event_functor()); 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); join(pool);
} }

View file

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

View file

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

View file

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

View file

@ -7,11 +7,20 @@ namespace roar11 {
class ThreadPool; class ThreadPool;
} //namespace roar11 } //namespace roar11
namespace oro {
class Api;
} //namespace oro
namespace duck { namespace duck {
class TimerItems : eve::Timer { class TimerItems : eve::Timer {
public: 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; virtual void on_timer() override;
@ -19,6 +28,7 @@ private:
void fetch_data(); void fetch_data();
roar11::ThreadPool* m_pool; roar11::ThreadPool* m_pool;
oro::Api* m_oro_api;
}; };
} //namespace duck } //namespace duck