2020-05-20 09:40:49 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-06-02 13:15:53 +00:00
|
|
|
#include <thread>
|
2020-05-20 09:40:49 +00:00
|
|
|
#include <memory>
|
2020-06-02 13:15:53 +00:00
|
|
|
#include <mutex>
|
2020-05-20 09:40:49 +00:00
|
|
|
#include <amqpcpp.h>
|
|
|
|
#include <amqpcpp/linux_tcp.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2020-06-24 21:14:49 +00:00
|
|
|
#include <amqpcpp/libuv.h>
|
2020-05-20 09:40:49 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-08-08 16:45:52 +00:00
|
|
|
namespace Loop
|
|
|
|
{
|
|
|
|
static const UInt8 RUN = 1;
|
|
|
|
static const UInt8 STOP = 2;
|
|
|
|
}
|
|
|
|
|
2021-09-10 10:28:09 +00:00
|
|
|
using ChannelPtr = std::unique_ptr<AMQP::TcpChannel>;
|
2021-05-01 18:00:43 +00:00
|
|
|
|
2020-06-24 21:14:49 +00:00
|
|
|
class RabbitMQHandler : public AMQP::LibUvHandler
|
2020-05-20 09:40:49 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2020-06-30 01:48:11 +00:00
|
|
|
RabbitMQHandler(uv_loop_t * loop_, Poco::Logger * log_);
|
2020-08-28 08:52:02 +00:00
|
|
|
|
2020-05-20 09:40:49 +00:00
|
|
|
void onError(AMQP::TcpConnection * connection, const char * message) override;
|
2020-08-01 12:52:00 +00:00
|
|
|
void onReady(AMQP::TcpConnection * connection) override;
|
2020-06-29 15:41:17 +00:00
|
|
|
|
2021-05-04 16:26:47 +00:00
|
|
|
/// Loop for background thread worker.
|
2020-06-27 17:26:00 +00:00
|
|
|
void startLoop();
|
2021-05-04 16:26:47 +00:00
|
|
|
|
|
|
|
/// Loop to wait for small tasks in a non-blocking mode.
|
2021-05-04 19:57:45 +00:00
|
|
|
/// Adds synchronization with main background loop.
|
2020-07-02 16:44:04 +00:00
|
|
|
void iterateLoop();
|
2020-08-28 08:52:02 +00:00
|
|
|
|
2021-05-04 16:26:47 +00:00
|
|
|
/// Loop to wait for small tasks in a blocking mode.
|
2021-05-04 19:57:45 +00:00
|
|
|
/// No synchronization is done with the main loop thread.
|
2021-05-05 07:52:21 +00:00
|
|
|
void startBlockingLoop();
|
2021-05-04 16:26:47 +00:00
|
|
|
|
|
|
|
void stopLoop();
|
|
|
|
|
2021-09-11 17:38:55 +00:00
|
|
|
bool connectionRunning() const { return connection_running.load(); }
|
|
|
|
bool loopRunning() const { return loop_running.load(); }
|
2020-08-28 08:52:02 +00:00
|
|
|
|
2020-08-08 16:45:52 +00:00
|
|
|
void updateLoopState(UInt8 state) { loop_state.store(state); }
|
|
|
|
UInt8 getLoopState() { return loop_state.load(); }
|
2020-05-20 09:40:49 +00:00
|
|
|
|
|
|
|
private:
|
2020-06-24 21:14:49 +00:00
|
|
|
uv_loop_t * loop;
|
2020-05-20 09:40:49 +00:00
|
|
|
Poco::Logger * log;
|
2020-05-29 16:04:44 +00:00
|
|
|
|
2020-08-28 08:52:02 +00:00
|
|
|
std::atomic<bool> connection_running, loop_running;
|
2020-08-08 16:45:52 +00:00
|
|
|
std::atomic<UInt8> loop_state;
|
2020-07-02 16:44:04 +00:00
|
|
|
std::mutex startup_mutex;
|
2020-05-20 09:40:49 +00:00
|
|
|
};
|
|
|
|
|
2021-09-10 10:28:09 +00:00
|
|
|
using RabbitMQHandlerPtr = std::shared_ptr<RabbitMQHandler>;
|
|
|
|
|
2020-05-20 09:40:49 +00:00
|
|
|
}
|