GPUdb C++ API  Version 7.2.2.4
FailbackPollerService.hpp
Go to the documentation of this file.
1 #ifndef __FAILBACKPOLLERSERVICE_HPP__
2 #define __FAILBACKPOLLERSERVICE_HPP__
3 
4 #include "gpudb/GPUdb.hpp"
5 
6 #include <iostream>
7 #include <thread>
8 #include <atomic>
9 #include <mutex>
10 #include <memory>
11 #include <vector>
12 #include <chrono>
13 
14 
15 namespace gpudb {
17  public:
18  static constexpr int DEFAULT_START_DELAY = 0;
19  static constexpr int DEFAULT_POLLING_INTERVAL = 5; // seconds
20 
21  private:
22 
23  const gpudb::GPUdb& db;
24  std::string primaryUrl;
25  long pollingInterval;
26  std::atomic<bool> is_running;
27  std::mutex service_mutex;
28  std::unique_ptr<std::thread> scheduler;
29 
30  FailbackPollerService(const gpudb::GPUdb& db, long pollingInterval)
31  : db(db), primaryUrl(db.getPrimaryURL()), pollingInterval(pollingInterval), is_running(false) {}
32 
33  void polling_task();
34 
35  bool poll();
36 
37  void reset_cluster_pointers();
38 
39  void handle_exception(const std::exception& e);
40 
41  inline void log_debug(const std::string& msg) { std::cout << msg << std::endl; }
42  inline void log_info(const std::string& msg) { std::cout << msg << std::endl; }
43  inline void log_warn(const std::string& msg) { std::cout << msg << std::endl; }
44  inline void log_error(const std::string& msg) { std::cout << msg << std::endl; }
45 
46 
47  public:
48  static FailbackPollerService& get_instance(const gpudb::GPUdb& db, long pollingInterval = DEFAULT_POLLING_INTERVAL);
49 
50  // Delete copy constructor and assignment operator
51  FailbackPollerService(const FailbackPollerService&) = delete;
52  FailbackPollerService& operator=(const FailbackPollerService&) = delete;
53 
54  virtual ~FailbackPollerService();
55 
59  bool isRunning();
60 
64  void start();
65 
69  void stop();
70 
71  void restart();
72  };
73 
74 }
75 
76 #endif
static constexpr int DEFAULT_POLLING_INTERVAL
static constexpr int DEFAULT_START_DELAY
static FailbackPollerService & get_instance(const gpudb::GPUdb &db, long pollingInterval=DEFAULT_POLLING_INTERVAL)
FailbackPollerService & operator=(const FailbackPollerService &)=delete
void start()
Starts the poller service.
bool isRunning()
Checks whether the poller is running and returns true or false.
void stop()
Stops the poller service.