/// <summary> /// 定时执行事件 /// </summary> /// <param name="source"></param> /// <param name="e"></param> public void TimedExecuteJob(object source, System.Timers.ElapsedEventArgs e) { try { SchedulerHelper scheduler = new SchedulerHelper(); //查询变动的数据 List <SchedulerConfigModel> list = SchedulerConfigQuery.GetSchedulerRecord((int)JobEnum.DataType.UpdateData); if (list == null || list.Count == 0) { return; } for (var i = 0; i < list.Count; i++) { //新增|更新 if (list[i].Status == (int)JobEnum.DataState.Insert || list[i].Status == (int)JobEnum.DataState.Update) { var _delete = scheduler.IsExistsDelJob(list[i].JobName, list[i].JobGroup); if (_delete) { //启用新的Job var _update = ExecuteWebApi(list[i]); if (_update) { SchedulerConfigQuery.UpdateStatus(list[i].ID); } } } //禁用Job else if (list[i].Status == (int)JobEnum.DataState.Delete) { var _delete = scheduler.IsExistsDelJob(list[i].JobName, list[i].JobGroup); LogWriter.info("Job-" + list[i].JobName + (_delete ? "已禁用" : "禁用失败") + ",执行时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); if (_delete) { SchedulerConfigQuery.UpdateStatus(list[i].ID); } } } } catch (Exception ex) { LogWriter.error("定时获取数据库数据执行Job异常:" + ex.ToString() + "/r/n" + ex.StackTrace); } }
protected override void OnStart(string[] args) { try { LogWriter.info("服务启动" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); List <SchedulerConfigModel> list = SchedulerConfigQuery.GetSchedulerRecord((int)JobEnum.DataType.EnableData); if (list != null && list.Count > 0) { for (var i = 0; i < list.Count; i++) { ExecuteWebApi(list[i]); } } } catch (Exception ex) { LogWriter.error("服务启动异常:" + ex.ToString()); } }