public DateTime GetLastRunDate(IPeriodicallyService periodicallyService, string jobKey) { DateTime lastSuccessExecDt; lastSuccessExecDt = IgnoreSecurity() .GetDefaultQuery() .Where(x => x.JobName == jobKey && x.Status == Constants.JobLogStates.Success) .OrderByDescending(x => x.RunDt) .Select(x => x.RunDt) .FirstOrDefault(); if (lastSuccessExecDt == DateTime.MinValue) { lastSuccessExecDt = periodicallyService.GetBeginingExecutionTime(); } return(lastSuccessExecDt); }
public bool ExecuteJob(IPeriodicallyService periodicallyService, string jobKey, DateTime from, DateTime to) { _logger.InfoFormat("run job {0} from {0} to {1}", jobKey, from, to); EnableCommandLog(); if (from == to || (DateTime.Now - from).TotalHours < 1 || to < from) { _logger.InfoFormat("job {0} cannot run for date from {1} to {2} because invalid date!", jobKey, from, to); return(false); } _logger.InfoFormat("last success executed date for job {0} is {1}", jobKey, from); _logger.InfoFormat("start run job {0}", jobKey); Stopwatch watch = new Stopwatch(); watch.Start(); var jobLog = SaveJob(jobKey, to); try { periodicallyService.ExecutePeriodicallyJob(from, to); jobLog.Status = Constants.JobLogStates.Success; _logger.InfoFormat("job {0} executed for date {1} successfuly.", jobKey, from); } catch (Exception ex) { _logger.ErrorFormat("exception occurred on executing job {0} on day {1}", ex, jobKey, from); jobLog.Status = Constants.JobLogStates.Error; ZipJobException(jobKey, ex, jobLog, from); } watch.Stop(); jobLog.Duration = TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds); SaveJob(jobKey, from, jobLog); return(jobLog.Status == Constants.JobLogStates.Success); }
public bool RunFrom(IPeriodicallyService periodicallyService, string jobKey, DateTime from) { _logger.InfoFormat("RunForm jobKey:{0}, from:{1})", jobKey, from); DateTime lastSuccessExecDt; if (from != DateTime.MinValue) { lastSuccessExecDt = from; } else { lastSuccessExecDt = GetLastRunDate(periodicallyService, jobKey); } if (periodicallyService.GetPeriod() >= TimeSpan.FromDays(1)) { lastSuccessExecDt = lastSuccessExecDt.Date + periodicallyService.GetPeriodicallyExecutionTimeOfDay(); } DateTime untilExecDt = (periodicallyService.IsContainCurrentDate()) ? DateTime.Now : DateTime.Now.AddDays(-1); var intervals = (untilExecDt - lastSuccessExecDt).TotalMinutes / periodicallyService.GetPeriod().TotalMinutes; DateTime to = lastSuccessExecDt; from = lastSuccessExecDt; for (int i = 0; i < Math.Floor(intervals); ++i) { to = to + periodicallyService.GetPeriod(); var result = ExecuteJob(periodicallyService, jobKey, from, to); from = to; if (!result) { return(false); } } return(true); }