Example #1
0
        /// <summary>
        /// 添加后台作业
        /// </summary>
        /// <param name="jobItem"></param>
        /// <returns></returns>
        public string AddHttpbackgroundjob(HttpJobItem jobItem)
        {
            try
            {
                var queueName = !string.IsNullOrEmpty(jobItem.AgentClass) ? "JobAgent" : jobItem.QueueName;
                if (string.IsNullOrEmpty(queueName))
                {
                    queueName = EnqueuedState.DefaultQueue;
                }

                if (!string.IsNullOrEmpty(jobItem.RunAt))
                {
                    //如果设置了 指定的运行时间 先parse一下
                    if (DateTimeOffset.TryParse(jobItem.RunAt, out var runAtTime))
                    {
                        return(BackgroundJob.Schedule(() => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null), runAtTime));
                    }
                }

                if (jobItem.DelayFromMinutes <= 0)
                {
                    return(BackgroundJob.Enqueue(() => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null)));
                }

                return(BackgroundJob.Schedule(() => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null),
                                              TimeSpan.FromMinutes(jobItem.DelayFromMinutes)));
            }
            catch (Exception ex)
            {
                Logger.ErrorException("HttpJobDispatcher.AddHttpbackgroundjob", ex);
                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// 添加周期性作业
        /// </summary>
        /// <param name="jobItem"></param>
        /// <param name="timeZone">job 时区信息</param>
        /// <returns></returns>
        public bool AddHttprecurringjob(HttpJobItem jobItem)
        {
            if (string.IsNullOrEmpty(jobItem.QueueName))
            {
                jobItem.QueueName = EnqueuedState.DefaultQueue;
            }
            else
            {
                //get queues from server
                // ReSharper disable once ReplaceWithSingleCallToFirstOrDefault
                var server = JobStorage.Current.GetMonitoringApi().Servers().Where(p => p.Queues.Count > 0).FirstOrDefault();
                // ReSharper disable once PossibleNullReferenceException
                var queues = server.Queues.ToList();
                if (!queues.Exists(p => p == jobItem.QueueName.ToLower()) || queues.Count == 0)
                {
                    Logger.Warn($"HttpJobDispatcher.AddHttprecurringjob Error => HttpJobItem.QueueName:`{jobItem.QueueName}` not exist, Use DEFAULT extend!");
                }
            }

            var queueName = !string.IsNullOrEmpty(jobItem.AgentClass) ? "JobAgent" : jobItem.QueueName;

            if (string.IsNullOrEmpty(queueName))
            {
                queueName = EnqueuedState.DefaultQueue;
            }


            try
            {
                // 先用每个job配置的 如果没有就用系统配置的 在没有就用Local
                TimeZoneInfo timeZone = null;
                if (!string.IsNullOrEmpty(jobItem.TimeZone))
                {
                    timeZone = TimeZoneInfoHelper.OlsonTimeZoneToTimeZoneInfo(jobItem.TimeZone);
                }

                if (timeZone == null)
                {
                    timeZone = CodingUtil.HangfireHttpJobOptions.RecurringJobTimeZone ?? TimeZoneInfo.Local;
                }
                if (string.IsNullOrEmpty(jobItem.Cron))
                {
                    //支持添加一个 只能手动出发的
                    RecurringJob.AddOrUpdate(jobItem.JobName, () => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null), Cron.Never,
                                             timeZone, jobItem.QueueName.ToLower());
                    return(true);
                }

                RecurringJob.AddOrUpdate(jobItem.JobName, () => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null), jobItem.Cron,
                                         timeZone, jobItem.QueueName.ToLower());
                return(true);
            }
            catch (Exception ex)
            {
                Logger.ErrorException("HttpJobDispatcher.AddHttprecurringjob", ex);
                return(false);
            }
        }
Example #3
0
 /// <summary>
 /// 添加后台作业
 /// </summary>
 /// <param name="jobItem"></param>
 /// <returns></returns>
 public bool AddHttpbackgroundjob(HttpJobItem jobItem)
 {
     try
     {
         BackgroundJob.Schedule(() => HttpJob.Excute(jobItem, jobItem.JobName, jobItem.QueueName, jobItem.IsRetry, null), TimeSpan.FromMinutes(jobItem.DelayFromMinutes));
         return(true);
     }
     catch (Exception ex)
     {
         Logger.ErrorException("HttpJobDispatcher.AddHttpbackgroundjob", ex);
         return(false);
     }
 }
Example #4
0
 /// <summary>
 /// 添加周期性作业
 /// </summary>
 /// <param name="jobItem"></param>
 /// <returns></returns>
 public bool AddHttprecurringjob(HttpJobItem jobItem)
 {
     try
     {
         RecurringJob.AddOrUpdate(jobItem.JobName, () => HttpJob.Excute(jobItem, jobItem.JobName, null), jobItem.Corn, TimeZoneInfo.Local);
         return(true);
     }
     catch (Exception ex)
     {
         Logger.ErrorException("HttpJobDispatcher.AddHttprecurringjob", ex);
         return(false);
     }
 }
