/// <summary> /// Waits for resources to shutdown. /// </summary> protected void WaitForResourcesToShutdown() { TimeSpan waitTimeout = TimeSpan.FromSeconds(60); if (GlobalSettings.Items.ContainsKey(Setting.VmShutdownWaitTimeoutInSeconds)) { waitTimeout = TimeSpan.FromSeconds(int.Parse(GlobalSettings.Items[Setting.VmShutdownWaitTimeoutInSeconds])); } try { MapElement.UpdateStatus("ShutdownWait"); _resourcesShutdownWaitHandle.Wait(waitTimeout); TraceFactory.Logger.Info("Offline signal received"); } catch (TimeoutException ex) { // Log error, but continue TraceFactory.Logger.Error(ex); } finally { _resourcesShutdownWaitHandle = null; } }
/// <summary> /// Waits for resource to signal ready. /// </summary> protected void WaitForResourceToSignalReady() { try { MapElement.UpdateStatus("ReadyWait"); _resourcesReadyWaitHandle.Wait(WaitTimeout); TraceFactory.Logger.Info("Ready signal received"); } finally { _resourcesReadyWaitHandle = null; } }
/// <summary> /// Waits for host to register. /// </summary> protected void WaitForClientControllerToRegister() { try { MapElement.UpdateStatus("RegWait"); _registrationWaitHandle.Wait(WaitTimeout); TraceFactory.Logger.Info("Registration received"); MapElement.UpdateStatus("Registered"); } finally { _registrationWaitHandle = null; } }
/// <summary> /// Pauses this instance. /// </summary> public void Pause() { if (MapElement.State == RuntimeState.Running) { TraceFactory.Logger.Debug("Pausing all resources on {0}".FormatWith(Machine.Name)); // Only apply a pause to the resources that are still running. Others in a different state // will be ignored. Then only have the wait handle wait for those resources that were chosen var runningResources = Resources.Where(e => e.MapElement.State == RuntimeState.Running); try { if (runningResources.Any()) { _resourcesPausedWaitHandle = new ResettableCountdownEvent(runningResources.Count()); Parallel.ForEach <ResourceInstance>(runningResources, r => r.Pause()); MapElement.UpdateStatus("PauseWait"); _resourcesPausedWaitHandle.Wait(WaitTimeout); TraceFactory.Logger.Debug("All pause signals received on {0}, setting state to Paused".FormatWith(Machine.Name)); } else { TraceFactory.Logger.Debug("There are no running resources to pause"); } MapElement.UpdateStatus("Paused", RuntimeState.Paused); } catch (TimeoutException ex) { MapElement.UpdateStatus("Error", RuntimeState.Error); TraceFactory.Logger.Error(ex); } finally { _resourcesPausedWaitHandle = null; } } else { TraceFactory.Logger.Debug("{0} not in a running state".FormatWith(Machine.Name)); } }
/// <summary> /// Initializes the wait handles. /// </summary> protected void InitializeWaitHandles() { _resourcesReadyWaitHandle = new ResettableCountdownEvent(Resources.Count); _registrationWaitHandle = new ResettableCountdownEvent(1); }
/// <summary> /// Initializes the shutdown wait handle. /// </summary> protected void InitializeShutdownWaitHandle() { TraceFactory.Logger.Debug("Resource count {0}".FormatWith(Resources.Count)); _resourcesShutdownWaitHandle = new ResettableCountdownEvent(Resources.Count); }