示例#1
0
        /// <summary>
        /// 按绝对时间插入任务
        /// </summary>
        /// <param name="plan"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static bool Insert(NotifyPlan plan, DateTime time)
        {
            //System.Diagnostics.Debug.WriteLine($"插入时间{DateTime.Now}");
            var seconds = Math.Round((time - DateTime.Now).TotalSeconds, 0, MidpointRounding.AwayFromZero);

            if (seconds <= 0)
            {
                return(false);
            }
            //System.Diagnostics.Debug.WriteLine($"时间差{seconds}");
            return(_queue.Insert(plan, (int)seconds));
        }
示例#2
0
        /// <summary>
        /// 根据任务实体插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private static bool InsertByEntity(ScheduleDelayedEntity entity)
        {
            bool       success = false;
            NotifyPlan plan    = new NotifyPlan()
            {
                Key            = entity.Id.ToString(),
                NotifyUrl      = entity.NotifyUrl,
                NotifyDataType = entity.NotifyDataType,
                NotifyBody     = entity.NotifyBody,
                Callback       = NotifyExecutedEvent
            };
            string pattern = ConfigurationCache.GetField <string>("DelayTask_DelayPattern");

            if (pattern.ToLower() == "absolute")
            {
                success = Insert(plan, entity.DelayAbsoluteTime);
            }
            else
            {
                success = Insert(plan, entity.DelayTimeSpan);
            }
            return(success);
        }
示例#3
0
 /// <summary>
 /// 按相对时间插入任务
 /// </summary>
 /// <param name="plan"></param>
 /// <param name="delaySeconds"></param>
 /// <returns></returns>
 public static bool Insert(NotifyPlan plan, int delaySeconds)
 {
     return(_queue.Insert(plan, delaySeconds));
 }
示例#4
0
        private static async Task NotifyRequest(NotifyPlan plan)
        {
            Guid sid     = Guid.Parse(plan.Key);
            Guid traceId = Guid.NewGuid();

            using (var scope = new ScopeDbContext())
            {
                var db     = scope.GetDbContext();
                var tracer = scope.GetService <Common.RunTracer>();

                var entity = await db.ScheduleDelayeds.FirstOrDefaultAsync(x => x.Id == sid);

                if (entity == null)
                {
                    LogHelper.Info($"不存在的任务ID。", sid, traceId);
                    return;
                }

                entity.ExecuteTime = DateTime.Now;
                Exception failedException = null;

                try
                {
                    //创建一条trace
                    await tracer.Begin(traceId, plan.Key);

                    var httpClient = scope.GetService <IHttpClientFactory>().CreateClient();
                    plan.NotifyBody = plan.NotifyBody.Replace("\r\n", "");
                    HttpContent reqContent = new StringContent(plan.NotifyBody, System.Text.Encoding.UTF8, "application/json");
                    if (plan.NotifyDataType == "application/x-www-form-urlencoded")
                    {
                        //任务创建时要确保参数是键值对的json格式
                        reqContent = new FormUrlEncodedContent(Newtonsoft.Json.JsonConvert.DeserializeObject <IEnumerable <KeyValuePair <string, string> > >(plan.NotifyBody));
                    }

                    LogHelper.Info($"即将请求:{entity.NotifyUrl}", sid, traceId);
                    var response = await httpClient.PostAsync(plan.NotifyUrl, reqContent);

                    var content = await response.Content.ReadAsStringAsync();

                    LogHelper.Info($"请求结束,响应码:{response.StatusCode.GetHashCode().ToString()},响应内容:{(response.Content.Headers.GetValues("Content-Type").Any(x => x.Contains("text/html")) ? "html文档" : content)}", sid, traceId);

                    if (response.IsSuccessStatusCode && content.Contains("success"))
                    {
                        await tracer.Complete(ScheduleRunResult.Success);

                        //更新结果字段
                        entity.FinishTime = DateTime.Now;
                        entity.Status     = (int)ScheduleDelayStatus.Successed;
                        //更新日志
                        LogHelper.Info($"延时任务[{entity.Topic}:{entity.ContentKey}]执行成功。", sid, traceId);
                    }
                    else
                    {
                        failedException = new Exception("异常的返回结果。");
                    }
                }
                catch (Exception ex)
                {
                    failedException = ex;
                }
                // 对异常进行处理
                if (failedException != null)
                {
                    //更新trace
                    await tracer.Complete(ScheduleRunResult.Failed);

                    //失败重试策略
                    int maxRetry = ConfigurationCache.GetField <int>("DelayTask_RetryTimes");
                    if (entity.FailedRetrys < (maxRetry > 0 ? maxRetry : 3))
                    {
                        //更新结果字段
                        entity.FailedRetrys++;
                        //计算下次延时间隔
                        int timespan = ConfigurationCache.GetField <int>("DelayTask_RetrySpans");
                        int delay    = (timespan > 0 ? timespan : 10) * entity.FailedRetrys;
                        //重新进入延时队列
                        Insert(plan, delay);
                        //更新日志
                        LogHelper.Error($"延时任务[{entity.Topic}:{entity.ContentKey}]执行失败,将在{delay.ToString()}秒后开始第{entity.FailedRetrys.ToString()}次重试。", failedException, sid, traceId);
                    }
                    else
                    {
                        entity.Status = (int)ScheduleDelayStatus.Failed;
                        entity.Remark = $"重试{entity.FailedRetrys}次后失败结束";
                        //更新日志
                        LogHelper.Error($"延时任务[{entity.Topic}:{entity.ContentKey}]重试{entity.FailedRetrys}次后失败结束。", failedException, sid, traceId);
                        //邮件通知
                        var user = await db.SystemUsers.FirstOrDefaultAsync(x => x.UserName == entity.CreateUserName && !string.IsNullOrEmpty(x.Email));

                        if (user != null)
                        {
                            var keeper = new List <KeyValuePair <string, string> >()
                            {
                                new KeyValuePair <string, string>(user.RealName, user.Email)
                            };
                            MailKitHelper.SendMail(keeper, $"延时任务异常 — {entity.Topic}:{entity.ContentKey}",
                                                   Common.QuartzManager.GetErrorEmailContent($"{entity.Topic}:{entity.ContentKey}", failedException)
                                                   , sid: sid, traceId: traceId);
                        }
                        else
                        {
                            LogHelper.Error($"用户无邮箱,无法发送邮件:{entity.CreateUserName}", new Exception("用户无邮箱"), sid, traceId);
                        }
                    }
                    // .....
                    // 其实这个重试策略稍微有点问题,只能在抢锁成功的节点上进行重试,如果遭遇单点故障会导致任务丢失
                    // 严格来说应该通知到master让其对所有节点执行重试策略,但考虑到master也会有单点问题,综合考虑后还是放到当前worker中重试,若worker节点异常可以在控制台中人工干预进行重置或立即执行
                }
                db.Update(entity);
                await db.SaveChangesAsync();
            }
        }