79using shutdown_hook = std::function<void(std::chrono::milliseconds remaining_timeout)>;
176 std::chrono::milliseconds timeout = std::chrono::seconds(30));
221 const std::string& name,
266 const std::string& name,
375inline std::vector<unified_bootstrapper::shutdown_hook_entry>
377inline std::vector<unified_bootstrapper::module_entry>
381 std::lock_guard<std::mutex> lock(
mutex_);
393 if (core_result.is_err()) {
399 if (optional_result.is_err()) {
404 module.services_registered = false;
406 return optional_result;
425 std::lock_guard<std::mutex> lock(
mutex_);
430 "System is not initialized",
431 "di::unified_bootstrapper"
458 throw std::runtime_error(
459 "unified_bootstrapper: System is not initialized. "
460 "Call initialize() first."
475 const std::string& name,
478 std::lock_guard<std::mutex> lock(
mutex_);
483 "System is not initialized",
484 "di::unified_bootstrapper"
490 if (entry.name == name) {
493 "Shutdown hook already registered: " + name,
494 "di::unified_bootstrapper"
504 const std::string& name) {
506 std::lock_guard<std::mutex> lock(
mutex_);
510 return entry.name == name;
516 "Shutdown hook not found: " + name,
517 "di::unified_bootstrapper"
528 if (trigger_shutdown) {
534 std::lock_guard<std::mutex> lock(
mutex_);
543 const std::string& name,
546 std::lock_guard<std::mutex> lock(
mutex_);
549 for (
const auto& entry :
modules_) {
550 if (entry.name == name) {
553 "Module already registered: " + name,
554 "di::unified_bootstrapper"
562 auto result = fn(container);
563 if (!result.is_ok()) {
566 modules_.push_back({name, std::move(fn),
true});
569 modules_.push_back({name, std::move(fn),
false});
579 std::string(M::module_name()),
581 return r.register_services(container);
587 std::lock_guard<std::mutex> lock(
mutex_);
591 return entry.name == name;
597 "Module not found: " + name,
598 "di::unified_bootstrapper"
607 std::lock_guard<std::mutex> lock(
mutex_);
609 std::vector<std::string> names;
611 for (
const auto& entry :
modules_) {
612 names.push_back(entry.name);
618 std::lock_guard<std::mutex> lock(
mutex_);
622 return entry.name == name;
658#if KCENON_WITH_LOGGER_SYSTEM
667#if KCENON_WITH_MONITORING_SYSTEM
674#if KCENON_WITH_DATABASE_SYSTEM
681#if KCENON_WITH_NETWORK_SYSTEM
689 if (!module.services_registered) {
690 auto result =
module.registration_fn(container);
691 if (!result.is_ok()) {
694 "Module '" + module.name +
"' registration failed: "
695 + result.error().message,
696 "di::unified_bootstrapper"
699 module.services_registered = true;
711 "service_container_cleanup",
712 [](std::chrono::milliseconds) {
723 sigemptyset(&sa.sa_mask);
726 sigaction(SIGTERM, &sa,
nullptr);
727 sigaction(SIGINT, &sa,
nullptr);
744 std::chrono::milliseconds timeout) {
746 auto start = std::chrono::steady_clock::now();
750 auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
751 std::chrono::steady_clock::now() - start);
752 auto remaining = timeout - elapsed;
754 if (remaining <= std::chrono::milliseconds::zero()) {
Result type for error handling with member function support.
static Result< T > ok(U &&value)
Create a successful result with value (static factory)
Abstract interface for dependency injection containers.
Concrete implementation of IServiceContainer.
static service_container & global()
Get the global service container instance.
VoidResult clear() override
Clear all registrations.
Coordinates system initialization and shutdown.
static bool is_shutdown_requested()
Check if shutdown has been requested.
static std::atomic< bool > initialized_
static std::vector< std::string > registered_modules()
Get list of registered module names.
unified_bootstrapper()=delete
static bool is_initialized()
Check if the system is initialized.
static VoidResult unregister_module(const std::string &name)
Unregister a module.
static std::vector< shutdown_hook_entry > shutdown_hooks_
static void request_shutdown(bool trigger_shutdown=false)
Request graceful shutdown.
static bootstrapper_options get_options()
Get the initialization options.
unified_bootstrapper(const unified_bootstrapper &)=delete
static void setup_signal_handlers()
Set up signal handlers.
~unified_bootstrapper()=delete
static bool is_module_registered(const std::string &name)
Check if a module is registered.
static std::atomic< bool > shutdown_requested_
static VoidResult register_module(const std::string &name, module_registration_fn fn)
Register a module's service registration function.
static std::vector< module_entry > modules_
static VoidResult register_core_services()
Register core services that are always required.
static void signal_handler(int signal)
Signal handler function.
static VoidResult register_shutdown_hook(const std::string &name, shutdown_hook hook)
Register a shutdown hook.
static bootstrapper_options options_
static service_container & services()
Get the service container.
static VoidResult register_optional_services(const bootstrapper_options &opts)
Register optional services based on options.
static VoidResult shutdown(std::chrono::milliseconds timeout=std::chrono::seconds(30))
Shutdown the unified system gracefully.
static void setup_default_shutdown_hooks()
Set up default shutdown hooks.
unified_bootstrapper & operator=(const unified_bootstrapper &)=delete
static VoidResult initialize(const bootstrapper_options &opts={})
Initialize the unified system.
static VoidResult unregister_shutdown_hook(const std::string &name)
Unregister a shutdown hook.
static void execute_shutdown_hooks(std::chrono::milliseconds timeout)
Execute all shutdown hooks.
A class-based module registrar for ecosystem DI integration.
System module integration flags.
Standard logger interface for all systems.
std::function< void(std::chrono::milliseconds remaining_timeout)> shutdown_hook
Shutdown hook callback type.
std::function< VoidResult(IServiceContainer &)> module_registration_fn
Module registration callback type.
constexpr int ALREADY_EXISTS
constexpr int NOT_INITIALIZED
Result< T > make_error(int code, const std::string &message, const std::string &module="")
Create an error result with code and message.
Result< std::monostate > VoidResult
Specialized Result for void operations.
C++20 concepts for dependency injection and service container.
Implementation of the service container for dependency injection.
Configuration options for the unified bootstrapper.
bool enable_logging
Enable logging system services.
bool enable_database
Enable database system services.
bool register_signal_handlers
Register signal handlers (SIGTERM, SIGINT)
bool enable_network
Enable network system services.
bool enable_monitoring
Enable monitoring system services.
std::string config_path
Path to configuration file (optional)
std::chrono::milliseconds shutdown_timeout
Default shutdown timeout.
module_registration_fn registration_fn