Esempio n. 1
0
        private async Task RenewalManager(TimeSpan checkInterval, TimeSpan timeReserve, CancellationToken cancellationToken)
        {
            var ttlMinimum = checkInterval + timeReserve;

            while (!cancellationToken.IsCancellationRequested)
            {
                var currentTime = DateTime.UtcNow;
                var batch       = new List <BrokeredMessage>();
                foreach (var message in _attachedMessages.Values)
                {
                    var ttl = message.LockedUntilUtc - currentTime;
                    if (ttl <= TimeSpan.Zero)
                    {
                        BrokeredMessage detachedMessage;
                        _tracer.ErrorFormat("Can not schedule renewal: lock expired, lock: {0}", message.LockToken);
                        _attachedMessages.TryRemove(message.LockToken, out detachedMessage);
                    }

                    if (ttl <= ttlMinimum)
                    {
                        batch.Add(message);
                    }
                }

                if (batch.Any())
                {
                    _renewalQueue.TryAdd(batch);
                }
                batch = null;
                await Task.Delay(checkInterval, cancellationToken);
            }
        }
Esempio n. 2
0
 public override void Log(ExceptionLoggerContext context)
 {
     _tracer.ErrorFormat(context.Exception, "Unhandled exception processing {0} for {1}", context.Request.Method, context.Request.RequestUri);
 }
Esempio n. 3
0
        /// <summary>
        /// Note:序列化,从配置中获取所有数据
        /// </summary>
        /// <returns></returns>
        public void Initialize()
        {
            using (var reader = XmlReader.Create(GetFullPath(FILE_NAME)))
            {
                logRun.Debug("任务配置实例初始化开始...");
                var slz = new XmlSerializer(typeof(TaskConfig));
                var rst = slz.Deserialize(reader) as TaskConfig;
                reader.Close();
                _instance = rst; //获得实例
                if (_instance != null && _instance.Tasks.Count > 0)
                {
                    foreach (var task in _instance.Tasks)
                    {
                        //相关配置初始化,保持默认值的合法性

                        //锁定超时至少5秒
                        if (task.WorkSetting.Timeout < 5)
                        {
                            task.WorkSetting.Timeout = 5;
                        }
                        var taskId = task.Meta.Id;
                        //1. 任务Id唯一性检测
                        var tmpJobs = _instance.Tasks.FindAll(x => x.Meta.Id == taskId);
                        //logRun.Debug($"jobs[{taskId}]={tmpJobs.Count}");
                        if (tmpJobs.Count < 2)
                        {
                            //没有重复的任务Id
                            task.Meta.TaskHash = task.Type.GetHashCode();
                            logRun.Debug($"{task}的任务Hash:{task.Meta.TaskHash}");
                        }
                        else
                        {
                            var msg = $"Task.Id={taskId}重复,{FILE_NAME}里每一个Job的Id必须是与其它任务的Id不同的数字。";
                            logRun.ErrorFormat(msg);
                            throw new ConfigurationErrorsException(msg);
                        }
                    }
                }
            }


            if (_instance == null)
            {
                var msg = $"请查检{FILE_NAME}配置文件是否存在并且配置正确。";
                logRun.ErrorFormat(msg);
                throw new ConfigurationErrorsException(msg);
            }

            //计时器初始化
            if (_instance.WatchTimer == null)
            {
                _instance.WatchTimer = new TimerConfig();
            }

            //初始化运行状态
            //instance.Execution = ExecutionStatus.Instance();
            logRun.DebugFormat("初始化一个新的XML运行状态实例");
            _execution = ExecutionStatus.Instance();

            //var instance =  Serializer.XmlDeserializerFormFile<TaskConfig>();
            //将上次运行状态恢复至本实例中
            foreach (var task in _instance.Tasks)
            {
                var jobId = task.Meta.Id;
                //任务状态初始化或匹配
                var job = _execution.Tasks[jobId]; //查询Job的运行情况
                if (job == null)
                {
                    logRun.DebugFormat("开始初始化任务({0})运行状态[新的]。", task.Meta.Name);
                    task.Execution = new ExecutionInfo(); //初始化新执行状态
                    _execution.Tasks.Add(task.Meta);      //工作运行状态增加
                }
                else
                {
                    logRun.DebugFormat("开始读取任务({0})运行状态[已有的]。", task.Meta.Name);
                    //task.Execution = _execution.Tasks[task.Meta.Id].Execution;      //运行配置上的状态引用更新到任务上

                    //Note:序列化时将状态置为默认,因为有些异常会奖Runing记下使得该任务再也无法运行了。
                    task.Execution.RunStatus = TaskRunStatusType.Default;
                }
                task.Execution.IsExsit = true;
            }

            ////更新已移除的任务状态
            //foreach (var job in _execution.Tasks)
            //{
            //    if (!job.Execution.IsExsit)
            //    {
            //        job.Execution.RunStatus = TaskRunStatusType.Removing;
            //    }
            //}
            //Save();
            logRun.DebugFormat("任务配置实例化完成。");
            _instance.Execution = _execution;
        }