Beispiel #1
0
        /// <summary>
        /// 开始执行全部
        /// </summary>
        public void Start()
        {
            //Note:线程锁定
            lock (this)
            {
                Logger.Debug("----- 服务启动开始 -----");

                //初始化任务集合
                Tasks = new List <AbstractTask>();

                //载入所有配置
                TaskSetting          = TaskConfig.GetInstance();
                TaskSetting.Changed += TaskConfigChanged; //只保留一次的事件

                //Note:初始化计时器
                _watcher          = new Timer(TaskSetting.WatchTimer.DelayMillisecond); //Note:第一次运行不按间隔时间执行。
                _watcher.Elapsed += Working;
                _watcher.Start();                                                       //启动工作回调
                Logger.Debug("监视工人第一次工作将于{0}后执行", TimeSpan.FromMilliseconds(_watcher.Interval));
                Logger.Debug("----- 服务启动完成 -----");
            }
        }
Beispiel #2
0
        /// <summary>
        /// Note:序列化,从配置中获取所有数据
        /// </summary>
        /// <returns></returns>
        public void Initialize()
        {
            using (var reader = XmlReader.Create(GetFullPath(FILE_NAME)))
            {
                Logger.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)
                    {
                        //相关配置初始化,保持默认值的合法性
                        if (task.WorkSetting == null)
                        {
                            task.WorkSetting = new TaskWorkSettingInfo();
                        }

                        //锁定超时至少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();
                            Logger.Debug("{0}的任务Hash:{1}", task, task.Meta.TaskHash);
                        }
                        else
                        {
                            var msg = String.Format("Task.Id={0}重复,{1}里每一个Job的Id必须是与其它任务的Id不同的数字。", taskId, FILE_NAME);
                            Logger.Error(msg);
                            throw new ConfigurationErrorsException(msg);
                        }
                    }
                }
            }


            if (_instance == null)
            {
                var msg = String.Format("请查检{0}配置文件是否存在并且配置正确。", FILE_NAME);
                Logger.Error(msg);
                throw new ConfigurationErrorsException(msg);
            }

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

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

            //将上次运行状态恢复至本实例中
            foreach (var task in _instance.Tasks)
            {
                var jobId = task.Meta.Id;
                //任务状态初始化或匹配
                var job = _execution.Tasks[jobId]; //查询Job的运行情况
                if (job == null)
                {
                    Logger.Debug("开始初始化任务({0})运行状态[新的]。", task.Meta.Name);
                    task.Execution = new ExecutionInfo(); //初始化新执行状态
                    _execution.Tasks.Add(task.Meta);      //工作运行状态增加
                }
                else
                {
                    Logger.Debug("开始读取任务({0})运行状态[已有的]。", task.Meta.Name);
                    //Note:序列化时将状态置为默认,因为有些异常会奖Runing记下使得该任务再也无法运行了。
                    task.Execution.RunStatus = TaskRunStatus.Default;
                }

                task.Execution.IsExsit = true;
            }

            Logger.Debug("任务配置实例化完成。");
            _instance.Execution = _execution;
        }