public void RunBatchJobs(IWindsorContainer container) { log.Info("----------------------------------Invincible's dividing line---------------------------------------"); log.Info("BatchJobs run start."); IList<BatchTrigger> tobeFiredTriggerList = this.batchTriggerMgrE.GetTobeFiredTrigger(); if (tobeFiredTriggerList != null && tobeFiredTriggerList.Count > 0) { foreach (BatchTrigger tobeFiredTrigger in tobeFiredTriggerList) { BatchJobDetail jobDetail = tobeFiredTrigger.BatchJobDetail; BatchRunLog runLog = new BatchRunLog(); try { #region Job运行前处理 log.Info("Start run job. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name); runLog.BatchJobDetail = jobDetail; runLog.BatchTrigger = tobeFiredTrigger; runLog.StartTime = DateTime.Now; runLog.Status = "InProcess"; this.batchRunLogMgrE.CreateBatchRunLog(runLog); #endregion #region 运行Job JobDataMap dataMap = new JobDataMap(); #region Job参数获取 IList<BatchJobParameter> batchJobParameterList = this.batchJobParameterMgrE.GetBatchJobParameter(jobDetail.Id); if (batchJobParameterList != null && batchJobParameterList.Count > 0) { foreach (BatchJobParameter batchJobParameter in batchJobParameterList) { log.Debug("Set Job Parameter Name:" + batchJobParameter.ParameterName + ", Value:" + batchJobParameter.ParameterValue); dataMap.PutData(batchJobParameter.ParameterName, batchJobParameter.ParameterValue); } } #endregion #region Trigger参数获取 IList<BatchTriggerParameter> batchTriggerParameterList = this.batchTriggerParameterMgrE.GetBatchTriggerParameter(tobeFiredTrigger.Id); if (batchTriggerParameterList != null && batchTriggerParameterList.Count > 0) { foreach (BatchTriggerParameter batchTriggerParameter in batchTriggerParameterList) { log.Debug("Set Trigger Parameter Name:" + batchTriggerParameter.ParameterName + ", Value:" + batchTriggerParameter.ParameterValue); dataMap.PutData(batchTriggerParameter.ParameterName, batchTriggerParameter.ParameterValue); } } #endregion #region 初始化JobRunContext JobRunContext jobRunContext = new JobRunContext(tobeFiredTrigger, jobDetail, dataMap, container); #endregion #region 调用Job IJob job = container.Resolve<IJob>(jobDetail.ServiceName); log.Debug("Start run job: " + jobDetail.ServiceName); job.Execute(jobRunContext); #endregion #endregion #region Job运行后处理 log.Info("Job run successful. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name); runLog.EndTime = DateTime.Now; runLog.Status = "Successful"; this.batchRunLogMgrE.UpdateBatchRunLog(runLog); #endregion } catch (Exception ex) { try { log.Error("Job run failure. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name, ex); runLog.EndTime = DateTime.Now; runLog.Status = "Failure"; if (ex.Message != null && ex.Message.Length > 255) { runLog.Message = ex.Message.Substring(0, 255); } else { runLog.Message = ex.Message; } this.batchRunLogMgrE.UpdateBatchRunLog(runLog); } catch (Exception ex1) { log.Error("", ex1); } } finally { #region 更新BatchTrigger try { BatchTrigger oldTobeFiredTrigger = this.batchTriggerMgrE.LoadBatchTrigger(tobeFiredTrigger.Id); oldTobeFiredTrigger.TimesTriggered++; oldTobeFiredTrigger.PreviousFireTime = oldTobeFiredTrigger.NextFireTime; if (oldTobeFiredTrigger.RepeatCount != 0 && oldTobeFiredTrigger.TimesTriggered >= oldTobeFiredTrigger.RepeatCount) { //关闭Trigger log.Debug("Close Trigger:" + oldTobeFiredTrigger.Name); oldTobeFiredTrigger.Status = BusinessConstants.CODE_MASTER_STATUS_VALUE_CLOSE; oldTobeFiredTrigger.NextFireTime = null; } else { //设置下次运行时间 log.Debug("Set Trigger Next Start Time, Add:" + oldTobeFiredTrigger.Interval.ToString() + " " + oldTobeFiredTrigger.IntervalType); //if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_YEAR) //{ // oldTobeFiredTrigger.NextFireTime = dateTimeNow.AddYears(oldTobeFiredTrigger.Interval); //} //else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_MONTH) //{ // oldTobeFiredTrigger.NextFireTime = dateTimeNow.AddMonths(oldTobeFiredTrigger.Interval); //} //else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_DAY) //{ // oldTobeFiredTrigger.NextFireTime = dateTimeNow.AddDays(oldTobeFiredTrigger.Interval); //} //else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_HOUR) //{ // oldTobeFiredTrigger.NextFireTime = dateTimeNow.AddHours(oldTobeFiredTrigger.Interval); //} //else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_MINUTE) //{ // oldTobeFiredTrigger.NextFireTime = dateTimeNow.AddMinutes(oldTobeFiredTrigger.Interval); //} //else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_SECOND) //{ // oldTobeFiredTrigger.NextFireTime = dateTimeNow.AddSeconds(oldTobeFiredTrigger.Interval); //} //else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_MILLISECOND) //{ // oldTobeFiredTrigger.NextFireTime = dateTimeNow.AddMilliseconds(oldTobeFiredTrigger.Interval); //} DateTime dateTimeNow = DateTime.Now; if (!oldTobeFiredTrigger.NextFireTime.HasValue) { oldTobeFiredTrigger.NextFireTime = dateTimeNow; } while (oldTobeFiredTrigger.NextFireTime.Value <= dateTimeNow) { if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_YEAR) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddYears(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_MONTH) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddMonths(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_DAY) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddDays(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_HOUR) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddHours(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_MINUTE) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddMinutes(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_SECOND) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddSeconds(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_MILLISECOND) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddMilliseconds(oldTobeFiredTrigger.Interval); } else { throw new ArgumentException("invalid Interval Type:" + oldTobeFiredTrigger.IntervalType); } } log.Debug("Trigger Next Start Time is set as:" + oldTobeFiredTrigger.NextFireTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); } this.batchTriggerMgrE.UpdateBatchTrigger(oldTobeFiredTrigger); } catch (Exception ex) { log.Error("Error occur when update batch trigger.", ex); } #endregion } } } else { log.Info("No job found may run in this batch."); } log.Info("BatchJobs run end."); }
public virtual void DeleteBatchRunLog(BatchRunLog entity) { entityDao.DeleteBatchRunLog(entity); }
public virtual void UpdateBatchRunLog(BatchRunLog entity) { entityDao.UpdateBatchRunLog(entity); }
public virtual void DeleteBatchRunLog(BatchRunLog entity) { Delete(entity); }
public virtual void CreateBatchRunLog(BatchRunLog entity) { entityDao.CreateBatchRunLog(entity); }
public virtual void CreateBatchRunLog(BatchRunLog entity) { Create(entity); }
public virtual void UpdateBatchRunLog(BatchRunLog entity) { Update(entity); }
private void Run(IWindsorContainer container, bool isLeanEngine, bool isReceiveWO) { if (isLeanEngine) { lelog.Info("----------------------------------Invincible's dividing line---------------------------------------"); lelog.Info("BatchJobs run start."); } else if (isReceiveWO) { rwolog.Info("----------------------------------Invincible's dividing line---------------------------------------"); rwolog.Info("BatchJobs run start."); } else { log.Info("----------------------------------Invincible's dividing line---------------------------------------"); log.Info("BatchJobs run start."); } IList <BatchTrigger> tobeFiredTriggerList = this.batchTriggerMgr.GetTobeFiredTrigger(); if (tobeFiredTriggerList != null && tobeFiredTriggerList.Count > 0) { foreach (BatchTrigger tobeFiredTrigger in tobeFiredTriggerList) { bool isSuccess = true; if (isLeanEngine && tobeFiredTrigger.Id != 2 && tobeFiredTrigger.Id != 23 && tobeFiredTrigger.Id != 51 && tobeFiredTrigger.Id != 54 && tobeFiredTrigger.Id != 55 && tobeFiredTrigger.Id != 56 && tobeFiredTrigger.Id != 7) { continue; } if (isReceiveWO && tobeFiredTrigger.Id != 57 && tobeFiredTrigger.Id != 50) { continue; } if (!isLeanEngine && !isReceiveWO && tobeFiredTrigger.Id != 8 && tobeFiredTrigger.Id != 16 && tobeFiredTrigger.Id != 20) { continue; } BatchJobDetail jobDetail = tobeFiredTrigger.BatchJobDetail; BatchRunLog runLog = new BatchRunLog(); try { #region Job运行前处理 if (isLeanEngine) { lelog.Info("Start run job. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name); } else if (isReceiveWO) { rwolog.Info("Start run job. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name); } else { log.Info("Start run job. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name); } runLog.BatchJobDetail = jobDetail; runLog.BatchTrigger = tobeFiredTrigger; runLog.StartTime = DateTime.Now; runLog.Status = "InProcess"; this.batchRunLogMgr.CreateBatchRunLog(runLog); #endregion #region 运行Job JobDataMap dataMap = new JobDataMap(); #region Job参数获取 IList <BatchJobParameter> batchJobParameterList = this.batchJobParameterMgr.GetBatchJobParameter(jobDetail.Id); if (batchJobParameterList != null && batchJobParameterList.Count > 0) { foreach (BatchJobParameter batchJobParameter in batchJobParameterList) { if (isLeanEngine) { lelog.Debug("Set Job Parameter Name:" + batchJobParameter.ParameterName + ", Value:" + batchJobParameter.ParameterValue); } else if (isReceiveWO) { rwolog.Debug("Set Job Parameter Name:" + batchJobParameter.ParameterName + ", Value:" + batchJobParameter.ParameterValue); } else { log.Debug("Set Job Parameter Name:" + batchJobParameter.ParameterName + ", Value:" + batchJobParameter.ParameterValue); } dataMap.PutData(batchJobParameter.ParameterName, batchJobParameter.ParameterValue); } } #endregion #region Trigger参数获取 IList <BatchTriggerParameter> batchTriggerParameterList = this.batchTriggerParameterMgr.GetBatchTriggerParameter(tobeFiredTrigger.Id); if (batchTriggerParameterList != null && batchTriggerParameterList.Count > 0) { foreach (BatchTriggerParameter batchTriggerParameter in batchTriggerParameterList) { if (isLeanEngine) { lelog.Debug("Set Trigger Parameter Name:" + batchTriggerParameter.ParameterName + ", Value:" + batchTriggerParameter.ParameterValue); } else if (isReceiveWO) { rwolog.Debug("Set Trigger Parameter Name:" + batchTriggerParameter.ParameterName + ", Value:" + batchTriggerParameter.ParameterValue); } else { log.Debug("Set Trigger Parameter Name:" + batchTriggerParameter.ParameterName + ", Value:" + batchTriggerParameter.ParameterValue); } dataMap.PutData(batchTriggerParameter.ParameterName, batchTriggerParameter.ParameterValue); } } #endregion #region 初始化JobRunContext JobRunContext jobRunContext = new JobRunContext(tobeFiredTrigger, jobDetail, dataMap, container); #endregion #region 调用Job IJob job = container.Resolve <IJob>(jobDetail.ServiceName); if (isLeanEngine) { lelog.Debug("Start run job: " + jobDetail.ServiceName); } else if (isReceiveWO) { rwolog.Debug("Start run job: " + jobDetail.ServiceName); } else { log.Debug("Start run job: " + jobDetail.ServiceName); } job.Execute(jobRunContext); #endregion #endregion #region Job运行后处理 if (isLeanEngine) { lelog.Info("Job run successful. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name); } else if (isReceiveWO) { rwolog.Info("Job run successful. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name); } else { log.Info("Job run successful. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name); } runLog.EndTime = DateTime.Now; runLog.Status = "Successful"; this.batchRunLogMgr.UpdateBatchRunLog(runLog); #endregion } catch (Exception ex) { try { this.batchTriggerMgr.CleanSession(); isSuccess = false; if (isLeanEngine) { lelog.Error("Job run failure. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name, ex); } else if (isReceiveWO) { rwolog.Error("Job run failure. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name, ex); } else { log.Error("Job run failure. JobId:" + jobDetail.Id + ", JobName:" + jobDetail.Name, ex); } runLog.EndTime = DateTime.Now; runLog.Status = "Failure"; if (ex.Message != null && ex.Message.Length > 255) { runLog.Message = ex.Message.Substring(0, 255); } else { runLog.Message = ex.Message; } this.batchRunLogMgr.UpdateBatchRunLog(runLog); } catch (Exception ex1) { if (isLeanEngine) { lelog.Error("", ex1); } else if (isReceiveWO) { rwolog.Error("", ex1); } else { log.Error("", ex1); } } } finally { #region 更新BatchTrigger try { BatchTrigger oldTobeFiredTrigger = this.batchTriggerMgr.LoadBatchTrigger(tobeFiredTrigger.Id); oldTobeFiredTrigger.TimesTriggered++; oldTobeFiredTrigger.PreviousFireTime = oldTobeFiredTrigger.NextFireTime; if (oldTobeFiredTrigger.RepeatCount != 0 && oldTobeFiredTrigger.TimesTriggered >= oldTobeFiredTrigger.RepeatCount) { //关闭Trigger if (isLeanEngine) { lelog.Debug("Close Trigger:" + oldTobeFiredTrigger.Name); } else if (isReceiveWO) { rwolog.Debug("Close Trigger:" + oldTobeFiredTrigger.Name); } else { log.Debug("Close Trigger:" + oldTobeFiredTrigger.Name); } oldTobeFiredTrigger.Status = BusinessConstants.CODE_MASTER_STATUS_VALUE_CLOSE; oldTobeFiredTrigger.NextFireTime = null; } else { if (isSuccess) { //设置下次运行时间 if (isLeanEngine) { lelog.Debug("Set Trigger Next Start Time, Add:" + oldTobeFiredTrigger.Interval.ToString() + " " + oldTobeFiredTrigger.IntervalType); } else if (isReceiveWO) { rwolog.Debug("Set Trigger Next Start Time, Add:" + oldTobeFiredTrigger.Interval.ToString() + " " + oldTobeFiredTrigger.IntervalType); } else { log.Debug("Set Trigger Next Start Time, Add:" + oldTobeFiredTrigger.Interval.ToString() + " " + oldTobeFiredTrigger.IntervalType); } DateTime dateTimeNow = DateTime.Now; if (!oldTobeFiredTrigger.NextFireTime.HasValue) { oldTobeFiredTrigger.NextFireTime = dateTimeNow; } while (oldTobeFiredTrigger.NextFireTime.Value <= dateTimeNow) { if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_YEAR) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddYears(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_MONTH) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddMonths(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_DAY) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddDays(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_HOUR) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddHours(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_MINUTE) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddMinutes(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_SECOND) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddSeconds(oldTobeFiredTrigger.Interval); } else if (oldTobeFiredTrigger.IntervalType == BusinessConstants.DATETIME_TYPE_MILLISECOND) { oldTobeFiredTrigger.NextFireTime = oldTobeFiredTrigger.NextFireTime.Value.AddMilliseconds(oldTobeFiredTrigger.Interval); } else { throw new ArgumentException("invalid Interval Type:" + oldTobeFiredTrigger.IntervalType); } } if (isLeanEngine) { lelog.Debug("Trigger Next Start Time is set as:" + oldTobeFiredTrigger.NextFireTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); } else if (isReceiveWO) { rwolog.Debug("Trigger Next Start Time is set as:" + oldTobeFiredTrigger.NextFireTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); } else { log.Debug("Trigger Next Start Time is set as:" + oldTobeFiredTrigger.NextFireTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); } } } this.batchTriggerMgr.UpdateBatchTrigger(oldTobeFiredTrigger); } catch (Exception ex) { if (isLeanEngine) { lelog.Error("Error occur when update batch trigger.", ex); } else if (isReceiveWO) { rwolog.Error("Error occur when update batch trigger.", ex); } else { log.Error("Error occur when update batch trigger.", ex); } } #endregion } } } else { if (isLeanEngine) { lelog.Info("No job found may run in this batch."); } else if (isReceiveWO) { rwolog.Info("No job found may run in this batch."); } else { log.Info("No job found may run in this batch."); } } if (isLeanEngine) { lelog.Info("BatchJobs run end."); } else if (isReceiveWO) { rwolog.Info("BatchJobs run end."); } else { log.Info("BatchJobs run end."); } }