private void ModuleChangedState(IServerModule module, ServerModuleState newState) { // Check if it switched to running if (!newState.HasFlag(ServerModuleState.Running)) { return; } // Now we start every service waiting on this service to return lock (WaitingModules) { if (!WaitingModules.ContainsKey(module)) { return; } // To increase boot speed we fork plugin start if more than one dependents was found foreach (var waitingModule in WaitingModules[module].ToArray()) { WaitingModules[module].Remove(waitingModule); StartModule(waitingModule); } // We remove this service for now after we started every dependend WaitingModules.Remove(module); } }
private void StateChange(ServerModuleState oldState, ServerModuleState newState) { if (StateChanged == null || oldState == newState) { return; } // Since event handling may take a while make sure we don't stop module execution foreach (var caller in StateChanged.GetInvocationList()) { ThreadPool.QueueUserWorkItem(delegate(object callObj) { try { var callDelegate = (Delegate)callObj; callDelegate.DynamicInvoke(this, new ModuleStateChangedEventArgs { OldState = oldState, NewState = newState }); } catch (Exception ex) { Logger?.LogException(LogLevel.Warning, ex, "Failed to notify listener of state change"); } }, caller); } }
internal static void PrintState(ServerModuleState state, bool line = false, int padRight = 0) { switch (state) { case ServerModuleState.Running: Console.ForegroundColor = ConsoleColor.Green; break; case ServerModuleState.Failure: Console.ForegroundColor = ConsoleColor.Red; break; case ServerModuleState.Starting: Console.ForegroundColor = ConsoleColor.DarkGreen; break; case ServerModuleState.Initializing: Console.ForegroundColor = ConsoleColor.DarkBlue; break; case ServerModuleState.Ready: Console.ForegroundColor = ConsoleColor.Blue; break; case ServerModuleState.Stopped: Console.ForegroundColor = ConsoleColor.Magenta; break; case ServerModuleState.Stopping: Console.ForegroundColor = ConsoleColor.DarkMagenta; break; default: Console.ResetColor(); break; } if (line) { Console.WriteLine(state.ToString().PadRight(padRight)); } else { Console.Write(state.ToString().PadRight(padRight)); } Console.ResetColor(); }
//[TestCase("OrderManager", ServerModuleState.Running, Description = "Check for OrderManager to start", ExpectedResult = false, ExpectedException = typeof(MoryxServiceNotFoundException))] public bool TestStartWaitStop(string service, ServerModuleState state) { PrintStartMsg("TestStartWaitStop('{0}', '{1}')", service, state); bool started = _hogController.StartApplication(); _hogController.CreateClients(); Assert.IsTrue(started, "Can't start HeartOfGold."); Assert.IsFalse(_hogController.Process.HasExited, "HeartOfGold has exited unexpectedly."); bool result = _hogController.WaitForService(service, state, 5); bool stopped = _hogController.StopHeartOfGold(10); Assert.IsTrue(stopped, "Can't stop HeartOfGold."); Assert.IsTrue(_hogController.Process.HasExited, "HeartOfGold did not stop."); return(result); }
/// <summary> /// Waits for a number of given services until they reach the given state. /// </summary> /// <param name="moduleNames">The services to wait for</param> /// <param name="state">The state that should be reached by the services.</param> /// <param name="timeoutInSeconds">The timeout for the services to reach the given state; in seconds.</param> /// <returns>True if all services reached the expected state, false otherwise</returns> public bool WaitForServices(string[] moduleNames, ServerModuleState state, double timeoutInSeconds) { // Calculate timeout time. DateTime timeout = DateTime.Now.AddSeconds(timeoutInSeconds); // Check service states until the timeout accrues or the given state has bin reached. while (DateTime.Now < timeout) { ServerModuleModel[] moduleModels; try { // Get all existing services of the heart of gold instance. moduleModels = _maintenanceClient.GetAll(); } catch (FaultException) { // This may happen while HeartOfGold is restarting services. Thread.Sleep(100); continue; } catch (WebException) { // This may happen while HeartOfGold is starting Thread.Sleep(100); continue; } catch (ServerTooBusyException) { // This may happen while HeartOfGold is starting Thread.Sleep(100); continue; } catch (Exception ex) { Console.WriteLine("Caught exception {0}", ex); throw; } // Will be set to false if any service didn't reached the given state. var statesReached = true; // Search the plugins with the given names foreach (var service in moduleNames) { var module = moduleModels.FirstOrDefault(m => m.Name == service); if (module == null) { throw new MoryxServiceNotFoundException($"No service found with name '{service}'!"); } // Check the state of the plugin if (module.HealthState == state) { continue; } statesReached = false; break; } // Check if all services have reached the given state if (statesReached) { return(true); } } return(false); }
/// <summary> /// Waits for a number of given services until they reach the given state. /// </summary> /// <param name="moduleNames">The services to wait for</param> /// <param name="state">The state that should be reached by the services.</param> /// <returns>True if all services reached the expected state, false otherwise</returns> public bool WaitForServices(string[] moduleNames, ServerModuleState state) { return(WaitForServices(moduleNames, state, 30.0)); }
/// <summary> /// Waits for a given service until it reaches the given state. /// </summary> /// <param name="service">The service to wait for</param> /// <param name="state">The state that should be reached by the service.</param> /// <param name="timeoutInSeconds">The timeout for the service to reach the given state; in seconds.</param> /// <returns>True if the service reached the expected state, false otherwise</returns> public bool WaitForService(string service, ServerModuleState state, double timeoutInSeconds) { string[] services = { service }; return(WaitForServices(services, state, timeoutInSeconds)); }
/// <summary> /// Waits for a given service until it reaches the given state. /// </summary> /// <param name="service">The service to wait for</param> /// <param name="state">The state that should be reached by the service.</param> /// <returns>True if the service reached the expected state, false otherwise</returns> public bool WaitForService(string service, ServerModuleState state) { string[] services = { service }; return(WaitForServices(services, state, 30.0)); }
/// <summary> /// Create a new instance of the <see cref="HealthStateException"/> to notify a facade user of an /// invalid operation /// </summary> /// <param name="current">Current state of the module</param> public HealthStateException(ServerModuleState current) { Current = current; }