Esempio n. 1
0
        /// <summary>
        /// Note:序列化,从配置中获取所有数据
        /// </summary>
        /// <returns></returns>
        public static ExecutionStatus Instance()
        {
            ExecutionStatus executionStatus;

            try
            {
                var fullFileName = GetFullPath(FILE_NAME);
                var exists       = File.Exists(fullFileName);
                if (!exists)
                {
                    executionStatus = new ExecutionStatus();
                }
                else
                {
                    using (var reader = XmlReader.Create(fullFileName))
                    {
                        var slz = new XmlSerializer(typeof(ExecutionStatus));
                        var rst = slz.Deserialize(reader) as ExecutionStatus;
                        reader.Close();
                        executionStatus = rst;
                    }
                }
            }
            catch (Exception ex)
            {
                executionStatus = new ExecutionStatus();   //文件未找到的异常,自已创建一个了
                log.Fatal(ex, "实例化任务状态时异常");
            }
            return(executionStatus);
        }
Esempio n. 2
0
        /// <summary>
        /// Note:序列化,从配置中获取所有数据
        /// </summary>
        /// <returns></returns>
        public void Initialize()
        {
            using (var reader = XmlReader.Create(GetFullPath(FILE_NAME)))
            {
                Log.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);
                        //log.Debug($"jobs[{taskId}]={tmpJobs.Count}");
                        if (tmpJobs.Count < 2)
                        {
                            //没有重复的任务Id
                            task.Meta.TaskHash = task.Type.GetHashCode();
                            Log.Debug($"{task}的任务Hash:{task.Meta.TaskHash}");
                        }
                        else
                        {
                            var msg = $"Task.Id={taskId}重复,{FILE_NAME}里每一个Job的Id必须是与其它任务的Id不同的数字。";
                            Log.Error(msg);
                            throw new ConfigException(msg);
                        }
                    }
                }
            }


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

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

            //初始化运行状态
            //instance.Execution = ExecutionStatus.Instance();
            Log.Debug("初始化一个新的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)
                {
                    Log.Debug("开始初始化任务({0})运行状态[新的]。", task.Meta.Name);
                    task.Execution = new ExecutionInfo(); //初始化新执行状态
                    _execution.Tasks.Add(task.Meta);      //工作运行状态增加
                }
                else
                {
                    Log.Debug("开始读取任务({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();
            Log.Debug("任务配置实例化完成。");
            _instance.Execution = _execution;
        }