/// <summary> /// Builds all components in this map. /// </summary> public override void Stage(ParallelLoopState loopState) { Thread.CurrentThread.SetName("Stage-{0}".FormatWith(Thread.CurrentThread.ManagedThreadId)); TraceFactory.Logger.Debug("Entering..."); // Use the index to create an initial unique hostname that will be // replaced during the validation stage with an actual hostname. foreach (var manifest in Configuration.ManifestSet) { ResourceHost host = new ResourceHost(manifest); host.OnResourcesComplete += new EventHandler(ResourceHostsCompleted); Hosts.Add(host); } try { Parallel.ForEach <ResourceHost>(Hosts, (h, l) => h.Stage(l)); if (!SessionMapElement.AllElementsSetTo <ResourceHost>(Hosts, RuntimeState.Available)) { loopState.Break(); return; } } catch (AggregateException ex) { // Log the exception at this element level, then throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Staging error", ex); throw; } }
/// <summary> /// Initializes all components in this map. /// </summary> public override void Revalidate(ParallelLoopState loopState) { Thread.CurrentThread.SetName("Revalidate-{0}".FormatWith(Thread.CurrentThread.ManagedThreadId)); // If we got into a hard error state (not an aggregate error), we won't be able to recover if (MapElement.State == RuntimeState.Error) { MapElement.UpdateStatus(); return; } MapElement.UpdateStatus("Validating", RuntimeState.Validating); try { Parallel.ForEach <ResourceHost>(Hosts, (h, l) => h.Revalidate(l)); if (!SessionMapElement.AllElementsSetTo <ResourceHost>(Hosts, RuntimeState.Validated)) { MapElement.UpdateStatus(RuntimeState.AggregateError); loopState.Break(); return; } MapElement.UpdateStatus("Validated", RuntimeState.Validated); } catch (AggregateException ex) { // Log the exception at this element level, then throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Validation error", ex); throw; } }
/// <summary> /// Builds all components in this map. /// </summary> /// <param name="loopState">State of the loop.</param> public override void Stage(ParallelLoopState loopState) { var allQueues = Configuration.ManifestSet.SelectMany(n => n.ActivityPrintQueues.Values.SelectMany(m => m.OfType <RemotePrintQueueInfo>())); foreach (RemotePrintQueueInfo queue in allQueues.GroupBy(n => n.PrintQueueId).Select(n => n.First())) { RemotePrintQueueElements.Add(new RemotePrintQueueElement(queue.QueueName, queue.ServerHostName)); } try { Parallel.ForEach <RemotePrintQueueElement>(RemotePrintQueueElements, (h, l) => h.Stage(l)); if (!SessionMapElement.AllElementsSetTo <RemotePrintQueueElement>(RemotePrintQueueElements, RuntimeState.Available)) { loopState.Break(); return; } MapElement.UpdateStatus("Available", RuntimeState.Available); } catch (AggregateException ex) { MapElement.UpdateStatus(RuntimeState.Error, "Staging error", ex); throw; } }
/// <summary> /// Turns on all resource hosts in this map. /// </summary> public override void PowerUp(ParallelLoopState loopState) { try { MapElement.UpdateStatus("Starting", RuntimeState.Starting); TraceFactory.Logger.Debug("Calling PowerUp() in parallel on each Host"); Parallel.ForEach <ResourceHost>(Hosts, (h, l) => h.PowerUp(l)); if (!SessionMapElement.AllElementsSetTo <ResourceHost>(Hosts, RuntimeState.Ready)) { MapElement.UpdateStatus(RuntimeState.Error); loopState.Break(); return; } MapElement.UpdateStatus("Ready", RuntimeState.Ready); } catch (AggregateException ex) { TraceFactory.Logger.Error(ex.Message); // Log the exception at this element level, then throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Power up error", ex); throw; } }
/// <summary> /// Executes this resource host, which may mean different things. /// </summary> public void Run(ParallelLoopState loopState) { Parallel.ForEach <ResourceInstance>(Resources, (r, l) => r.Run(l)); if (!SessionMapElement.AllElementsSetTo <ResourceInstance>(Resources, RuntimeState.Running)) { TraceFactory.Logger.Debug("{0}: Not all resources reached a running state".FormatWith(Machine.Name)); loopState.Break(); } MapElement.UpdateStatus("Running", RuntimeState.Running); }
/// <summary> /// Initializes all components in this map. /// </summary> public override void Validate(ParallelLoopState loopState) { Thread.CurrentThread.SetName("Validate-{0}".FormatWith(Thread.CurrentThread.ManagedThreadId)); TraceFactory.Logger.Debug("Entering..."); MapElement.UpdateStatus("Validating", RuntimeState.Validating); try { TraceFactory.Logger.Debug("Reserving required VMs"); // Try to allocate all the needed VMs if this fails then an error // state will be sent back to the client. _resourcePool.ReserveMachines(); } catch (Exception ex) { TraceFactory.Logger.Error(ex); MapElement.UpdateStatus(ex.Message, RuntimeState.Error); loopState.Break(); return; } try { // First assign a machine instance to each host. This needs to be done before // calling validate on each host. foreach (var host in Hosts) { TraceFactory.Logger.Debug("Assigning machine {0}".FormatWith(host.Machine.Name)); _resourcePool.AssignMachine(host); } Parallel.ForEach <ResourceHost>(Hosts, (h, l) => h.Validate(l)); // Both "Validated" and "Warning" are valid states to allow the system to continue. if (!SessionMapElement.AllElementsSetTo <ResourceHost>(Hosts, RuntimeState.Validated, RuntimeState.Warning)) { MapElement.UpdateStatus(RuntimeState.AggregateError); loopState.Break(); return; } MapElement.UpdateStatus("Validated", RuntimeState.Validated); } catch (AggregateException ex) { // Log the exception at this element level, then throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Validation error", ex); throw; } }
/// <summary> /// Builds all components in this map. /// </summary> public override void Stage(ParallelLoopState loopState) { foreach (var asset in Configuration.ManifestSet.SelectMany(x => x.Assets.Devices).Distinct()) { var className = string.Empty; var attribute = ( from a in asset.GetType().GetCustomAttributes(true) where a.GetType() == typeof(AssetHostAttribute) select a as AssetHostAttribute ).FirstOrDefault(); if (attribute != null) { className = "{0}.{1}".FormatWith(GetType().Namespace, attribute.ClassName); } else { var assetName = asset.GetType().Name; var exception = new DispatcherOperationException("Invalid AssetHost class for {0}".FormatWith(assetName)); MapElement.UpdateStatus(RuntimeState.Error, "Invalid asset: ".FormatWith(assetName), exception); } TraceFactory.Logger.Debug(string.Format("Type:{0} Asset: {1}", className, asset.Description)); var assetHost = (AssetHost)Activator.CreateInstance(Type.GetType(className), new object[] { asset }); TraceFactory.Logger.Debug("Adding {0}".FormatWith(assetHost.Asset.AssetId)); Hosts.Add(assetHost); } try { Parallel.ForEach <AssetHost>(Hosts, (h, l) => h.Stage(l)); if (!SessionMapElement.AllElementsSetTo <AssetHost>(Hosts, RuntimeState.Available)) { loopState.Break(); return; } MapElement.UpdateStatus("Available", RuntimeState.Available); } catch (AggregateException ex) { // Log the exception at this element level, the throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Staging error", ex); throw; } }
/// <summary> /// Runs all components in this map. /// </summary> /// <param name="loopState">State of the loop.</param> public override void Run(ParallelLoopState loopState) { try { Parallel.ForEach <RemotePrintQueueElement>(RemotePrintQueueElements, (h, l) => h.Run(l)); if (!SessionMapElement.AllElementsSetTo <RemotePrintQueueElement>(RemotePrintQueueElements, RuntimeState.Running)) { loopState.Break(); } MapElement.UpdateStatus("Running", RuntimeState.Running); } catch (AggregateException ex) { MapElement.UpdateStatus(RuntimeState.Error, "Run error", ex); throw; } }
/// <summary> /// Runs all components in this map. /// </summary> public override void Run(ParallelLoopState loopState) { try { Parallel.ForEach <AssetHost>(Hosts, (h, l) => h.Run(l)); if (!SessionMapElement.AllElementsSetTo <AssetHost>(Hosts, RuntimeState.Running)) { loopState.Break(); } MapElement.UpdateStatus("Running", RuntimeState.Running); } catch (AggregateException ex) { // Log the exception at this element level, the throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Run error", ex); throw; } }
/// <summary> /// Shuts down all maps using the specified options /// </summary> /// <param name="options">The shutdown options (unused).</param> /// <param name="loopState">State of the loop.</param> public override void Shutdown(ShutdownOptions options, ParallelLoopState loopState) { try { MapElement.UpdateStatus("Shutdown", RuntimeState.ShuttingDown); Parallel.ForEach <RemotePrintQueueElement>(RemotePrintQueueElements, (h, l) => h.Shutdown(options, l)); if (!SessionMapElement.AllElementsSetTo <RemotePrintQueueElement>(RemotePrintQueueElements, RuntimeState.Offline)) { loopState.Break(); } MapElement.UpdateStatus("Offline", RuntimeState.Offline); } catch (AggregateException ex) { MapElement.UpdateStatus(RuntimeState.Error, "Shutdown error", ex); throw; } }
/// <summary> /// Turns on all components in this map. /// </summary> /// <param name="loopState">State of the loop.</param> public override void PowerUp(ParallelLoopState loopState) { try { MapElement.UpdateStatus("Starting", RuntimeState.Starting); Parallel.ForEach <RemotePrintQueueElement>(RemotePrintQueueElements, (h, l) => h.PowerUp(l)); if (!SessionMapElement.AllElementsSetTo <RemotePrintQueueElement>(RemotePrintQueueElements, RuntimeState.Ready)) { loopState.Break(); } MapElement.UpdateStatus("Ready", RuntimeState.Ready); } catch (AggregateException ex) { MapElement.UpdateStatus(RuntimeState.Error, "Power up error", ex); throw; } }
/// <summary> /// Turns on all components in this map. /// </summary> public override void PowerUp(ParallelLoopState loopState) { try { TraceFactory.Logger.Debug("Starting..."); MapElement.UpdateStatus("Starting", RuntimeState.Starting); Parallel.ForEach <AssetHost>(Hosts, (h, l) => h.PowerUp(l)); if (!SessionMapElement.AllElementsSetTo <AssetHost>(Hosts, RuntimeState.Ready)) { loopState.Break(); } MapElement.UpdateStatus("Ready", RuntimeState.Ready); } catch (AggregateException ex) { // Log the exception at this element level, the throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Power up error", ex); throw; } }
/// <summary> /// Shuts down all maps /// </summary> public override void Shutdown(ShutdownOptions options, ParallelLoopState loopState) { // The shutdown options are not used by the asset map so just ignore them try { MapElement.UpdateStatus("Shutdown", RuntimeState.ShuttingDown); Parallel.ForEach <AssetHost>(Hosts, (h, l) => h.Shutdown(options, l)); if (!SessionMapElement.AllElementsSetTo <AssetHost>(Hosts, RuntimeState.Offline)) { loopState.Break(); } MapElement.UpdateStatus("Offline", RuntimeState.Offline); } catch (AggregateException ex) { // Log the exception at this element level, the throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Shutdown error", ex); throw; } }