public BackgroundProcessingServer(DataStorage storage, BackgroundProcessingServerOptions options, IDictionary <string, object> properties, IEnumerable <IBackgroundProcess> processes) { if (storage == null) { throw new ArgumentNullException(nameof(storage)); } if (processes == null) { throw new ArgumentNullException(nameof(processes)); } if (properties == null) { throw new ArgumentNullException(nameof(properties)); } _options = options ?? throw new ArgumentNullException(nameof(options)); _processes.AddRange(processes); _processes.AddRange(GetRequiredProcesses()); var context = new BackgroundProcessContext( serverId: _options.ServerName, cancellationToken: _cts.Token, storage: storage, properties: properties, serverContext: options.ServerContext ); _bootstrapTask = WrapProcess(this).CreateTask(context); }
private static void RunProcess(IBackgroundProcess process, BackgroundProcessContext context) { // Long-running tasks are based on custom threads (not threadpool ones) as in // .NET Framework 4.5, so we can try to set custom thread name to simplify the // debugging experience. TrySetThreadName(process.ToString()); // LogProvider.GetLogger does not throw any exception, that is why we are not // using the `try` statement here. It does not return `null` value as well. var logger = LogProvider.GetLogger(); logger.Log(LogLevel.Debug, $"Background process '{process}' started."); try { process.Execute(context); } catch (Exception ex) { if (ex is OperationCanceledException && context.IsShutdownRequested) { // Graceful shutdown logger.Log(LogLevel.Debug, $"Background process '{process}' was stopped due to a shutdown request."); } else { logger.Log(LogLevel.Fatal, $"Fatal error occurred during execution of '{process}' process. It will be stopped. See the exception for details.", ex); } } logger.Log(LogLevel.Debug, $"Background process '{process}' stopped."); }
public void Execute(BackgroundProcessContext context) { try { var tasks = _processes .Select(WrapProcess) .Select(process => process.CreateTask(context)) .ToArray(); Task.WaitAll(tasks); } finally { context.Storage.RemoveServer(context.ServerId); } }
public static Task CreateTask(this IBackgroundProcess process, BackgroundProcessContext context) { if (process == null) { throw new ArgumentNullException(nameof(process)); } if (!(process is IBackgroundProcess)) { throw new ArgumentOutOfRangeException(nameof(process), "Long-running process must be of type IServerComponent or IBackgroundProcess."); } return(Task.Factory.StartNew( () => RunProcess(process, context), TaskCreationOptions.LongRunning)); }
public static void Execute(this IBackgroundProcess process, BackgroundProcessContext context) { process.Execute(context); }