public static EventAction_T GetEventAction(MonitActionType failed, MonitActionType succeeded, Func <bool> command1 = null, Func <bool> command2 = null) { var ea = new EventAction_T(); ea.failed = new Action_T(); ea.succeeded = new Action_T(); ea.failed.id = failed; ea.failed.count = 1; ea.failed.cycles = 1; if (failed == MonitActionType.Action_Exec) { ea.failed.exec = command1; } ea.succeeded.id = succeeded; ea.succeeded.count = 1; ea.succeeded.cycles = 1; if (succeeded == MonitActionType.Action_Exec) { ea.succeeded.exec = command2; } return(ea); }
/* * This is an in-fix recursive function called before s is started to * stop every service that depends on s, in reverse order *or* after s * was started to start again every service that depends on s. The * action parametere controls if this function should start or stop * the procceses that depends on s. * @param s A Service_T object * @param action An action to do on the dependant services * @param flag A Custom flag */ private static void _doDepend(Service_T s, MonitActionType action, bool flag) { foreach (var child in MonitWindowsAgent.servicelist) { if (child.dependantlist != null) { foreach (var d in child.dependantlist) { if (d == s.name) { if (action == MonitActionType.Action_Start) { _doStart(child); } else if (action == MonitActionType.Action_Monitor) { _doMonitor(child, flag); } _doDepend(child, action, flag); if (action == MonitActionType.Action_Stop) { _doStop(child, flag); } else if (action == MonitActionType.Action_Unmonitor) { _doUnmonitor(child, flag); } break; } } } } }
/// <summary> /// Check to see if we should try to start/stop service /// </summary> /// <param name="S">A service name as stated in the config file</param> /// <param name="A">An action id describing the action to execute</param> /// <returns>false for error, otherwise true</returns> public static bool ControlService(string S, MonitActionType A) { Service_T s = null; if ((s = Util.GetService(S)) == null) { Logger.Log.ErrorFormat("Service '{0}' -- doesn't exist", S); return(false); } switch (A) { case MonitActionType.Action_Start: _doDepend(s, MonitActionType.Action_Stop, false); _doStart(s); _doDepend(s, MonitActionType.Action_Start, false); break; case MonitActionType.Action_Stop: _doDepend(s, MonitActionType.Action_Stop, true); _doStop(s, true); break; case MonitActionType.Action_Restart: Logger.Log.InfoFormat("'{0}' trying to restart", s.name); _doDepend(s, MonitActionType.Action_Stop, false); if (s.restart != null) { _doRestart(s); _doDepend(s, MonitActionType.Action_Start, false); } else { if (_doStop(s, false)) { /* Only start if stop succeeded */ _doStart(s); _doDepend(s, MonitActionType.Action_Start, false); } else { /* enable monitoring of this service again to allow the restart retry in the next cycle up to timeout limit */ Util.MonitorSet(s); } } break; case MonitActionType.Action_Monitor: /* We only enable monitoring of this service and all prerequisite services. Chain of services which depends on this service keep its state */ _doMonitor(s, false); break; case MonitActionType.Action_Unmonitor: /* We disable monitoring of this service and all services which depends on it */ _doDepend(s, MonitActionType.Action_Unmonitor, false); _doUnmonitor(s, false); break; default: Logger.Log.ErrorFormat("Service '{0}' -- invalid action {1}", S, A); return(false); } return(true); }