Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Apr 14, 2024
1 parent 60a3947 commit 23be4b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
4 changes: 1 addition & 3 deletions nano/nano_node/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ void install_abort_signal_handler ()
#endif
}

volatile sig_atomic_t sig_int_or_term = 0;

constexpr std::size_t OPEN_FILE_DESCRIPTORS_LIMIT = 16384;
}

Expand Down Expand Up @@ -241,5 +239,5 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag
logger.critical (nano::log::type::daemon, "Error deserializing config: {}", error.get_message ());
}

logger.info (nano::log::type::daemon, "Daemon exiting");
logger.info (nano::log::type::daemon, "Daemon stopped");
}
38 changes: 19 additions & 19 deletions nano/nano_rpc/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>

#include <latch>

namespace
{
volatile sig_atomic_t sig_int_or_term = 0;

nano::logger logger{ "rpc_daemon" };

void run (std::filesystem::path const & data_path, std::vector<std::string> const & config_overrides)
Expand All @@ -41,7 +41,7 @@ void run (std::filesystem::path const & data_path, std::vector<std::string> cons
error = nano::read_tls_config_toml (data_path, *tls_config, logger);
if (error)
{
logger.critical (nano::log::type::daemon, "Error reading RPC TLS config: {}", error.get_message ());
logger.critical (nano::log::type::daemon_rpc, "Error reading RPC TLS config: {}", error.get_message ());
std::exit (1);
}
else
Expand All @@ -51,42 +51,42 @@ void run (std::filesystem::path const & data_path, std::vector<std::string> cons

std::shared_ptr<boost::asio::io_context> io_ctx = std::make_shared<boost::asio::io_context> ();

nano::signal_manager sigman;
runner = std::make_unique<nano::thread_runner> (io_ctx, rpc_config.rpc_process.io_threads);

try
{
nano::ipc_rpc_processor ipc_rpc_processor (*io_ctx, rpc_config);
auto rpc = nano::get_rpc (io_ctx, rpc_config, ipc_rpc_processor);
rpc->start ();

auto signal_handler = [io_ctx_w = std::weak_ptr{ io_ctx }] (int signum) {
logger.warn (nano::log::type::daemon, "Interrupt signal received, stopping...");
std::latch latch{ 1 };

if (auto io_ctx_l = io_ctx_w.lock ())
{
io_ctx_l->stop ();
}
sig_int_or_term = 1;
auto signal_handler = [&latch] (int signum) {
logger.warn (nano::log::type::daemon_rpc, "Interrupt signal received ({}), stopping...", nano::to_signal_name (signum));
latch.count_down ();
};

nano::signal_manager sigman;
sigman.register_signal_handler (SIGINT, signal_handler, true);
sigman.register_signal_handler (SIGTERM, signal_handler, false);

runner = std::make_unique<nano::thread_runner> (io_ctx, rpc_config.rpc_process.io_threads);
runner->join ();
// Keep running until latch is triggered
latch.wait ();

logger.info (nano::log::type::daemon_rpc, "Stopping...");

if (sig_int_or_term == 1)
{
rpc->stop ();
}
rpc->stop ();
io_ctx->stop ();
runner->join ();
}
catch (std::runtime_error const & e)
{
logger.critical (nano::log::type::daemon, "Error while running RPC: {}", e.what ());
logger.critical (nano::log::type::daemon_rpc, "Error while running RPC: {}", e.what ());
}
}
else
{
logger.critical (nano::log::type::daemon, "Error deserializing config: {}", error.get_message ());
logger.critical (nano::log::type::daemon_rpc, "Error deserializing config: {}", error.get_message ());
}

logger.info (nano::log::type::daemon_rpc, "Daemon stopped (RPC)");
Expand Down

0 comments on commit 23be4b2

Please sign in to comment.