Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
config_watcher_example.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
14
15#include <cstring>
16
19
20#include <chrono>
21#include <fstream>
22#include <iostream>
23#include <string>
24#include <thread>
25
26using namespace kcenon::common;
27using namespace kcenon::common::config;
28
29int main()
30{
31 std::cout << "=== Config Watcher Example ===\n\n";
32
33 // Create a sample config file
34 const std::string config_path = "example_config.yaml";
35 {
36 std::ofstream f(config_path);
37 f << "thread:\n"
38 << " pool_size: 4\n"
39 << " queue_type: adaptive\n"
40 << "logger:\n"
41 << " level: info\n"
42 << "monitoring:\n"
43 << " enabled: true\n"
44 << " metrics_interval: 30\n";
45 }
46
47 // Create watcher with history of 5 snapshots
48 std::cout << "1. Creating config watcher for '" << config_path << "'...\n";
49 config_watcher watcher(config_path, 5);
50
51 // Register change callback
52 watcher.on_change(
53 [](const unified_config& old_cfg, const unified_config& new_cfg)
54 {
55 std::cout << "\n [Change detected]\n";
56 std::cout << " Old pool_size: " << old_cfg.thread.pool_size << "\n";
57 std::cout << " New pool_size: " << new_cfg.thread.pool_size << "\n";
58 });
59
60 // Register error callback
61 watcher.on_error([](const std::string& error)
62 { std::cerr << " [Config error] " << error << "\n"; });
63
64 // Start watching
65 std::cout << "\n2. Starting file watcher...\n";
66 auto start_result = watcher.start();
67 if (start_result.is_ok())
68 {
69 std::cout << " Watcher running = " << (watcher.is_running() ? "true" : "false") << "\n";
70 }
71 else
72 {
73 std::cout << " Watcher start skipped (platform may not support file watching in this "
74 "environment)\n";
75 }
76
77 // Simulate a config change
78 std::cout << "\n3. Modifying config file...\n";
79 {
80 std::ofstream f(config_path);
81 f << "thread:\n"
82 << " pool_size: 8\n"
83 << " queue_type: lock_free\n"
84 << "logger:\n"
85 << " level: debug\n"
86 << "monitoring:\n"
87 << " enabled: true\n"
88 << " metrics_interval: 10\n";
89 }
90
91 // Give the watcher time to detect the change
92 std::this_thread::sleep_for(std::chrono::milliseconds(500));
93
94 // Manual reload
95 std::cout << "\n4. Manual reload...\n";
96 auto reload_result = watcher.reload();
97 std::cout << " Reload: " << (reload_result.is_ok() ? "success" : "skipped") << "\n";
98
99 // Stop watching
100 std::cout << "\n5. Stopping watcher...\n";
101 watcher.stop();
102 std::cout << " Watcher running = " << (watcher.is_running() ? "true" : "false") << "\n";
103
104 // Cleanup
105 std::remove(config_path.c_str());
106
107 std::cout << "\nDone.\n";
108 return 0;
109}
Monitors configuration files for changes and supports hot-reload.
bool is_running() const
Check if the watcher is currently running.
VoidResult reload()
Manually trigger a configuration reload.
VoidResult start()
Start watching the configuration file for changes.
void on_change(change_callback callback)
Register a callback for configuration changes.
void stop()
Stop watching the configuration file.
void on_error(error_callback callback)
Register a callback for reload errors.
Configuration hot-reload support with file system watching.
Core interfaces.
Definition adapter.h:21
size_t pool_size
Number of worker threads (default: hardware concurrency)
Root configuration structure for the unified system.
thread_config thread
Thread system configuration.
Unified configuration schema for the entire system.