uint32_t Ruleset::runOnce()

in src/oomd/engine/Ruleset.cpp [102:167]


uint32_t Ruleset::runOnce(OomdContext& context) {
  if (!enabled_) {
    return 0;
  }

  // If any DetectorGroup fires, then begin running action chain
  //
  // Note we're still check()'ing the detector groups so that any detectors
  // keeping sliding windows can update their window
  //
  // TODO(lnyng): Use prerun() for detector plugins to generate states, i.e.
  // store sliding window metrics, so we can break early in this loop. Need to
  // make sure time between prerun() and run() is short.
  bool run_actions = false;
  for (const auto& dg : detector_groups_) {
    if (dg->check(context, silenced_logs_) && !run_actions) {
      run_actions = true;
      context.setActionContext(
          {name_,
           dg->name(),
           Util::generateUuid(),
           std::chrono::steady_clock::now() +
               std::chrono::seconds(prekill_hook_timeout_)});
      context.setInvokingRuleset(this);
    }
  }

  OOMD_SCOPE_EXIT {
    context.setActionContext({"", "", "", std::nullopt});
    context.setInvokingRuleset(std::nullopt);
  };

  // run actions if now() == pause_actions_until_ because a delay of 0 should
  // not cause a pause.
  if (std::chrono::steady_clock::now() < pause_actions_until_) {
    return 0;
  }

  if (active_action_chain_state_ != std::nullopt) {
    // resume the action context from when the action chain was fired
    context.setActionContext(
        std::move(active_action_chain_state_->action_context));

    // clear active_async_plugin_ and save it to a temp
    BasePlugin& target = active_action_chain_state_->active_plugin;
    active_action_chain_state_ = std::nullopt;

    for (auto&& it = action_group_.begin(); it != action_group_.end(); ++it) {
      if (it->get() == &target) {
        return run_action_chain(it, action_group_.end(), context);
      }
    }
  }

  if (!run_actions) {
    return 0;
  }

  if (!(silenced_logs_ & LogSources::ENGINE)) {
    OLOG << "DetectorGroup=" << context.getActionContext().detectorgroup
         << " has fired for Ruleset=" << name_ << ". Running action chain.";
  }

  // Begin running action chain
  return run_action_chain(action_group_.begin(), action_group_.end(), context);
}