2020-05-20 09:40:49 +00:00
|
|
|
#include <common/logger_useful.h>
|
2020-06-29 15:41:17 +00:00
|
|
|
#include <Common/Exception.h>
|
2020-05-20 09:40:49 +00:00
|
|
|
#include <Storages/RabbitMQ/RabbitMQHandler.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-06-27 17:26:00 +00:00
|
|
|
/* The object of this class is shared between concurrent consumers (who share the same connection == share the same
|
|
|
|
* event loop and handler).
|
|
|
|
*/
|
2020-06-24 21:14:49 +00:00
|
|
|
RabbitMQHandler::RabbitMQHandler(uv_loop_t * loop_, Poco::Logger * log_) :
|
|
|
|
AMQP::LibUvHandler(loop_),
|
|
|
|
loop(loop_),
|
2020-08-08 16:45:52 +00:00
|
|
|
log(log_),
|
|
|
|
connection_running(false),
|
|
|
|
loop_state(Loop::STOP)
|
2020-05-20 09:40:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-01 12:52:00 +00:00
|
|
|
///Method that is called when the connection ends up in an error state.
|
2020-06-05 14:27:56 +00:00
|
|
|
void RabbitMQHandler::onError(AMQP::TcpConnection * connection, const char * message)
|
2020-05-20 09:40:49 +00:00
|
|
|
{
|
2020-05-26 20:43:20 +00:00
|
|
|
LOG_ERROR(log, "Library error report: {}", message);
|
2020-08-15 06:50:53 +00:00
|
|
|
connection_running.store(false);
|
2020-08-01 12:52:00 +00:00
|
|
|
if (connection)
|
|
|
|
connection->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RabbitMQHandler::onReady(AMQP::TcpConnection * /* connection */)
|
|
|
|
{
|
2020-08-08 16:45:52 +00:00
|
|
|
LOG_TRACE(log, "Connection is ready");
|
2020-08-15 06:50:53 +00:00
|
|
|
connection_running.store(true);
|
2020-08-08 16:45:52 +00:00
|
|
|
loop_state.store(Loop::RUN);
|
2020-05-20 09:40:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 16:44:04 +00:00
|
|
|
void RabbitMQHandler::startLoop()
|
2020-06-27 17:26:00 +00:00
|
|
|
{
|
2020-07-02 16:44:04 +00:00
|
|
|
std::lock_guard lock(startup_mutex);
|
2020-08-26 08:54:29 +00:00
|
|
|
LOG_DEBUG(log, "Background loop started");
|
2020-08-08 16:45:52 +00:00
|
|
|
while (loop_state.load() == Loop::RUN)
|
2020-06-27 17:26:00 +00:00
|
|
|
uv_run(loop, UV_RUN_NOWAIT);
|
2020-08-26 08:54:29 +00:00
|
|
|
|
|
|
|
LOG_DEBUG(log, "Background loop ended");
|
2020-06-27 17:26:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 16:44:04 +00:00
|
|
|
void RabbitMQHandler::iterateLoop()
|
2020-05-20 09:40:49 +00:00
|
|
|
{
|
2020-07-02 16:44:04 +00:00
|
|
|
std::unique_lock lock(startup_mutex, std::defer_lock);
|
|
|
|
if (lock.try_lock())
|
2020-06-29 15:41:17 +00:00
|
|
|
uv_run(loop, UV_RUN_NOWAIT);
|
2020-06-07 11:14:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 09:40:49 +00:00
|
|
|
}
|