control: Store/use trigger enable functions on events
Create and store trigger enablement functions on the events based on the
trigger's configuration so that they can be enabled separately from when
the event is created. This will allow actions that load events to be
able to be parsed and then enabled when the action determines that the
events should be enabled. It also supports the use of SIGHUP to reload
the event configuration since the events JSON configuration must
successfully be loaded before the newly created events can be enabled in
place of the currently enabled events.
Change-Id: I31871ee1691d5e6b26fe16cde2a829c426ad6504
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/triggers/init.cpp b/control/json/triggers/init.cpp
index 160b725..d21a918 100644
--- a/control/json/triggers/init.cpp
+++ b/control/json/triggers/init.cpp
@@ -19,6 +19,7 @@
#include "action.hpp"
#include "group.hpp"
#include "sdbusplus.hpp"
+#include "trigger_aliases.hpp"
#include <fmt/format.h>
@@ -119,9 +120,9 @@
}
}
-void triggerInit(const json& jsonObj, const std::string& eventName,
- Manager* mgr,
- std::vector<std::unique_ptr<ActionBase>>& actions)
+enableTrigger triggerInit(const json& jsonObj, const std::string& eventName,
+ Manager* mgr,
+ std::vector<std::unique_ptr<ActionBase>>& actions)
{
// Get the method handler if configured
auto handler = methods.end();
@@ -137,29 +138,37 @@
// Groups are optional, so a method is only required if there are groups
// i.e.) An init triggered event without any groups results in just
// running the actions
- for (const auto& group : action->getGroups())
+ if (!action->getGroups().empty() && handler == methods.end())
{
- if (handler == methods.end())
- {
- // Construct list of available methods
- auto availMethods = std::accumulate(
- std::next(methods.begin()), methods.end(),
- methods.begin()->first, [](auto list, auto method) {
- return std::move(list) + ", " + method.first;
- });
- auto msg =
- fmt::format("Event '{}' requires a supported method given "
- "to be init driven, available methods: {}",
- eventName, availMethods);
- log<level::ERR>(msg.c_str());
- throw std::runtime_error(msg.c_str());
- }
- // Call method handler for each group in the actions
- handler->second(mgr, group);
+ // Construct list of available methods
+ auto availMethods = std::accumulate(
+ std::next(methods.begin()), methods.end(),
+ methods.begin()->first, [](auto list, auto method) {
+ return std::move(list) + ", " + method.first;
+ });
+ auto msg =
+ fmt::format("Event '{}' requires a supported method given to "
+ "be init driven, available methods: {}",
+ eventName, availMethods);
+ log<level::ERR>(msg.c_str());
+ throw std::runtime_error(msg.c_str());
}
- // Run the action
- action->run();
}
+
+ return [handler = std::move(handler)](
+ const std::string& eventName, Manager* mgr,
+ std::vector<std::unique_ptr<ActionBase>>& actions) {
+ for (auto& action : actions)
+ {
+ for (const auto& group : action->getGroups())
+ {
+ // Call method handler for each group in the actions
+ handler->second(mgr, group);
+ }
+ // Run the action
+ action->run();
+ }
+ };
}
} // namespace phosphor::fan::control::json::trigger::init