private static Task Consumer( ILoggerFactory loggerFactory, JobStorage storage, CancellationToken token) { var server = new BackgroundJobServer( new BackgroundJobServerOptions { WorkerCount = 16 }, storage); return(Task.Run( () => { token.WaitHandle.WaitOne(); server.SendStop(); server.WaitForShutdown(TimeSpan.FromSeconds(30)); }, token)); }
public virtual void Configuration(IAppBuilder appBuilder) { appBuilder.Use <OwinExceptionHandlerMiddleware>(Log.Logger); // Configure Web API for self-host. HttpConfiguration config = new HttpConfiguration(); config.Services.Replace(typeof(IExceptionHandler), new OwinExceptionHandlerMiddleware.PassthroughExceptionHandler()); config.MapHttpAttributeRoutes(); config.EnableSwagger(c => { c.SingleApiVersion("v1", "RPA web API"); c.DescribeAllEnumsAsStrings(); c.IncludeXmlComments("RpaSelfHostedApp.xml"); c.OperationFilter <SwaggerParameterOperationFilter>(); }) .EnableSwaggerUi(); appBuilder.UseWebApi(config); appBuilder.MapSignalR(); var container = appBuilder.RegisterDependencies(config); Hangfire.GlobalConfiguration.Configuration .UseLiteDbStorage() .UseLightInjectActivator(container) .UseSerilogLogProvider(); var physicalFileSystem = new PhysicalFileSystem(@".\Web"); //. = root, Web = your physical directory that contains all other static content, see prev step var options = new FileServerOptions { EnableDefaultFiles = true, FileSystem = physicalFileSystem, StaticFileOptions = { FileSystem = physicalFileSystem, ServeUnknownFileTypes = false }, DefaultFilesOptions = { DefaultFileNames = new[] { "index.html" } }, }; //put whatever default pages you like here appBuilder.UseFileServer(options); var properties = new AppProperties(appBuilder.Properties); CancellationToken token = properties.OnAppDisposing; var client = new BackgroundJobServer(new BackgroundJobServerOptions() { SchedulePollingInterval = TimeSpan.FromSeconds(1), ServerCheckInterval = TimeSpan.FromSeconds(1) }, JobStorage.Current); Action disposeObjects = () => { if (!_wasDisposed) { _wasDisposed = true; var fullNameOfDll = Assembly.GetExecutingAssembly().FullName; Log.Logger.Warning($"Close {this.GetType().FullName} application \n {fullNameOfDll}"); client.Dispose(); container.Dispose(); } }; if (token != CancellationToken.None) { client.WaitForShutdown(TimeSpan.FromSeconds(10)); token.Register(disposeObjects); } else { Log.Fatal("Error with application start"); } AppDomain.CurrentDomain.ProcessExit += (sender, args) => { disposeObjects(); }; }