/// <summary>
        /// Processes the error by logging it and then running the hosted Error method and waiting for the result.
        /// </summary>
        /// <param name="hostedProcess">The hosted service.</param>
        /// <param name="e">The exception to handle.</param>
        /// <param name="forceStop">if set to <c>true</c> [force stop].</param>
        internal void ProcessError(IHostedProcess hostedProcess, Exception e, bool forceStop = true)
        {
            Status = HostStatus.Faulted;

            // Lock statement as this method can be accessed from both threads.
            lock (_lockGate)
            {
                _logger?.LogError($"Application host has caught an error {hostedProcess?.GetType().Name}: {e?.Message}");

                var args = new ErrorArgs {
                    ContinueClose = forceStop
                };

                try
                {
                    hostedProcess?.Error(e, args);
                    _logger?.LogDebug($"Ran {hostedProcess?.GetType().Name}'s Error method successfully");
                }
                catch (Exception ex)
                {
                    _logger?.LogError(ex, $"Error caught in process error handling for {hostedProcess?.GetType().Name}: {ex.Message}");
                }

                // Don't stop the app if the hosted process decides it does not want that and changes the Continue close arg.
                if (args.ContinueClose)
                {
                    // As an error occurred, stop processing now and shut down.
                    ProcessingStop();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Adds the hosted process into the service collection.
        /// </summary>
        /// <param name="process">The service to add.</param>
        /// <returns>AppHostBuilder with the new process added.</returns>
        public AppHostBuilder AddHostedProcess(IHostedProcess process)
        {
            var type = process.GetType();

            _services.AddSingleton(type, process);
            _processTypes.Add(type);
            return(this);
        }
        private Task StartHostedProcess(bool runOnce, [NotNull] IHostedProcess process, string name)
        {
            // Run the hosted process, wrapped in polly retry logic.
            return(BuildWrappedPolicy().ExecuteAsync(token => process.Start(_context, token), _cancellation.Token)
                   .ContinueWith(p =>
            {
                if (p.IsFaulted)
                {
                    _logger?.LogError(p.Exception, $"Error running hosted process {name} execution {p.Exception?.Message}");
                    ProcessError(process, p.Exception, !runOnce);
                }

                if (p.Status == TaskStatus.Canceled)
                {
                    _logger?.LogWarning("Hosted process cancelled");
                }
            }));
        }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProcessEventArgs"/> class.
 /// </summary>
 /// <param name="process">The process.</param>
 public ProcessEventArgs(IHostedProcess process)
 {
     this._Process = process;
 }
 public void HandleHostedProcessFailure(IHostedProcess sender, string reason)
 {
     HandleProcessFailure(reason);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ProcessTerminatedEventArgs"/> class.
 /// </summary>
 /// <param name="process">The process.</param>
 public ProcessTerminatedEventArgs(IHostedProcess process)
     : base(process)
 {
 }
Beispiel #7
0
 public void HandleHostedProcessFailure(IHostedProcess sender, string reason)
 {
     HandleProcessFailure(reason);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ProcessStartedEventArgs"/> class.
 /// </summary>
 /// <param name="process">The process.</param>
 public ProcessStartedEventArgs(IHostedProcess process)
     : base(process)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ProcessEventArgs"/> class.
 /// </summary>
 /// <param name="process">The process.</param>
 public ProcessEventArgs(IHostedProcess process)
 {
     this._Process = process;
 }
 /// <summary>
 /// Terminates the process.
 /// </summary>
 /// <param name="process">The process.</param>
 private void TerminateProcess(IHostedProcess process)
 {
     if (!process.HasExited)
     {
         this.OnProcessTerminating.RaiseEvent(this, new ProcessTerminatingEventArgs(process));
         process.Kill();
         this.OnProcessTerminated.RaiseEvent(this, new ProcessTerminatedEventArgs(process));
     }
 }
        /// <summary>
        /// Checks the remove completed process.
        /// </summary>
        /// <param name="generationDao">The generation DAO.</param>
        /// <param name="process">The process.</param>
        private void CheckRemoveCompletedProcess(GenerationDao generationDao, IHostedProcess process)
        {
            ReportGenerationQueue processingReport = generationDao.Find<ReportGenerationQueue>(queue =>
                queue.ReportGenerationQueueId == process.ReportGenerationQueueId);

            if (processingReport != null)
            {
                // If the process is completed, cancelled or failed, remove it from the queue
                if (processingReport.ReportGenerationStatus.In(ReportStatus.Completed, ReportStatus.Cancelled, ReportStatus.Failed))
                {
                    if (processingReport.ReportGenerationStatus == ReportStatus.Cancelled)
                    {
                        this.TerminateProcess(process);
                    }

                    this._reportGeneratorEngineList.Remove(process);
                }
                else
                {
                    if (process.HasExited)
                    {
                        this._reportGeneratorEngineList.Remove(process);
                    }
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// Adds the IHostedProcess to the service collection.
        /// </summary>
        /// <param name="serviceBuilder">A collection of services</param>
        /// <param name="process">The IHostedProcess to add to the service collection.</param>
        /// <returns>List of services with the IHostedProcess attached.</returns>
        public static IServiceCollection AddHostedProcess(this IServiceCollection serviceBuilder, IHostedProcess process)
        {
            var type = process.GetType();

            ProcessTypes.Add(type);
            serviceBuilder.AddSingleton(type, process);
            return(serviceBuilder);
        }