16 #include <boost/program_options.hpp>
17 #include <fmt/chrono.h>
18 #include <fmt/format.h>
19 #include <fmt/ostream.h>
20 #include <rad/helper.hpp>
27 : m_logger(std::move(logger)), m_mgr(m_logger) {
28 m_mgr.
Register(&m_config.
name, {
"cfg/procname",
"Component instance ID"});
29 m_mgr.
Register(&m_config.
dataroot, {
"cfg/dataroot",
"ICS standard DATAROOT directory"});
30 m_mgr.
Register(&m_config.
workspace, {
"cfg/daq/workspace",
"DPM merge workspace"});
31 m_mgr.
Register(&m_config.
rr_uri, {
"cfg/req_endpoint",
"Request/reply URI"});
32 m_mgr.
Register(&m_config.
ps_uri, {
"cfg/pub_endpoint",
"Publish/subscribe URI"});
33 m_mgr.
Register(&m_config.
no_ipc, {
"cfg/no_ipc",
"Disables IPC interfaces"});
35 {
"cfg/oldb_uri_prefix",
"CII OLDB prefix 'cii.oldb:/elt/..'"});
36 m_mgr.
Register(&m_config.
db_timeout, {
"cfg/oldb_conn_timeout",
"CII OLDB connection timeout"});
38 m_mgr.
Register(&m_config.
log_config, {
"cfg/log_properties",
"Log configuration file"});
42 m_mgr.
Register(&m_config.
merge_bin, {
"cfg/bin_merge",
"DPM Merge application binary."});
43 m_mgr.
Register(&m_config.
rsync_bin, {
"cfg/bin_rsync",
"rsync application binary."});
45 m_mgr.
Register(&m_config.
limit_daq, {
"cfg/limits/daq",
"Concurrency limit: Number of DAQs"});
47 {
"cfg/limits/merge",
"Concurrency limit: Number of merges"});
49 {
"cfg/limits/net_send",
"Concurrency limit: Number of network send tranfers."});
52 {
"cfg/limits/net_receive",
"Concurrency limit: Number of network receive transfers."});
54 if (
auto root = rad::Helper::GetEnvVar(
"DATAROOT"); !root.empty()) {
55 m_mgr.
Update(&m_config.
dataroot, root, {config::Origin::EnvironmentVariable,
"$DATAROOT"});
60 namespace po = boost::program_options;
63 po::options_description options_desc(
"Options");
67 options_desc.add_options()
68 (
"help,h",
"Print help messages")
70 po::value<std::string>(),
73 po::value<std::string>(),
74 "Log level: ERROR, WARNING, STATE, EVENT, ACTION, INFO, DEBUG, TRACE")
76 po::value<std::string>(),
77 "Configuration filename")
79 po::value<std::string>(),
80 "In-memory DB host (ipaddr:port)")
82 po::value<std::string>(),
85 po::value<std::string>(),
86 "Publish/subscribe URI")
88 po::value<std::string>(),
89 "workspace path (relative paths are relateive to dataroot)")
91 "Disable request/reply, publish/subscribe and OLDB interfaces for standalone mode")
93 "Poll scheduler once and then exit rather than running until asked to quit (this "
94 "option also implies --no-ipc)")
97 po::variables_map options_map;
98 po::store(po::parse_command_line(argc, argv, options_desc), options_map);
99 if (options_map.count(
"help")) {
100 std::cerr << options_desc << std::endl;
103 po::notify(options_map);
105 if (options_map.count(
"config")) {
106 auto value = options_map[
"config"].as<std::string>();
110 if (options_map.count(
"log-level")) {
113 auto ll_str = options_map[
"log-level"].as<std::string>();
114 auto ll = boost::lexical_cast<LogLevel>(ll_str);
118 if (options_map.count(
"proc-name")) {
119 auto value = options_map[
"proc-name"].as<std::string>();
120 if (m_mgr.
Update(&m_config.
name, value, si)) {
123 "cii.oldb:/elt/" + m_config.
name,
128 if (options_map.count(
"db-host")) {
129 LOG4CPLUS_WARN(m_logger,
130 "Command line parameter --db-host|-d is deprecated and only provided "
131 "for compatibility");
133 if (options_map.count(
"rr-uri")) {
134 auto value = options_map[
"rr-uri"].as<std::string>();
137 if (options_map.count(
"ps-uri")) {
138 auto value = options_map[
"ps-uri"].as<std::string>();
141 if (options_map.count(
"workspace")) {
142 auto value = options_map[
"workspace"].as<std::string>();
145 if (options_map.count(
"no-ipc")) {
148 if (options_map.count(
"poll-once")) {
157 std::throw_with_nested(std::runtime_error(
"Failed to parse command line arguments"));
162 void ConfigManager::LoadConfig()
try {
163 std::string resolved = rad::Helper::FindFile(m_config.config_file);
164 LOG4CPLUS_INFO(m_logger,
"Loading configuration from " << resolved);
165 if (resolved.empty()) {
166 auto msg = fmt::format(
"Could not resolve config file {}", m_config.config_file);
167 LOG4CPLUS_ERROR(m_logger, msg);
168 throw std::invalid_argument(msg);
172 auto ifs = std::ifstream(resolved);
173 ifs.exceptions(std::ios_base::badbit);
175 elt::configng::CiiConfigDocument config;
176 config.UpdateFromStream(ifs);
177 auto const& root = config.GetInstanceRootNode();
180 UpdateFromConfig<std::string>(&m_config.dataroot, root, si);
181 UpdateFromConfig<std::string>(&m_config.workspace, root, si);
182 UpdateFromConfig<std::string>(&m_config.log_config, root, si);
183 UpdateFromConfig<std::string>(&m_config.rr_uri, root, si);
184 UpdateFromConfig<std::string>(&m_config.ps_uri, root, si);
185 UpdateFromConfig<std::string>(&m_config.merge_bin, root, si);
186 UpdateFromConfig<std::string>(&m_config.rsync_bin, root, si);
188 UpdateFromConfig<std::string>(&m_config.db_prefix, root, si);
189 UpdateFromConfig<int>(&m_config.db_timeout, root, si);
191 UpdateFromConfig<int>(&m_config.limit_daq, root, si);
192 UpdateFromConfig<int>(&m_config.limit_merge, root, si);
193 UpdateFromConfig<int>(&m_config.limit_net_receive, root, si);
194 UpdateFromConfig<int>(&m_config.limit_net_send, root, si);
197 std::throw_with_nested(std::runtime_error(
198 fmt::format(
"Failed to load configuration from {}", m_config.config_file)));
bool Update(AttrType *ptr, T const &value, OriginInfo const &origin)
Update configuration value taking into account the origin of the change.
void Register(AttrType *ptr, Metadata const &meta)
Registers a configuration parameter/attribute.
ConfigManager(log4cplus::Logger logger)
bool ParseArguments(int argc, char *argv[])
Parse configuration from command line arguments.
Declaration of log4cplus helpers.
@ Configuration
Configuration file.
@ Default
Built-in default value.
@ CommandLine
Command line argument.
std::optional< T > GetParamAs(std::string const &query, elt::configng::CiiConfigInstanceNode const &node)
Performs lookup of parameters in the form root/node/leaf relative to the provided node.
Mutable metadata about a configuration attribute that describes where a value comes from.
std::string name
Process instance name.
log4cplus::LogLevel log_level
std::string rsync_bin
rsync application name.
std::filesystem::path dataroot
Dataroot, normally this should be configured from environment variable $DATAROOT.
std::string merge_bin
Merge application name.
std::filesystem::path workspace
Workspace.
std::chrono::seconds db_timeout
bool no_ipc
If true (set by command line option only) it disables MAL service registration.
std::string rr_uri
Request/reply URI.
std::string ps_uri
Pub/sub URI.
std::string log_config
Log file, defaults to nothing.
bool poll_once
Run scheduler once.
std::filesystem::path config_file
Configuration file.
uint16_t limit_net_receive