/// <summary> /// The do work. /// </summary> /// <exception cref="OperationCanceledException"> /// If canceled. /// </exception> private void DoWork() { string workerActionTypeName = this.workerActionConfiguration.ActionType.Name; int timeOutDelay = this.DelayStart; this.DelayOnStartIfNeeded(workerActionTypeName, timeOutDelay); if (this.Token.IsCancellationRequested) { return; } IWorkerActionFactory childFactory = null; try { childFactory = this.workerActionFactory.CreateChildFactory(); IWorkerAction workerAction = childFactory.Create(this.workerActionConfiguration.ActionType); bool actionHasAnotherWork = workerAction.Run(this.Token, this.workerActionConfiguration.Parameters); if (actionHasAnotherWork) { timeOutDelay = this.ShortTimeout; } else { timeOutDelay = (int)this.workerActionConfiguration.LongTimeout.TotalMilliseconds; } } catch (OperationCanceledException exception) { // check that cancellation issued by correct token if (exception.CancellationToken == this.Token) { InfraEventSource.Log.ActionCancel("OperationCanceledException", workerActionTypeName); throw; } InfraEventSource.Log.ActionFailed(workerActionTypeName, exception.Message, exception.ToString()); timeOutDelay = this.FailTimeout; } catch (Exception exception) { InfraEventSource.Log.ActionFailed(workerActionTypeName, exception.Message, exception.ToString()); timeOutDelay = this.FailTimeout; } finally { this.TryToDisposeActionFactory(childFactory, workerActionTypeName); } this.DelayAfterExecutionIfNeeded(timeOutDelay, workerActionTypeName); }
/// <summary> /// The try to dispose action factory. /// </summary> /// <param name="factory"> /// The factory. /// </param> /// <param name="actionName"> /// The action name. /// </param> private void TryToDisposeActionFactory(IWorkerActionFactory factory, string actionName) { IDisposable disposable = factory as IDisposable; if (disposable != null) { try { disposable.Dispose(); } catch (Exception exception) { InfraEventSource.Log.ActionFailed(actionName, "Exception occurred in the action factory dispose " + exception.Message, exception.ToString()); } } }
/// <summary> /// Initializes a new instance of the <see cref="ActionWorker" /> class. /// </summary> /// <param name="workerActionConfiguration">The worker configuration.</param> /// <param name="workerActionFactory">The worker action factory.</param> /// <exception cref="System.ArgumentNullException">Thrown if any argument is null. /// </exception> public ActionWorker( IWorkerActionConfiguration workerActionConfiguration, IWorkerActionFactory workerActionFactory) { if (workerActionConfiguration == null) { throw new ArgumentNullException(nameof(workerActionConfiguration)); } if (workerActionFactory == null) { throw new ArgumentNullException(nameof(workerActionFactory)); } this.workerActionConfiguration = workerActionConfiguration; this.workerActionFactory = workerActionFactory; }
/// <summary> /// The run workers host service. /// </summary> /// <param name="optionsBootstrap"> /// The options bootstrap. /// </param> /// <param name="actionFactoryBootstrap"> /// The action factory bootstrap. /// </param> public void RunWorkersHostService(Func <string[], WinServiceOptions> optionsBootstrap, Func <string[], IWorkerActionFactory> actionFactoryBootstrap) { this.RunWorkersHostService( optionsBootstrap, delegate(string[] strings) { IWorkerActionFactory actionFactory = actionFactoryBootstrap(strings); const string SectionName = "workersHost"; WorkerConfigurationSection configurationSection = ConfigurationManager.GetSection(SectionName) as WorkerConfigurationSection; if (configurationSection == null) { throw new InvalidOperationException($"Unable to find {SectionName} configuration section"); } return(new ActionWorkersProvider( configurationSection, actionFactory)); }); }
/// <summary> /// Initializes a new instance of the <see cref="ActionWorkersProvider" /> class. /// </summary> /// <param name="workerConfigurationSection">The worker configuration section.</param> /// <param name="workerActionFactory">The worker Action Factory.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown if any argument is null. /// </exception> public ActionWorkersProvider(WorkerConfigurationSection workerConfigurationSection, IWorkerActionFactory workerActionFactory) { if (workerConfigurationSection == null) { throw new ArgumentNullException(nameof(workerConfigurationSection)); } if (workerActionFactory == null) { throw new ArgumentNullException(nameof(workerActionFactory)); } this.workerActionFactory = workerActionFactory; IList <IWorkerActionConfiguration> workersList = new List <IWorkerActionConfiguration>(); foreach (WorkerConfig workerConfiguration in workerConfigurationSection.WorkerActions) { workersList.Add(new WorkerActionConfiguration(workerConfiguration)); } this.workers = workersList; }