Example #5
0
        /// <summary>
        /// 添加后台作业
        /// </summary>
        /// <param name="jobItem"></param>
        /// <returns></returns>
        public string AddHttpbackgroundjob(HttpJobItem jobItem)
        {
            try
            {
                if (string.IsNullOrEmpty(jobItem.QueueName))
                {
                    jobItem.QueueName = EnqueuedState.DefaultQueue;
                }
                else
                {
                    //get queues from server
                    // ReSharper disable once ReplaceWithSingleCallToFirstOrDefault
                    var server = JobStorage.Current.GetMonitoringApi().Servers().Where(p => p.Queues.Count > 0).FirstOrDefault();
                    // ReSharper disable once PossibleNullReferenceException
                    if (server == null)
                    {
                        return("active server not exist!");
                    }
                    var queues = server.Queues.Select(m => m.ToLower()).ToList();
                    if (!queues.Exists(p => p == jobItem.QueueName.ToLower()) || queues.Count == 0)
                    {
                        Logger.Warn($"HttpJobDispatcher.AddHttpbackgroundjob Error => HttpJobItem.QueueName:`{jobItem.QueueName}` not exist, Use DEFAULT extend!");
                        jobItem.QueueName = EnqueuedState.DefaultQueue;
                    }
                }

                if (!string.IsNullOrEmpty(jobItem.RunAt))
                {
                    //如果设置了 指定的运行时间 先parse一下
                    if (DateTimeOffset.TryParse(jobItem.RunAt, out var runAtTime))
                    {
                        return(BackgroundJob.Schedule(() => HttpJob.Excute(jobItem, null, null, false, null), runAtTime));
                    }
                }

                if (jobItem.DelayFromMinutes <= 0)
                {
                    return(BackgroundJob.Enqueue(() => HttpJob.Excute(jobItem, null, null, false, null)));
                }

                return(BackgroundJob.Schedule(() => HttpJob.Excute(jobItem, null, null, false, null),
                                              TimeSpan.FromMinutes(jobItem.DelayFromMinutes)));
            }
            catch (Exception ex)
            {
                Logger.ErrorException("HttpJobDispatcher.AddHttpbackgroundjob", ex);
                return(null);
            }
        }
        /// <summary>
        /// 添加周期性作业
        /// </summary>
        /// <param name="jobItem"></param>
        /// <returns></returns>
        public bool AddHttprecurringjob(HttpJobItem jobItem)
        {
            if (string.IsNullOrEmpty(jobItem.QueueName))
            {
                jobItem.QueueName = "DEFAULT";
            }
            else
            {
                //get queues from server
                // ReSharper disable once ReplaceWithSingleCallToFirstOrDefault
                var server = JobStorage.Current.GetMonitoringApi().Servers().Where(p => p.Queues.Count > 0).FirstOrDefault();
                // ReSharper disable once PossibleNullReferenceException
                var queues = server.Queues.ToList();
                if (!queues.Exists(p => p == jobItem.QueueName.ToLower()) || queues.Count == 0)
                {
                    Logger.Warn("HttpJobDispatcher.AddHttprecurringjob Error => HttpJobItem.QueueName not exist, Use DEFAULT extend!");
                }
            }

            var queueName = !string.IsNullOrEmpty(jobItem.AgentClass) ? "JobAgent" : jobItem.QueueName;

            if (string.IsNullOrEmpty(queueName))
            {
                queueName = "DEFAULT";
            }


            try
            {
                //支持添加一个 只能手动出发的
                if (string.IsNullOrEmpty(jobItem.Cron))
                {
                    RecurringJob.AddOrUpdate(jobItem.JobName, () => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null), Cron.Never, TimeZoneInfo.Local, jobItem.QueueName.ToLower());
                    return(true);
                }

                RecurringJob.AddOrUpdate(jobItem.JobName, () => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null), jobItem.Cron, TimeZoneInfo.Local, jobItem.QueueName.ToLower());
                return(true);
            }
            catch (Exception ex)
            {
                Logger.ErrorException("HttpJobDispatcher.AddHttprecurringjob", ex);
                return(false);
            }
        }
        public bool AddHttpbackgroundjob(HttpJobItem jobItem)
        {
            try
            {
                // 普通作业
                // 单纯的httpjob 有设置延迟
                // 单纯的httpjob 没有设置延迟  但是不可以不设置延迟 所以就设置一个非常大的延迟 比如100年后
                // 以agent模式开发的job 有设置延迟
                // 以agent模式开发的job 没有设置延迟
                // 没有设置延迟 代表的只可以自己触发
                var queueName = !string.IsNullOrEmpty(jobItem.AgentClass) ? "JobAgent" : jobItem.QueueName;
                if (string.IsNullOrEmpty(queueName))
                {
                    queueName = "DEFAULT";
                }
                if (jobItem.DelayFromMinutes == -1) //约定
                {
                    //代表设置的是智能自己触发的延迟job
                    var jobId = BackgroundJob.Schedule(() => HttpJob.Excute(jobItem, jobItem.JobName + (!string.IsNullOrEmpty(jobItem.AgentClass) ? "| JobAgent |" : ""), "multiple", jobItem.EnableRetry, null), DateTimeOffset.Now.AddYears(100));

                    //自己触发完成后再把自己添加一遍
                    BackgroundJob.ContinueJobWith(jobId, () => AddHttpbackgroundjob(jobItem), JobContinuationOptions.OnAnyFinishedState);
                    return(true);
                }

                if (jobItem.DelayFromMinutes == 0)
                {
                    BackgroundJob.Enqueue(() => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null));
                    return(true);
                }

                BackgroundJob.Schedule(() => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null), TimeSpan.FromMinutes(jobItem.DelayFromMinutes));
                return(true);
            }
            catch (Exception ex)
            {
                Logger.ErrorException("HttpJobDispatcher.AddHttpbackgroundjob", ex);
                return(false);
            }
        }
