Example #1
0
    public async Task Invoke(HttpContext context)
    {
        var pathRegex       = new Regex($@"\/([0-7]+)(.*)($|\/)");
        var matches         = pathRegex.Match(context.Request.Path.Value);
        var configurationId = int.Parse(matches.Groups[1].Value);
        var resource        = matches.Groups[2].Value;

        var configuration = _configuration
                            .QueryAllWorkerServers()
                            .Single(x => x.ConfigurationId == configurationId);

        var originalPath     = context.Request.Path;
        var originalPathBase = context.Request.PathBase;

        context.Request.PathBase += "/" + configurationId;
        context.Request.Path      = resource;

        try
        {
            await new AspNetCoreDashboardMiddleware(
                _next,
                configuration.JobStorage,
                _dashboardOptions,
                DashboardRoutes.Routes
                ).Invoke(context);
        }
        finally
        {
            context.Request.PathBase = originalPathBase;
            context.Request.Path     = originalPath;
        }
    }
        public void Configure(IApplicationBuilder app)

        {
            GlobalConfiguration.Configuration
            .UseColouredConsoleLogProvider()
            .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings();

            app.UseDeveloperExceptionPage();

            var configurationConnectionString   = @"Username=postgres;Password=root;Host=localhost;Database=""hangfire.sample"";";
            var defaultHangfireConnectionString = @"Username=postgres;Password=root;Host=localhost;Database=""hangfire.sample"";";
            var defaultHangfireSchema           = "hangfirecustomschemaname";

            app.Use((context, next) =>
            {
                // simulate a hosting site with content security policy
                context.Response.Headers.Append("Content-Security-Policy",
                                                "script-src 'self'; frame-ancestors 'self';");

                // simulate a hosting site with a static file handler
                if (context.Request.Path.Value.Split('/').Last().Contains("."))
                {
                    context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                    return(Task.CompletedTask);
                }

                return(next.Invoke());
            });

            var storageOptions = new PostgreSqlStorageOptions()
            {
                QueuePollInterval        = TimeSpan.FromSeconds(2),
                PrepareSchemaIfNecessary = true,
                SchemaName = "NotUsedSchemaName"
            };


            var options = new ConfigurationOptions
            {
                ConnectionString         = configurationConnectionString,
                PrepareSchemaIfNecessary = true,
                UpdateConfigurations     = new[]
                {
                    new UpdateStorageConfiguration
                    {
                        ConnectionString = defaultHangfireConnectionString,
                        Name             = DefaultConfigurationName.Name(),
                        SchemaName       = defaultHangfireSchema
                    }
                }
            };

            Console.WriteLine();
            Console.WriteLine(Program.NodeAddress + "/HangfireConfiguration");
            app.UseHangfireConfigurationUI("/HangfireConfiguration", options);

            HangfireConfiguration = app
                                    .UseHangfireConfiguration(options)
                                    .UseStorageOptions(storageOptions)
            ;

            HangfireConfiguration
            .UseStorageOptions(storageOptions)                     //Needed???? already set above
            .UseServerOptions(new BackgroundJobServerOptions
            {
                Queues = new[] { "critical", "default" },
            })
            .StartPublishers()
            .StartWorkerServers(new[] { new CustomBackgroundProcess() });

            HangfireConfiguration
            .QueryAllWorkerServers()
            .ForEach(x => { Console.WriteLine(Program.NodeAddress + $"/HangfireDashboard/{x.ConfigurationId}"); });

            app.UseDynamicHangfireDashboards("/HangfireDashboard", options, new DashboardOptions());
        }