Skip to content

Commit

Permalink
Add better shutdown handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tuokri committed Feb 5, 2025
1 parent 030804c commit 9502bb7
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions build_commands_bot/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <format>
#include <iostream>
#include <memory>
#include <optional>
#include <random>
#include <string_view>
#include <thread>
Expand Down Expand Up @@ -33,6 +34,9 @@
#include <boost/redis/response.hpp>
#include <boost/redis/src.hpp>

// TODO: group this up after merge:
#include <boost/process/v2/process.hpp>

#include <dpp/dpp.h>

#include <spdlog/spdlog.h>
Expand All @@ -53,6 +57,7 @@ namespace chrono = std::chrono;
namespace asio = boost::asio;
namespace redis = boost::redis;
namespace cobalt = boost::cobalt;
namespace process = boost::process::v2;

using boost::asio::experimental::concurrent_channel;

Expand Down Expand Up @@ -429,12 +434,47 @@ int main()
// MessageChannel msg_channel{ioc.get_executor(), msg_channel_size};
auto msg_channel = std::make_shared<MessageChannel>(ioc.get_executor(), msg_channel_size);

std::thread bot_thread(bot_main, msg_channel);
const auto stop = [&msg_channel, &ioc]
(std::optional<boost::system::error_code> ec = std::nullopt,
std::optional<int> signal = std::nullopt)
{
if (g_logger)
{
const auto estr = (ec) ? std::to_string(ec.value().value()) : "notset";
const auto sstr = (signal) ? std::to_string(signal.value()) : "notset";
g_logger->info("stopping, ec={}, sig={}", estr, sstr);
}
if (g_bot)
{
g_bot->shutdown();
}
if (msg_channel)
{
msg_channel->cancel();
}
ioc.stop();
};

std::exception_ptr bot_main_ex;
std::thread bot_thread(
[&bot_main_ex, &msg_channel, &stop]()
{
try
{
bot_main(msg_channel);
}
catch (...)
{
stop();
bot_main_ex = std::current_exception();
}
});

asio::co_spawn(ioc, co_main(redis_cfg, msg_channel), [](std::exception_ptr e)
asio::co_spawn(ioc, co_main(redis_cfg, msg_channel), [&stop](std::exception_ptr e)
{
if (e)
{
stop();
std::rethrow_exception(e);
}
});
Expand All @@ -448,33 +488,41 @@ int main()
#endif // !BO_WINDOWS
};
signals.async_wait(
[&](auto, auto)
[&stop](boost::system::error_code ec, int signal)
{
g_bot->shutdown();
msg_channel->cancel();
ioc.stop();
stop(ec, signal);
});

// TODO: is there any need to make this multi-threaded too?
ioc.run();

bot_thread.join();

if (bot_main_ex)
{
std::rethrow_exception(bot_main_ex);
}

g_logger->info("exiting");

if (g_logger)
{
g_logger->flush();
}

return EXIT_SUCCESS;
}
catch (const std::exception& ex)
{
std::cout << std::format("unhandled exception: {}\n", ex.what());
std::cout << std::format("unhandled exception: {}", ex.what()) << std::endl;
if (debugger_present())
{
throw;
}
}
catch (...)
{
std::cout << "unhandled error\n";
std::cout << "unhandled error" << std::endl;
if (debugger_present())
{
throw;
Expand Down

0 comments on commit 9502bb7

Please sign in to comment.