main.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <csignal>
  2. #include <iostream>
  3. #include "runner_service.hpp"
  4. #include "logging/logger.hpp"
  5. #include "config/config_loader.hpp"
  6. #include "common/config_arg.hpp"
  7. using namespace smartbotic;
  8. std::atomic<bool> g_shutdown{false};
  9. void signalHandler(int signal) {
  10. LOG_INFO("Received signal {}, shutting down...", signal);
  11. g_shutdown = true;
  12. }
  13. int main(int argc, char* argv[]) {
  14. // Initialize logging
  15. logging::LogConfig log_config;
  16. log_config.name = "runner";
  17. log_config.level = logging::LogLevel::Info;
  18. logging::Logger::init(log_config);
  19. LOG_INFO("SmartBotic Runner starting...");
  20. // Load configuration
  21. const auto config_arg = common::parseConfigArg(argc, argv, "./config/runner.json");
  22. const std::filesystem::path config_path = config_arg.path;
  23. runner::RunnerServiceConfig config;
  24. if (std::filesystem::exists(config_path)) {
  25. config = runner::RunnerService::loadConfig(config_path);
  26. LOG_INFO("Loaded configuration from {}", config_path.string());
  27. } else if (config_arg.explicitly_given) {
  28. // See the webserver's main for why this is fatal rather than a warning.
  29. LOG_ERROR("Config file not found at {}. It was given on the command line, "
  30. "so refusing to start on built-in defaults", config_path.string());
  31. return 1;
  32. } else {
  33. LOG_WARN("Config file not found at {}, using defaults", config_path.string());
  34. }
  35. // Setup signal handlers
  36. std::signal(SIGINT, signalHandler);
  37. std::signal(SIGTERM, signalHandler);
  38. // Create and start service
  39. runner::RunnerService service(config);
  40. try {
  41. service.start();
  42. // Wait for shutdown signal
  43. while (!g_shutdown) {
  44. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  45. }
  46. service.stop();
  47. } catch (const std::exception& e) {
  48. LOG_ERROR("Fatal error: {}", e.what());
  49. return 1;
  50. }
  51. LOG_INFO("SmartBotic Runner stopped");
  52. logging::Logger::shutdown();
  53. return 0;
  54. }