private void PerformSystemSetupActivities() { ChangeMachineStatusMessage("Plugin Setup"); // Once the all child workers have reported in, notify all the workers that they can continue // the registration process. foreach (var workerEndpoint in _userEndpoints) { TraceFactory.Logger.Debug("{0} - ReadyToRegister START".FormatWith(workerEndpoint)); using (var client = VirtualResourceManagementConnection.Create(workerEndpoint)) { try { client.Channel.ReadyToRegister(); TraceFactory.Logger.Debug("{0} - ReadyToRegister END".FormatWith(workerEndpoint)); } catch (Exception ex) { TraceFactory.Logger.Debug("{0} - ReadyToRegister did not return: {1}".FormatWith(workerEndpoint, ex)); } } } TraceFactory.Logger.Debug("All workers sent notification to resume registration"); }
/// <summary> /// Executes this asset host, which may mean different things. /// </summary> public virtual void Run(ParallelLoopState loopState) { if (Endpoint == null) { MapElement.UpdateStatus("The endpoint for '{0}' is null".FormatWith(Id), RuntimeState.Error); return; } Action action = () => { using (var client = VirtualResourceManagementConnection.Create(Endpoint)) { TraceFactory.Logger.Debug("{0}: starting activities".FormatWith(Id)); client.Channel.StartMainRun(); } }; try { Retry.WhileThrowing(action, 2, TimeSpan.FromSeconds(1), new List <Type>() { typeof(Exception) }); MapElement.UpdateStatus("Running", RuntimeState.Running); } catch (EndpointNotFoundException ex) { MapElement.UpdateStatus("ComError", RuntimeState.Error); TraceFactory.Logger.Error(ex); } }
/// <summary> /// Opens a service endpoint for the PerfMonCollector that other clients can talk to /// </summary> private void LoadServiceEndpoint() { if (_serviceEndpoint == null) { _commandService = VirtualResourceManagementConnection.CreateServiceHost(Environment.MachineName, (int)WcfService.VirtualResource); _serviceEndpoint = _commandService.BaseAddresses[0]; _commandService.Open(); } }
/// <summary> /// Pings this instance for a responce. /// </summary> public void CheckHealth() { try { using (var client = VirtualResourceManagementConnection.Create(Endpoint)) { client.Channel.Ping(); } } catch (Exception ex) { MapElement.UpdateStatus("Not Responding", RuntimeState.Error); TraceFactory.Logger.Error(ex); } }
/// <summary> /// Handles the Elapsed event of the _timer control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param> /// <remarks> /// This method performs a cleanup on stale entries. It does it by trying to /// communicate with the endpoint for the defined Citrix user, and if it doesn't /// get a response or if the session Id is different, then this item is considered /// stale and can be removed. /// </remarks> private void _cleanupTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { _cleanupTimer.Stop(); Dictionary <string, List <PrinterRegistryEntry> > registryEntriesCopy = null; lock (_registryEntriesLock) { registryEntriesCopy = (from r in _registryEntries select r).ToDictionary(r => r.Key, r => r.Value); } foreach (string key in registryEntriesCopy.Keys) { foreach (PrinterRegistryEntry entry in registryEntriesCopy[key]) { // Create a time value that is the actual start time (when this service received a message) // plus the delay time for the client to actually start, plus a long wait window. If all // of that time has passed and there is still an entry, the remove it. DateTime actualStartupTime = entry.ClientData.SessionStart.Add(entry.ClientData.StartupDelay); actualStartupTime = actualStartupTime.Add(_cleanupTimeDelta); DateTime now = DateTime.Now; if (DateTime.Compare(actualStartupTime, now) < 0) { using (var service = VirtualResourceManagementConnection.Create(entry.ClientData.EndPoint)) { try { service.Channel.Ping(); } catch (FaultException) { PurgeEntry(key, entry); } catch (EndpointNotFoundException) { PurgeEntry(key, entry); } catch (SocketException) { PurgeEntry(key, entry); } } } } } _cleanupTimer.Start(); }
/// <summary> /// Resumes operations to an asset which may or may not be used by this instance. /// </summary> /// <param name="assetId"></param> public void BringOnline(string assetId) { try { using (var client = VirtualResourceManagementConnection.Create(Endpoint)) { TraceFactory.Logger.Debug("Bringing Asset {0} Online".FormatWith(assetId)); client.Channel.BringOnline(assetId); } } catch (Exception ex) { MapElement.UpdateStatus("Error", RuntimeState.Error); TraceFactory.Logger.Error(ex); } }
/// <summary> /// Sends a synchronization signal with the specified event name. /// </summary> /// <param name="eventName">The synchronization event name.</param> public void SignalSynchronizationEvent(string eventName) { try { using (var client = VirtualResourceManagementConnection.Create(Endpoint)) { TraceFactory.Logger.Debug($"{Id}: sending sync event '{eventName}"); client.Channel.SignalSynchronizationEvent(eventName); } } catch (Exception ex) { MapElement.UpdateStatus("Error", RuntimeState.Error); TraceFactory.Logger.Error(ex); } }
/// <summary> /// Shuts down this instance. /// </summary> public void Shutdown(ShutdownOptions options) { if (Endpoint == null) { TraceFactory.Logger.Info("{0} never registered, so marking as already shut down.".FormatWith(Id)); ChangeState(RuntimeState.Offline); return; } MapElement.UpdateStatus("Shutdown", RuntimeState.ShuttingDown); try { TraceFactory.Logger.Debug("{0} - Shutdown Signaled".FormatWith(Endpoint)); ChangeState(RuntimeState.ShuttingDown); var action = new Action(() => { using (var client = VirtualResourceManagementConnection.Create(Endpoint)) { client.Channel.Shutdown(options.CopyLogs); } }); var listofExceptions = new List <Type>() { typeof(EndpointNotFoundException), typeof(TimeoutException) }; int retries = Retry.WhileThrowing(action, 2, TimeSpan.FromSeconds(20), listofExceptions); TraceFactory.Logger.Debug("{0} -> Shutdown Complete: RETRIES: {1}".FormatWith(Endpoint, retries)); } catch (Exception ex) { TraceFactory.Logger.Error("{0} -> Shutdown Failed, continuing: {1}".FormatWith(Endpoint, ex.ToString())); //ChangeState(RuntimeState.Offline); //MapElement.UpdateStatus("Offline", RuntimeState.Offline); } finally { //Only used to speed up shutdown in ResourceHost ChangeState(RuntimeState.Offline); MapElement.UpdateStatus("Offline", RuntimeState.Offline); } }
/// <summary> /// Halts this instance. /// </summary> public void Halt() { MapElement.UpdateStatus("Halting", RuntimeState.Halting); try { using (var client = VirtualResourceManagementConnection.Create(Endpoint)) { TraceFactory.Logger.Debug("{0}: halting activities".FormatWith(Id)); client.Channel.HaltResource(); } } catch (Exception ex) { MapElement.UpdateStatus("ComError", RuntimeState.Error); TraceFactory.Logger.Error(ex); } }
/// <summary> /// Pings the worker. /// </summary> /// <param name="credential">The credential.</param> /// <returns></returns> private bool PingWorker(OfficeWorkerCredential credential) { bool result = false; var endpoint = new Uri("http://{0}:{1}/{2}".FormatWith(_citrixServer, credential.Port, WcfService.VirtualResource)); TraceFactory.Logger.Debug("Pinging {0}".FormatWith(endpoint)); using (var service = VirtualResourceManagementConnection.Create(_citrixServer, credential.Port)) { if (service.Channel.Ping()) { TraceFactory.Logger.Debug("Ping succeeded"); result = true; } } return(result); }
/// <summary> /// Performs the defined action. /// </summary> /// <param name="action">The action.</param> protected void PerformAction(Action <IVirtualResourceManagementService> action) { if (Endpoint == null) { MapElement.UpdateStatus("The endpoint for '{0}' is null".FormatWith(Id), RuntimeState.Error); return; } Action retryAction = () => { using (var client = VirtualResourceManagementConnection.Create(Endpoint)) { TraceFactory.Logger.Debug("{0}: starting activities".FormatWith(Id)); action(client.Channel); } }; try { _waitEvent = new AutoResetEvent(false); var listofExceptions = new List <Type>() { typeof(EndpointNotFoundException), typeof(TimeoutException) }; int retries = Retry.WhileThrowing(retryAction, 2, TimeSpan.FromSeconds(1), listofExceptions); TraceFactory.Logger.Debug("Message sent, waiting for completion: RETRIES: {0}".FormatWith(retries)); _waitEvent.WaitOne(); } catch (EndpointNotFoundException ex) { MapElement.UpdateStatus("Error", RuntimeState.Error); TraceFactory.Logger.Error(ex); } finally { if (_waitEvent != null) { _waitEvent.Dispose(); _waitEvent = null; } } }
/// <summary> /// Pauses this instance. /// </summary> public void Pause() { // Earlier it was determined that this instance was running, so it could be paused // if that has changed, the ResourceHost will still be waiting for this instance // to flag that it is paused. So go ahead and send a state changed notice // that will take care of it. But don't actually change the state of this instance. if (MapElement.State != RuntimeState.Running) { SendStateChangeEvent(RuntimeState.Paused); return; } MapElement.UpdateStatus("Pausing", RuntimeState.Pausing); Action action = () => { using (var client = VirtualResourceManagementConnection.Create(Endpoint)) { TraceFactory.Logger.Debug("{0}: pausing activities".FormatWith(Id)); client.Channel.PauseResource(); } }; try { int retries = Retry.WhileThrowing(action, 2, TimeSpan.FromSeconds(1), new List <Type>() { typeof(Exception) }); TraceFactory.Logger.Debug("RETRIES: {0}".FormatWith(retries)); } catch (EndpointNotFoundException ex) { MapElement.UpdateStatus("Error", RuntimeState.Error); TraceFactory.Logger.Error(ex); } }
/// <summary> /// Override so that child controllers can establish their own service host /// </summary> /// <param name="port"></param> /// <returns></returns> protected virtual ServiceHost CreateServiceHost(int port) { TraceFactory.Logger.Debug("Creating endpoint"); return(VirtualResourceManagementConnection.CreateServiceHost(Environment.MachineName, port)); }
protected override ServiceHost CreateServiceHost(int port) { TraceFactory.Logger.Debug("Creating endpoint"); return(VirtualResourceManagementConnection.CreateServiceHost("localhost", Guid.NewGuid().ToShortString())); }
/// <summary> /// Registers the resource using the currently defined management endpoint. /// </summary> /// <param name="resourceName">Name of the resource.</param> /// <exception cref="System.InvalidOperationException">Unable to start service.</exception> protected void OpenManagementServiceEndpoint(string resourceName) { _managementService = VirtualResourceManagementConnection.CreateServiceHost(Environment.MachineName, (int)WcfService.VirtualResource); _managementService.Open(); ServiceEndpoint = _managementService.BaseAddresses[0]; }