Skip to content

Commit

Permalink
squash. server stands up, forks agent factory, and responds to shutdo…
Browse files Browse the repository at this point in the history
…wn "signals" - SIGINT / SIGTERM.
  • Loading branch information
korydraughn committed Sep 4, 2024
1 parent fe5ba2a commit 52ada7f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
13 changes: 8 additions & 5 deletions server/main_server/src/agent_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include <string>
#include <string_view>
#include <thread>
#include <span>

#ifdef __cpp_lib_filesystem
# include <filesystem>
Expand Down Expand Up @@ -167,6 +168,7 @@ int main(int _argc, char* _argv[])
// - shared memory for replica access table, dns cache, hostname cache?
// - delay server salt

// To see log messages from rsyslog, you must add irodsAgent5 to /etc/rsyslog.d/00-irods.conf.
init_logger(config);

log_af::info("{}: Initializing loggers for agent factory.", __func__);
Expand Down Expand Up @@ -198,11 +200,11 @@ int main(int _argc, char* _argv[])
log_af::info("{}: Initializing shared memory for agent factory.", __func__);

namespace hnc = irods::experimental::net::hostname_cache;
hnc::init("irods_hostname_cache", irods::get_hostname_cache_shared_memory_size());
hnc::init("irods_hostname_cache5", irods::get_hostname_cache_shared_memory_size());
irods::at_scope_exit deinit_hostname_cache{[] { hnc::deinit(); }};

namespace dnsc = irods::experimental::net::dns_cache;
dnsc::init("irods_dns_cache", irods::get_dns_cache_shared_memory_size());
dnsc::init("irods_dns_cache5", irods::get_dns_cache_shared_memory_size());
irods::at_scope_exit deinit_dns_cache{[] { dnsc::deinit(); }};

irods::experimental::replica_access_table::init();
Expand Down Expand Up @@ -276,17 +278,18 @@ int main(int _argc, char* _argv[])
unsigned int priority{};
#endif

log_af::info("{}: Waiting for client request.", __func__);
while (true) {
if (g_terminate) {
log_af::info("{}: Received shutdown instruction. Exiting agent factory main loop.", __func__);
// TODO Send shutdown message to main server process.
break;
}

if (g_reload_config) {
log_af::info("{}: Received configuration reload instruction. Reloading configuration.", __func__);
}

log_af::info("{}: Received configuration reload instruction. Reloading configuration.", __func__);
std::this_thread::sleep_for(std::chrono::seconds{1});
}

Expand Down Expand Up @@ -322,7 +325,7 @@ namespace
// SIGINT
struct sigaction sa_terminate; // NOLINT(cppcoreguidelines-pro-type-member-init)
sigemptyset(&sa_terminate.sa_mask);
sa_terminate.sa_flags = SIGHUP;
sa_terminate.sa_flags = 0;
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
sa_terminate.sa_handler = [](int) { g_terminate = 1; };
if (sigaction(SIGINT, &sa_terminate, nullptr) == -1) {
Expand All @@ -340,7 +343,7 @@ namespace
sa_sighup.sa_flags = 0;
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
sa_sighup.sa_handler = [](int) { g_reload_config = 1; };
if (sigaction(SIGTERM, &sa_sighup, nullptr) == -1) {
if (sigaction(SIGHUP, &sa_sighup, nullptr) == -1) {
return -1;
}
#if 0
Expand Down
44 changes: 27 additions & 17 deletions server/main_server/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int main(int _argc, char* _argv[])
("dump-config-template", "")
("dump-default-jsonschema", "")
("daemonize,d", "")
("pid-file,P", "")
("pid-file,P", po::value<std::string>(), "")
("help,h", "")
("version,v", "");
// clang-format on
Expand Down Expand Up @@ -184,11 +184,12 @@ int main(int _argc, char* _argv[])
// - delay server salt

init_logger(config);

// This message queue gives child processes a way to notify the parent process.
// This will only be used by the agent factory because iRODS 5.0 won't have a control plane.
// Or at least that's the plan.
constexpr const auto* mq_name = "irodsd_mq"; // TODO Make this name unique.
log_server::info("{}: Creating communication channel between main server process and agent factory.", __func__);
constexpr const auto* mq_name = "irodsServer5"; // TODO Make this name unique.
constexpr auto max_number_of_msg = 1; // Set to 1 to protect against duplicate/spamming of messages.
constexpr auto max_msg_size = 512;
boost::interprocess::message_queue::remove(mq_name);
Expand All @@ -199,8 +200,8 @@ int main(int _argc, char* _argv[])
log_server::info("{}: Launching Agent Factory.", __func__);
g_pid_af = fork();
if (0 == g_pid_af) {
char pname[] = "irodsAgent5";
char parent_mq_name[] = "irodsd_mq";
char pname[] = "/usr/sbin/irodsAgent5";
char parent_mq_name[] = "irodsServer5";
char* args[] = {pname, config_dir_path.data(), parent_mq_name, nullptr};
execv(pname, args);
_exit(1);
Expand Down Expand Up @@ -255,20 +256,29 @@ int main(int _argc, char* _argv[])
unsigned int priority{};

while (true) {
if (g_terminate) {
log_server::info("{}: Received shutdown instruction. Exiting agent factory main loop.", __func__);
break;
}

if (g_reload_config) {
log_server::info("{}: Received configuration reload instruction. Reloading configuration.", __func__);
g_reload_config = 0;
}
#if 0
// TODO Handle messages from agent factory: shutdown
msg_buf.fill(0);

// TODO Change to try_receive() or equivalent.
// This MUST NOT block.
pproc_mq.receive(msg_buf.data(), msg_buf.size(), recvd_size, priority);

std::string_view msg(msg_buf.data(), recvd_size);
fmt::print("irodsd: received message: [{}], recvd_size: [{}]\n", msg, recvd_size);

if (msg == "shutdown") {
fmt::print("Received shutdown instruction from control plane.\n");
break;
if (pproc_mq.try_receive(msg_buf.data(), msg_buf.size(), recvd_size, priority)) {
std::string_view msg(msg_buf.data(), recvd_size);
fmt::print("irodsd: received message: [{}], recvd_size: [{}]\n", msg, recvd_size);

if (msg == "shutdown") {
fmt::print("Received shutdown instruction from control plane.\n");
break;
}
}
#endif
std::this_thread::sleep_for(std::chrono::seconds{1});

// TODO Reap child processes: agent factory, delay server
// TODO Fork agent factory and/or delay server again if necessary.
Expand Down Expand Up @@ -486,7 +496,7 @@ Mandatory arguments to long options are mandatory for short options too.
sa_sighup.sa_flags = 0;
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
sa_sighup.sa_handler = [](int) { g_reload_config = 1; };
if (sigaction(SIGTERM, &sa_sighup, nullptr) == -1) {
if (sigaction(SIGHUP, &sa_sighup, nullptr) == -1) {
return -1;
}
#if 0
Expand Down

0 comments on commit 52ada7f

Please sign in to comment.