public async Task InsertJobHistoryEntry(IJobExecutionContext context, JobExecutionException jobException) { var sql = AdoJobStoreUtil.ReplaceTablePrefix(SqlInsertJobExecuted, tablePrefix, null); using (var connection = GetConnection(IsolationLevel.ReadUncommitted)) { using (var command = Delegate.PrepareCommand(connection, sql)) { Delegate.AddCommandParameter(command, "schedulerName", context.Scheduler.SchedulerName); Delegate.AddCommandParameter(command, "instanceName", context.Scheduler.SchedulerInstanceId); Delegate.AddCommandParameter(command, "jobName", context.JobDetail.Key.Name); Delegate.AddCommandParameter(command, "jobGroup", context.JobDetail.Key.Group); Delegate.AddCommandParameter(command, "triggerName", context.Trigger.Key.Name); Delegate.AddCommandParameter(command, "triggerGroup", context.Trigger.Key.Group); Delegate.AddCommandParameter(command, "scheduledTime", Delegate.GetDbDateTimeValue(context.ScheduledFireTimeUtc)); Delegate.AddCommandParameter(command, "firedTime", Delegate.GetDbDateTimeValue(context.FireTimeUtc)); Delegate.AddCommandParameter(command, "runTime", Delegate.GetDbTimeSpanValue(context.JobRunTime)); Delegate.AddCommandParameter(command, "error", Delegate.GetDbBooleanValue(jobException != null)); Delegate.AddCommandParameter(command, "errorMessage", jobException?.ToString()); await command.ExecuteNonQueryAsync().ConfigureAwait(false); connection.Commit(); } } }
public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException) { TaskLogUtil log = new TaskLogUtil(); log.TaskID = context.JobDetail.Key.Name; log.RunTime = DateTime.Now; log.IsSuccess = 1; string result = Convert.ToString(context.Result); if (!String.IsNullOrWhiteSpace(result)) { log.IsSuccess = 0; log.Result = result; } else { log.Result = "执行完成"; } if (jobException != null) { log.IsSuccess = 0; if (jobException.InnerException != null) { log.Result += jobException.ToString(); } } TaskHelper.SaveTaskLog(log); }
public override async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default) { string group = context.JobDetail.Key.Group; string name = context.JobDetail.Key.Name; DateTime fireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.FireTimeUtc.DateTime, TimeZoneInfo.Local); DateTime?nextFireTimeUtc = null; if (context.NextFireTimeUtc != null) { nextFireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.NextFireTimeUtc.Value.DateTime, TimeZoneInfo.Local); } if (!JobHelper.IsBaseJob(group, name)) { //更新任务运行情况 await _jobRepository.UpdateExecuteAsync(group, name, fireTimeUtc, nextFireTimeUtc); //记录运行日志 double totalSeconds = context.JobRunTime.TotalSeconds; bool succ = true; string exception = string.Empty; if (jobException != null) { succ = false; exception = jobException.ToString(); } JobRunLog log = new JobRunLog(group, name, totalSeconds, fireTimeUtc, succ, exception); await _jobRunLogRepository.InsertAsync(log); } }
public void AddExecutionList( IJobExecutionContext context, JobExecutionException exception) { if (ExecutionList == null) { ExecutionList = new List <SingleJobExecutionStatus>(); } if (ExecutionList.Count >= 20) { ExecutionList = ExecutionList .Take(19) .ToList(); } SingleJobExecutionStatus status = new SingleJobExecutionStatus() { ExecutionStatus = exception == null, ExecutionTimestamp = DateTime.UtcNow, }; if (context.Result != null) { status.Message = context.Result.ToString(); } if (exception != null) { status.ExceptionMessage = exception.ToString(); } ExecutionList.Insert(0, status); }
public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default) { _stopwatches[context.FireInstanceId].Stop(); var endDocument = new JobDataEndDocument { JobId = context.FireInstanceId, JobType = context.JobDetail.JobType.Name, JobName = context.JobDetail.Key.Name, EndDate = DateTime.Now, ElapsedMilliseconds = _stopwatches[context.FireInstanceId].ElapsedMilliseconds }; await jobDataSource.Finish(endDocument); if (jobException != null) { var logDocument = new JobDataLogDocument { JobId = context.FireInstanceId, Level = "Error", JobName = context.JobDetail.Key.Name, Description = jobException.ToString(), CreatedDate = DateTime.Now }; await jobDataSource.Log(logDocument); await RetryJob(context); } _stopwatches.Remove(context.FireInstanceId); }
/// <summary> /// 准备监听器执行完毕 /// </summary> /// <param name="context"></param> /// <param name="jobException"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = new CancellationToken()) { if (jobException == null) { } else { this.Logger(this.GetType(), "JobWasExecuted:发生异常:" + jobException.ToString(), LoggerLevel.Error); } return(Task.FromResult(0)); }
/// <summary> /// 运行成功以后 /// </summary> /// <param name="context"></param> /// <param name="jobException"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default) { //Job执行完成 return(Task.Factory.StartNew(() => { DateTime NextFireTimeUtc = context.Trigger.GetNextFireTimeUtc().HasValue ? context.Trigger.GetNextFireTimeUtc().Value.LocalDateTime : default(DateTime); DateTime FireTimeUtc = context.Trigger.GetPreviousFireTimeUtc().HasValue ? context.Trigger.GetPreviousFireTimeUtc().Value.LocalDateTime : default(DateTime); double TotalSeconds = context.JobRunTime.TotalSeconds; string JobName = context.JobDetail.Key.Name; string LogContent = $"jobName:{JobName},Job: {context.JobDetail.Key} 执行完成,Name={Name},JobWasExecuted"; int joibId = 0; if (context.MergedJobDataMap != null) { // JobName = context.MergedJobDataMap.GetString("JobName"); System.Text.StringBuilder log = new System.Text.StringBuilder(); int i = 0; foreach (var item in context.MergedJobDataMap) { string key = item.Key; if (key == MangertKey.JobDataMapKeyJobId) { joibId = Convert.ToInt32(item.Value); } if (key.StartsWith("extend_")) { if (i > 0) { log.Append(","); } log.AppendFormat("{0}:{1}", item.Key, item.Value); i++; } } if (i > 0) { LogContent = string.Concat("[", log.ToString(), "]"); } } if (jobException != null) { LogContent = LogContent + " EX:" + jobException.ToString(); } Singleton <MangerLog> .Instance.Infor(LogContent); Console.WriteLine(LogContent); _logmsg.Insert(new model.quarzt.logMsg { JobName = JobName, Msg = LogContent }); _quatzjobRepostory.UpdateRunTime(joibId); })); }
/// <summary> /// Called by the Scheduler after a <see cref="IJobDetail" /> /// has been executed, and be for the associated <see cref="ITrigger" />'s /// <see cref="IOperableTrigger.Triggered" /> method has been called. /// </summary> /// <param name="context"></param> /// <param name="jobException"></param> public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException) { Logger.InfoFormat("JobWasExecuted: {0}, {1}", context.JobDetail.Key.Name, context.JobDetail.Key.Group); var auditLog = GetAuditLog("JobWasExecuted", context); if (null != jobException) { auditLog.ExecutionException = jobException.ToString(); } _persistanceStore.InsertAuditLog(auditLog); }
public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken token) { if (jobException != null) { // Log/handle error here _logger.LogError($"Job Errored : {context.JobDetail.Description} - {jobException.ToString()}"); } else { _logger.LogInformation($"Job Executed : {context.JobDetail.Description} ({context.JobDetail.Key}) Result ({context.Result ?? "null"}) Next run at {context.NextFireTimeUtc}"); } return(Task.CompletedTask); }
public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException) { string BackgroundJobId = GuidUtil.New(); BackgroundJobId = context.JobDetail.Key.Name; //string.TryParse(context.JobDetail.Key.Name, out BackgroundJobId); DateTime NextFireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.NextFireTimeUtc.Value.DateTime, TimeZoneInfo.Local); DateTime FireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.FireTimeUtc.Value.DateTime, TimeZoneInfo.Local); decimal TotalSeconds = decimal.Parse(context.JobRunTime.TotalSeconds.ToString()); string JobName = string.Empty; string LogContent = string.Empty; if (context.MergedJobDataMap != null) { JobName = context.MergedJobDataMap.GetString("JobName"); System.Text.StringBuilder log = new System.Text.StringBuilder(); int i = 0; foreach (var item in context.MergedJobDataMap) { string key = item.Key; if (key.StartsWith("extend_")) { if (i > 0) { log.Append(","); } log.AppendFormat("{0}:{1}", item.Key, item.Value); i++; } } if (i > 0) { LogContent = string.Concat("[", log.ToString(), "]"); } } if (jobException != null) { LogContent = LogContent + " EX:" + jobException.ToString(); } AdminServiceFactory.BackgroundJob.UpdateInfo(GuidUtil.New(), BackgroundJobId, JobName, FireTimeUtc, NextFireTimeUtc, TotalSeconds, LogContent); }
public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken)) { try { _logger.Debug($"SchedulerJobListener.JobWasExecuted: {context.JobDetail.Key.Name}"); var taskId = int.Parse(context.JobDetail.Key.Name); DateTime nextFireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.NextFireTimeUtc.Value.DateTime, TimeZoneInfo.Local); DateTime fireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.FireTimeUtc.UtcDateTime, TimeZoneInfo.Local); double duration = context.JobRunTime.TotalSeconds; string jobName = string.Empty; string executedResult = string.Empty; if (context.MergedJobDataMap != null) { _logger.Debug($"SchedulerJobListener.JobWasExecuted context.MergedJobDataMap: {context.MergedJobDataMap.Count}"); jobName = context.MergedJobDataMap.GetString("JobName"); _logger.Debug($"JobWasExecuted, jobName=\"{jobName}\""); executedResult = context.MergedJobDataMap.FirstOrDefault(a => a.Key == "executedResult").Value as string; _logger.Debug($"SchedulerJobListener.JobWasExecuted: {executedResult ?? "null"}"); } if (jobException != null) { _logger.Debug($"SchedulerJobListener.JobWasExecuted.jobException: {jobException.ToString()}"); executedResult = executedResult + " EX:" + jobException.ToString(); } if (!string.IsNullOrEmpty(executedResult)) { executedResult = executedResult.Length > 1000 ? executedResult.Substring(0, 1000) : executedResult; } else { executedResult = "job执行失败。。。"; } new ScheduledTaskService().UpdateScheduledTaskStatus(taskId, jobName, fireTimeUtc, nextFireTimeUtc, duration, executedResult); _logger.Debug($"SchedulerJobListener.JobWasExecuted: all done"); } catch (Exception ex) { _logger.Error(ex.Message); } return(Task.FromResult(true)); }
public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken)) { //System.Guid BackgroundJobId = System.Guid.Empty; //Guid.TryParse(context.JobDetail.Key.Name, out BackgroundJobId); long jobId = Convert.ToInt64(context.JobDetail.Key.Name); DateTime NextFireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.NextFireTimeUtc.Value.DateTime, TimeZoneInfo.Local); DateTime FireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.FireTimeUtc.DateTime, TimeZoneInfo.Local); double TotalSeconds = context.JobRunTime.TotalSeconds; string JobName = string.Empty; string LogContent = string.Empty; if (context.MergedJobDataMap != null) { JobName = context.MergedJobDataMap.GetString("JobName"); StringBuilder log = new StringBuilder(); int i = 0; foreach (var item in context.MergedJobDataMap) { string key = item.Key; if (key.StartsWith("extend_")) { if (i > 0) { log.Append(","); } log.AppendFormat("{0}:{1}", item.Key, item.Value); i++; } } if (i > 0) { LogContent = string.Concat("[", log.ToString(), "]"); } } if (jobException != null) { LogContent = LogContent + " EX:" + jobException.ToString(); } //new BackgroundJobService().UpdateBackgroundJobStatus(BackgroundJobId, JobName, FireTimeUtc, NextFireTimeUtc, TotalSeconds, LogContent); JobService.UpdateJobStatus(jobId, FireTimeUtc, NextFireTimeUtc, TotalSeconds, LogContent); return(Task.CompletedTask); }
public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException) { System.Guid BackgroundJobId = System.Guid.Empty; Guid.TryParse(context.JobDetail.Key.Name, out BackgroundJobId); DateTime NextFireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.NextFireTimeUtc.Value.DateTime, TimeZoneInfo.Local); DateTime FireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.FireTimeUtc.Value.DateTime, TimeZoneInfo.Local); double TotalSeconds = context.JobRunTime.TotalSeconds; string JobName = string.Empty; if (context.MergedJobDataMap != null) { JobName = context.MergedJobDataMap.GetString("JobName"); } string LogContent = string.Empty; if (jobException != null) { LogContent = jobException.ToString(); } new BackgroundJobService().UpdateBackgroundJobStatus(BackgroundJobId, JobName, FireTimeUtc, NextFireTimeUtc, TotalSeconds, LogContent); }
public override void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException) { var description = "Job was executed: " + Describe(context); if (jobException != null) { description += string.Format("<br/>with exception: <pre>{0}</pre>", HttpUtility.HtmlEncode(jobException.ToString())); } entries.Add(new LogEntry(description)); }
/// <summary> ///3 任务监听,任务执行完成 /// </summary> /// <param name="context"></param> /// <param name="jobException"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken)) { DateTime NextFireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.NextFireTimeUtc.Value.DateTime, TimeZoneInfo.Local); DateTime FireTimeUtc = TimeZoneInfo.ConvertTimeFromUtc(context.FireTimeUtc.DateTime, TimeZoneInfo.Local); double TotalSeconds = context.JobRunTime.TotalSeconds; DateTime endTimeUtc = context.Trigger.EndTimeUtc.Value.DateTime; string JobNames = context.JobDetail.Key.Name; Guid jobId = new Guid(JobNames.Split('|')[0]); string JobName = JobNames.Split('|')[1]; string LogContent = string.Empty; if (context.MergedJobDataMap != null) { StringBuilder log = new StringBuilder(); int i = 0; foreach (var item in context.MergedJobDataMap) { string key = item.Key; if (key.StartsWith("extend_")) { if (i > 0) { log.Append(","); } log.AppendFormat("{0}:{1}", item.Key, item.Value); i++; } } if (i > 0) { LogContent = string.Concat("[", log.ToString(), "]"); } } if (jobException != null) { LogContent = LogContent + " EX:" + jobException.ToString(); } LogUtil.Debug(string.Format("[{0}={2}]任务监听,name:{1}|任务执行完成。", NextFireTimeUtc, jobId, endTimeUtc)); //var timesOfLoop = 10; //休眠毫秒 //Thread.Sleep(timesOfLoop); ////更新运行次数 //var ents = await _baseDomain.GetEntity(jobId); //ents.RunCount = ents.RunCount + 1; //ents.UpdateTime = DateTime.Now; //await _baseDomain.EditDomain(ents); //////记录日志 //await _baseDomainLog.AddDomain(new TaskJobLog() //{ // ExecutionTime = FireTimeUtc, // ExecutionDuration = TotalSeconds, // ID = Guid.NewGuid(), // JobName = JobName, // RunLog = LogContent, // TaskJobId = jobId //}); //// 任务已经结束 更新状态 //if (endTimeUtc.ToString() == NextFireTimeUtc.ToString()) //{ // var ent = await _baseDomain.GetEntity(jobId); // ent.TaskState = TaskState.Finsh.ToInt(); // ent.UpdateTime = DateTime.Now; // await _baseDomain.EditDomain(ent); //} }