示例#1
0
        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
        }