Exemplo n.º 1
0
        private static async Task Start(IHosSchedule schedule)
        {
            JobDataMap map = new JobDataMap
            {
                new KeyValuePair <string, object> ("instance", schedule),
            };
            string jobKey = schedule.Main.Id.ToString();

            try
            {
                IJobDetail job = JobBuilder.Create().OfType(schedule.GetQuartzJobType()).WithIdentity(jobKey).UsingJobData(map).Build();

                //添加监听器
                var listener = new JobRunListener(jobKey);
                listener.OnSuccess += StartedEvent;
                _scheduler.ListenerManager.AddJobListener(listener, KeyMatcher <JobKey> .KeyEquals(new JobKey(jobKey)));

                await _scheduler.ScheduleJob(job, GetTrigger(schedule.Main), schedule.CancellationTokenSource.Token);
            }
            catch (Exception ex)
            {
                throw new SchedulerException(ex);
            }
            LogHelper.Info($"任务[{schedule.Main.Title}]启动成功!", schedule.Main.Id);

            _ = Task.Run(() =>
            {
                while (true)
                {
                    if (schedule.RunnableInstance == null)
                    {
                        break;
                    }
                    var log = schedule.RunnableInstance.ReadLog();
                    if (log != null)
                    {
                        LogManager.Queue.Write(new SystemLogEntity
                        {
                            Category   = log.Category,
                            Message    = log.Message,
                            ScheduleId = log.ScheduleId,
                            Node       = log.Node,
                            StackTrace = log.StackTrace,
                            TraceId    = log.TraceId,
                            CreateTime = log.CreateTime
                        });
                    }
                    else
                    {
                        Thread.Sleep(3000);
                    }
                }
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// 启动一个任务,带重试机制
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public static async Task <bool> StartWithRetry(Guid sid)
        {
            var jk = new JobKey(sid.ToString().ToLower());

            if (await _scheduler.CheckExists(jk))
            {
                return(true);
            }
            ScheduleContext context  = GetScheduleContext(sid);
            IHosSchedule    schedule = await HosScheduleFactory.GetHosSchedule(context);

            try
            {
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        await Start(schedule);

                        return(true);
                    }
                    catch (SchedulerException sexp)
                    {
                        LogHelper.Error($"任务启动失败!开始第{i + 1}次重试...", sexp, context.Schedule.Id);
                    }
                }
                //最后一次尝试
                await Start(schedule);

                return(true);
            }
            catch (SchedulerException sexp)
            {
                LogHelper.Error($"任务所有重试都失败了,已放弃启动!", sexp, context.Schedule.Id);
                return(false);
            }
            catch (Exception exp)
            {
                LogHelper.Error($"任务启动失败!", exp, context.Schedule.Id);
                return(false);
            }
        }
Exemplo n.º 3
0
        private static async Task Start(IHosSchedule schedule)
        {
            JobDataMap map = new JobDataMap
            {
                new KeyValuePair <string, object> ("instance", schedule),
            };
            string jobKey = schedule.Main.Id.ToString();

            try
            {
                IJobDetail job = JobBuilder.Create().OfType(schedule.GetQuartzJobType()).WithIdentity(jobKey).UsingJobData(map).Build();

                //添加监听器
                var listener = new JobRunListener(jobKey);
                listener.OnSuccess += StartedEvent;
                _scheduler.ListenerManager.AddJobListener(listener, KeyMatcher <JobKey> .KeyEquals(new JobKey(jobKey)));

                ITrigger trigger = GetTrigger(schedule.Main);
                await _scheduler.ScheduleJob(job, trigger, schedule.CancellationTokenSource.Token);

                using (var scope = new Core.ScopeDbContext())
                {
                    var db   = scope.GetDbContext();
                    var task = db.Schedules.FirstOrDefault(x => x.Id == schedule.Main.Id);
                    if (task != null)
                    {
                        task.NextRunTime = TimeZoneInfo.ConvertTimeFromUtc(trigger.GetNextFireTimeUtc().Value.UtcDateTime, TimeZoneInfo.Local);
                        await db.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SchedulerException(ex);
            }
            LogHelper.Info($"任务[{schedule.Main.Title}]启动成功!", schedule.Main.Id);

            _ = Task.Run(async() =>
            {
                while (true)
                {
                    if (schedule.RunnableInstance == null)
                    {
                        break;
                    }
                    var log = schedule.RunnableInstance.ReadLog();
                    if (log != null)
                    {
                        LogManager.Queue.Write(new SystemLogEntity
                        {
                            Category   = log.Category,
                            Message    = log.Message,
                            ScheduleId = log.ScheduleId,
                            Node       = log.Node,
                            StackTrace = log.StackTrace,
                            TraceId    = log.TraceId,
                            CreateTime = log.CreateTime
                        });
                    }
                    else
                    {
                        await Task.Delay(3000);
                    }
                }
            });
        }