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
 /// <summary>
 /// 获取agent服务器
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 private async Task GetAgentServer(DashboardContext context)
 {
     try
     {
         var html = JobAgentHeartBeatServer.GetAgentServerListHtml(context.Request.PathBase);
         await context.Response.WriteAsync(html);
     }
     catch (Exception e)
     {
         await context.Response.WriteAsync("err:" + e.Message);
     }
 }
示例#3
0
        /// <summary>
        /// 新增周期job 只是新增 如果已存在了就不新增了
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private async Task AddRecurringJob(DashboardContext context)
        {
            var jobItemRt = await GetCheckedJobItem(context);

            if (!string.IsNullOrEmpty(jobItemRt.Item2))
            {
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                await context.Response.WriteAsync(jobItemRt.Item2);

                return;
            }
            if (CodingUtil.HangfireHttpJobOptions.AddHttpJobFilter != null)
            {
                if (!CodingUtil.HangfireHttpJobOptions.AddHttpJobFilter(jobItemRt.Item1))
                {
                    context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    await context.Response.WriteAsync("HttpJobFilter return false");

                    return;
                }
            }

            if (string.IsNullOrEmpty(jobItemRt.Item1.QueueName))
            {
                jobItemRt.Item1.QueueName = CodingUtil.HangfireHttpJobOptions.DefaultRecurringQueueName;
            }
            var result = AddHttprecurringjob(jobItemRt.Item1, true);

            if (string.IsNullOrEmpty(result))
            {
                JobAgentHeartBeatServer.Start(false);
                context.Response.ContentType = "application/json";
                context.Response.StatusCode  = (int)HttpStatusCode.NoContent;
                return;
            }
            else
            {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                await context.Response.WriteAsync(result);

                return;
            }
        }
示例#4
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);
        }