예제 #1
0
        private static HttpJobItem PrepareAddRecurringHttpJobItem(string hangfireServerUrl, RecurringJob recurringJob,
                                                                  HangfireServerPostOption option = null)
        {
            if (string.IsNullOrEmpty(hangfireServerUrl))
            {
                throw new ArgumentNullException(nameof(hangfireServerUrl));
            }

            if (recurringJob == null)
            {
                throw new ArgumentNullException(nameof(recurringJob));
            }

            if (string.IsNullOrEmpty(recurringJob.JobName))
            {
                throw new ArgumentNullException(nameof(recurringJob.JobName));
            }

            if (string.IsNullOrEmpty(recurringJob.Cron))
            {
                throw new ArgumentNullException(nameof(recurringJob.Cron));
            }

            CheckChildJob(recurringJob.Success, recurringJob.Fail);

            if (option == null)
            {
                option = new HangfireServerPostOption();
            }
            option.HttpClient = !string.IsNullOrEmpty(option.ProxyUrl) ?
                                HangfireJobHttpClientFactory.GetProxiedHttpClient(option.ProxyUrl) :
                                HangfireJobHttpClientFactory.GetHttpClient(hangfireServerUrl);
            var _data = string.Empty;

            if (recurringJob.Data != null)
            {
                if (recurringJob.Data is string _dataStr)
                {
                    _data = _dataStr;
                }
                else
                {
                    _data = JsonConvert.SerializeObject(recurringJob.Data);
                }
            }
            var url = hangfireServerUrl.EndsWith("/httpjob?op=recurringjob")
                ? hangfireServerUrl
                : hangfireServerUrl + "/httpjob?op=recurringjob";
            HttpJobItem jobItem = new HttpJobItem(url, option)
            {
                RecurringJobIdentifier = recurringJob.RecurringJobIdentifier,
                Url                  = recurringJob.Url,
                Method               = recurringJob.Method,
                Data                 = _data,
                ContentType          = recurringJob.ContentType,
                Timeout              = recurringJob.Timeout,
                JobName              = recurringJob.JobName,
                QueueName            = recurringJob.QueueName,
                Cron                 = recurringJob.Cron,
                SendSuccess          = recurringJob.SendSuccess,
                SendFail             = recurringJob.SendFail,
                Mail                 = recurringJob.Mail != null && recurringJob.Mail.Any() ? string.Join(",", recurringJob.Mail) : "",
                EnableRetry          = recurringJob.EnableRetry,
                RetryTimes           = recurringJob.RetryTimes,
                RetryDelaysInSeconds = recurringJob.RetryDelaysInSeconds,
                BasicUserName        = recurringJob.BasicUserName,
                BasicPassword        = recurringJob.BasicPassword,
                AgentClass           = recurringJob.AgentClass,
                Headers              = recurringJob.Headers,
                CallbackEL           = recurringJob.CallbackEL
            };

            AppendChildJob(jobItem, recurringJob.Success, recurringJob.Fail);
            return(jobItem);
        }
예제 #2
0
 /// <summary>
 /// AddRecurringJob
 /// </summary>
 /// <param name="hangfireServerUrl">hangfire的dashbord的根目录地址</param>
 /// <param name="recurringJob">job参数</param>
 /// <param name="option">提交httprequest的其他配置</param>
 /// <returns></returns>
 public static HangfirJobResult AddRecurringJob(string hangfireServerUrl, RecurringJob recurringJob, HangfireServerPostOption option = null)
 {
     return(PrepareAddRecurringHttpJobItem(hangfireServerUrl, recurringJob, option).Post <HangfirJobResult>());
 }