public static IGlobalConfiguration UseHangfireHttpJob(this IGlobalConfiguration config, HangfireHttpJobOptions options = null)
        {
            if (options == null)
            {
                options = new HangfireHttpJobOptions();
            }
            var assembly = typeof(HangfireHttpJobOptions).GetTypeInfo().Assembly;

            JobFilterProviders.Providers.Add(new QueueProviderFilter());

            //处理http请求
            DashboardRoutes.Routes.Add("/httpjob", new HttpJobDispatcher());
            DashboardRoutes.Routes.AddRazorPage("/cron", x => new CronJobsPage());

            var jsPath = DashboardRoutes.Routes.Contains("/js[0-9]+") ? "/js[0-9]+" : "/js[0-9]{3}";

            DashboardRoutes.Routes.Append(jsPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.jsoneditor.js"));
            DashboardRoutes.Routes.Append(jsPath, new DynamicJsDispatcher(options));
            DashboardRoutes.Routes.Append(jsPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.cron.js"));
            DashboardRoutes.Routes.Append(jsPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.httpjob.js"));
            DashboardRoutes.Routes.Append(jsPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.sweetalert2.min.js"));



            var cssPath = DashboardRoutes.Routes.Contains("/css[0-9]+") ? "/css[0-9]+" : "/css[0-9]{3}";

            DashboardRoutes.Routes.Append(cssPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.jsoneditor.css"));
            DashboardRoutes.Routes.Append(cssPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.sweetalert2.min.css"));
            DashboardRoutes.Routes.Append(cssPath, new DynamicCssDispatcher(options));

            if (options.GlobalHttpTimeOut < 2000)
            {
                options.GlobalHttpTimeOut = 2000;
            }
            if (options.CheckHttpResponseStatusCode == null)
            {
                options.CheckHttpResponseStatusCode = (code, result) => ((int)code) < 400;
            }

            if (string.IsNullOrEmpty(options.GlobalSettingJsonFilePath))
            {
                options.GlobalSettingJsonFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hangfire_global.json");
            }

            if (options.GlobalHttpClientTimeOut > 0)
            {
                HangfireHttpClientFactory.SetDefaultHttpJobInstance(new HangfireHttpClientFactory(TimeSpan.FromMilliseconds(options.GlobalHttpClientTimeOut), null));
            }
            else
            {
                HangfireHttpClientFactory.SetDefaultHttpJobInstance(options.HttpJobClientFactory);
            }
            HangfireHttpClientFactory.SetDefaultDingTalkInstance(options.DingTalkClientFactory);

            CodingUtil.HangfireHttpJobOptions = options;
            JobAgentReportServer.Start();
            JobAgentHeartBeatServer.Start();
            LosedJobCheckServer.Start();
            return(config);
        }
Пример #2
0
        public static IGlobalConfiguration UseHangfireHttpJob(this IGlobalConfiguration config, HangfireHttpJobOptions options = null)
        {
            if (options == null)
            {
                options = new HangfireHttpJobOptions();
            }
            var assembly = typeof(HangfireHttpJobOptions).GetTypeInfo().Assembly;

            JobFilterProviders.Providers.Add(new QueueProviderFilter());

            //处理http请求
            DashboardRoutes.Routes.Add("/httpjob", new HttpJobDispatcher());
            DashboardRoutes.Routes.AddRazorPage("/cron", x => new CronJobsPage());

            var jsPath = DashboardRoutes.Routes.Contains("/js[0-9]+") ? "/js[0-9]+" : "/js[0-9]{3}";

            DashboardRoutes.Routes.Append(jsPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.jsoneditor.js"));
            DashboardRoutes.Routes.Append(jsPath, new DynamicJsDispatcher(options));
            DashboardRoutes.Routes.Append(jsPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.cron.js"));
            DashboardRoutes.Routes.Append(jsPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.httpjob.js"));
            DashboardRoutes.Routes.Append(jsPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.sweetalert2.min.js"));



            var cssPath = DashboardRoutes.Routes.Contains("/css[0-9]+") ? "/css[0-9]+" : "/css[0-9]{3}";

            DashboardRoutes.Routes.Append(cssPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.jsoneditor.css"));
            DashboardRoutes.Routes.Append(cssPath, new EmbeddedResourceDispatcher(assembly, "Hangfire.HttpJob.Content.sweetalert2.min.css"));
            DashboardRoutes.Routes.Append(cssPath, new DynamicCssDispatcher(options));

            if (options.GlobalHttpTimeOut < 2000)
            {
                options.GlobalHttpTimeOut = 2000;
            }
            if (options.CheckHttpResponseStatusCode == null)
            {
                options.CheckHttpResponseStatusCode = (code, result) => ((int)code) < 400;
            }

            if (string.IsNullOrEmpty(options.GlobalSettingJsonFilePath))
            {
                options.GlobalSettingJsonFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hangfire_global.json");
            }

            if (!File.Exists(options.GlobalSettingJsonFilePath))
            {
                try
                {
                    var fileAll = File.ReadAllText(options.GlobalSettingJsonFilePath);
                    File.WriteAllText(options.GlobalSettingJsonFilePath, fileAll);//如果没有权限则会报错
                }
                catch (Exception e)
                {
                    Logger.WarnException($"{nameof(HangfireHttpJobOptions.GlobalSettingJsonFilePath)}:[{options.GlobalSettingJsonFilePath}] access error", e);
                }
            }

            CodingUtil.HangfireHttpJobOptions = options;
            JobAgentReportServer.Start();
            JobAgentHeartBeatServer.Start();
            LosedJobCheckServer.Start();
            return(config);
        }