Example #8
0
        /// <summary>
        /// 添加周期性作业
        /// </summary>
        /// <param name="jobItem"></param>
        /// <returns></returns>
        public bool AddHttprecurringjob(HttpJobItem jobItem)
        {
            //get queues from server
            var server = JobStorage.Current.GetMonitoringApi().Servers().
                         Where(p => p.Queues.Count > 0).FirstOrDefault();
            var queues = server.Queues.ToList();

            if (!queues.Exists(p => p == jobItem.QueueName.ToLower()) || queues.Count == 0)
            {
                return(false);
            }
            try
            {
                RecurringJob.AddOrUpdate(jobItem.JobName, () => HttpJob.Excute(jobItem, jobItem.JobName, jobItem.QueueName, jobItem.IsRetry, null), jobItem.Corn, TimeZoneInfo.Local, jobItem.QueueName.ToLower());
                return(true);
            }
            catch (Exception ex)
            {
                Logger.ErrorException("HttpJobDispatcher.AddHttprecurringjob", ex);
                return(false);
            }
        }
Example #9
0
        /// <summary>
        /// 添加周期性作业
        /// </summary>
        /// <returns></returns>
        public string AddHttprecurringjob(HttpJobItem jobItem, bool addOnly = false)
        {
            if (string.IsNullOrEmpty(jobItem.QueueName))
            {
                jobItem.QueueName = EnqueuedState.DefaultQueue;
            }
            else
            {
                //get queues from server
                // ReSharper disable once ReplaceWithSingleCallToFirstOrDefault
                var server = JobStorage.Current.GetMonitoringApi().Servers().Where(p => p.Queues.Count > 0).FirstOrDefault();
                // ReSharper disable once PossibleNullReferenceException
                if (server == null)
                {
                    return("active server not exist!");
                }
                var queues = server.Queues.Select(m => m.ToLower()).ToList();
                if (!queues.Exists(p => p == jobItem.QueueName.ToLower()) || queues.Count == 0)
                {
                    Logger.Warn($"HttpJobDispatcher.AddHttprecurringjob Error => HttpJobItem.QueueName:`{jobItem.QueueName}` not exist, Use DEFAULT extend!");
                    jobItem.QueueName = EnqueuedState.DefaultQueue;
                }
            }

            try
            {
                // 先用每个job配置的 如果没有就用系统配置的 在没有就用Local
                TimeZoneInfo timeZone = null;
                if (!string.IsNullOrEmpty(jobItem.TimeZone))
                {
                    timeZone = TimeZoneInfoHelper.OlsonTimeZoneToTimeZoneInfo(jobItem.TimeZone);
                }

                //https://github.com/yuzd/Hangfire.HttpJob/issues/78
                var jobidentifier = string.IsNullOrEmpty(jobItem.RecurringJobIdentifier) ? jobItem.JobName : jobItem.RecurringJobIdentifier;
                if (addOnly)
                {
                    using (var connection = JobStorage.Current.GetConnection())
                    {
                        var existItem = connection.GetAllEntriesFromHash("recurring-job:" + jobidentifier);
                        if (existItem != null && existItem.Count > 0)
                        {
                            return(jobidentifier + "is registerd!");
                        }
                    }
                }

                if (timeZone == null)
                {
                    timeZone = TimeZoneInfo.Local;
                }
                if (string.IsNullOrEmpty(jobItem.Cron))
                {
                    //支持添加一个 只能手动出发的
                    RecurringJob.AddOrUpdate(jobidentifier, () => HttpJob.Excute(jobItem, null, null, false, null), Cron.Never,
                                             timeZone, jobItem.QueueName.ToLower());
                    return(string.Empty);
                }

                RecurringJob.AddOrUpdate(jobidentifier, () => HttpJob.Excute(jobItem, null, null, false, null), jobItem.Cron,
                                         timeZone, jobItem.QueueName.ToLower());
                return(string.Empty);
            }
            catch (Exception ex)
            {
                Logger.ErrorException("HttpJobDispatcher.AddHttprecurringjob", ex);
                return(ex.Message);
            }
        }