public static ReportExecution GetNextJob() { ReportExecution exec = null; #region logging Logger.TraceEvent( TraceEventType.Verbose, 0, "{0}: BEGIN GetNextJob()", System.Threading.Thread.CurrentThread.ManagedThreadId); #endregion try { using (ReportingDataContext data = new ReportingDataContext()) { //get the next execution from the queue var execRow = (from e in data.GetExecutionFromQueue() where e.ScheduledStartDate.HasValue select e).FirstOrDefault(); if (execRow == null) { return(null); } try { var format = data.GetFormat(execRow.ReportFormatID).FirstOrDefault(); var report = data.GetReport(execRow.ReportID, execRow.EnvironmentID).FirstOrDefault(); var subscription = data.GetSubscription(execRow.ReportSubscriptionID).FirstOrDefault(); exec = GetReportExecutionFromRow(execRow, format, report, subscription); } catch (Exception ex) { //WI #2889: mark the execution for retry on exception data.SetExecutionRetry( execRow.ReportExecutionID, execRow.Name, null, null, ex.ToString(), null, execRow.ErrorCount + 1, execRow.ScheduledStartDate, "SYSTEM", DateTime.Now); //re-throw exception throw; } } } #region logging finally { Logger.TraceEvent( TraceEventType.Verbose, 0, "{0}: END GetNextJob(id={1}, state={2}, run={3}, nextRun={4}, subscription={5}, reportId={6}, path={7}, formatId={8}, format={9})", System.Threading.Thread.CurrentThread.ManagedThreadId, exec != null ? exec.Id : null, exec != null ? (ReportExecutionStateEnum?)exec.State : null, exec != null ? exec.ScheduledRunTime : null, exec != null ? exec.NextScheduledRunTime : null, exec != null ? exec.SubscriptionId : null, exec != null ? (int?)exec.ReportId : null, exec != null ? exec.ReportPath : null, exec != null ? (int?)exec.FormatId : null, exec != null ? exec.Format : null); } #endregion return(exec); }
public static void CompleteExecution(ReportExecution execution) { #region logging execution.Log(TraceEventType.Verbose, "BEGIN CompleteExecution()"); #endregion try { int executionId = int.Parse(execution.Id); if (execution.State != ReportExecutionStateEnum.Failed && execution.State != ReportExecutionStateEnum.Succeeded) { throw new InvalidOperationException(string.Format("Execution {0} cannot be completed from its current state: {1}", executionId, execution.State)); } if (execution.State == ReportExecutionStateEnum.Succeeded && execution.Data == null) { throw new InvalidOperationException(string.Format("Execution {0} is in the 'Succeeded' state but it does not have any data", executionId)); } using (ReportingDataContext content = new ReportingDataContext("Emdat.InVision.ReportContent")) using (ReportingDataContext data = new ReportingDataContext()) { //only update content if the data has been set if (execution.State == ReportExecutionStateEnum.Succeeded && execution.Data != null) { #region logging execution.Log( TraceEventType.Verbose, "Setting execution data (fileType={0}, dataLength={1})", execution.FileType, execution.Data != null ? execution.Data.Length : 0); #endregion try { //save the report data var result = content.SetExecutionData(executionId, execution.FileType, execution.Data); if (result < 1) { throw new ApplicationException(string.Format("The execution data could not be set: {0}", executionId)); } } catch (Exception ex) { //TFS #13667: retry if there are problems saving the report content execution.Log(TraceEventType.Warning, "Exception saving report content: {0}", ex.Message); execution.Log(TraceEventType.Information, "{0}", ex); execution.Error = ex; execution.ErrorCount++; data.SetExecutionRetry( executionId, execution.Name, execution.StartTime, execution.EndTime, execution.ErrorDescription, execution.ErrorCode.HasValue ? (int?)execution.ErrorCode.Value : null, execution.ErrorCount, execution.ScheduledRunTime, "SYSTEM", DateTime.Now); return; } } //only fail if success or the error code is set, otherwise, retry if (execution.State == ReportExecutionStateEnum.Succeeded || execution.ErrorCode.HasValue) { string nextPrmsXml = ReportParametersHelper.GetParameterValuesXml(execution.NextScheduledRunParameters); #region logging execution.Log( TraceEventType.Verbose, "Setting execution status (name={0}, startTime={1:s}, endTime={2:s}, state={3}, errorCode={4}, errorCount={5}, usedHistory={6}, nextRun={7:s}, nextPrms={8}, report={9}, subscription={10}, format={11})", execution.Name, execution.StartTime, execution.EndTime, execution.State, execution.ErrorCode, execution.ErrorCount, !string.IsNullOrEmpty(execution.HistoryId), execution.NextScheduledRunTime, nextPrmsXml, execution.ReportId, execution.SubscriptionId, execution.FormatId); #endregion //update execution state data.SetExecutionStatus( reportExecutionID: executionId, name: execution.Name, startDate: execution.StartTime, endDate: execution.EndTime, reportExecutionStatusID: (int)execution.State, errorDescription: execution.ErrorDescription, errorCount: execution.ErrorCount, reportExecutionErrorID: execution.ErrorCode.HasValue ? (int?)execution.ErrorCode.Value : null, usedHistory: !string.IsNullOrEmpty(execution.HistoryId), nextExecutionDate: execution.NextScheduledRunTime, nextExecutionParameters: nextPrmsXml, nextExecutionReportID: execution.ReportId, nextExecutionSubscriptionID: execution.SubscriptionId, nextExecutionFormatID: execution.FormatId, modifiedUser: "******", modifiedDate: DateTime.Now); } else { #region logging execution.Log( TraceEventType.Verbose, "Setting execution retry (name={0}, startTime={1:s}, endTime={2:s}, state={3}, errorCode={4}, errorCount={5}, run={6})", execution.Name, execution.StartTime, execution.EndTime, execution.State, execution.ErrorCode, execution.ErrorCount, execution.ScheduledRunTime); #endregion //retry data.SetExecutionRetry( executionId, execution.Name, execution.StartTime, execution.EndTime, execution.ErrorDescription, execution.ErrorCode.HasValue ? (int?)execution.ErrorCode.Value : null, execution.ErrorCount, execution.ScheduledRunTime, "SYSTEM", DateTime.Now); } } } #region logging finally { execution.Log(TraceEventType.Verbose, "END CompleteExecution()"); } #endregion }