Beispiel #1
0
        private JobStatusResult WaitForExpectedStatus(string jobId, string expectedStatus, int maxSeconds = 60)
        {
            //Get the job status...
            JobStatusResult statusResult = new JobStatusResult {
                Status = string.Empty
            };
            //Avoid infinite loop if something goes wrong
            var timeout = DateTime.Now.AddSeconds(maxSeconds);

            while (!statusResult.Status.Equals(expectedStatus, StringComparison.OrdinalIgnoreCase) && DateTime.Now < timeout)
            {
                var responseJson = ts.CallSchedulerWebApi($"api/v1/export/{jobId}", "GET");
                statusResult = JsonConvert.DeserializeObject <JobStatusResult>(responseJson,
                                                                               new JsonSerializerSettings {
                    DateTimeZoneHandling = DateTimeZoneHandling.Unspecified
                });

                Assert.IsNotNull(statusResult, "Should get a job status response");
                Console.WriteLine($"Scheduled Job Status: {statusResult.Status}");
                Thread.Sleep(5000);
            }
            if (!statusResult.Status.Equals(expectedStatus, StringComparison.OrdinalIgnoreCase))
            {
                Assert.Fail("Test timed out");
            }
            return(statusResult);
        }
Beispiel #2
0
        public JobStatusResult GetJobStatus(string jobId)
        {
            log.LogInformation($"GetJobStatus: jobId={jobId}");

            var jobData = JobStorage.Current.GetConnection()?.GetJobData(jobId);
            var status  = jobData?.State;

            if (string.IsNullOrEmpty(status))
            {
                throw new ServiceException(HttpStatusCode.BadRequest,
                                           new ContractExecutionResult(ContractExecutionStatesEnum.ValidationError,
                                                                       $"Missing job details for {jobId}"));
            }

            log.LogInformation($"GetJobStatus: {jobId} status={status}");
            string         key          = null;
            string         downloadLink = null;
            FailureDetails details      = null;

            if (status.Equals(Hangfire.States.SucceededState.StateName, StringComparison.OrdinalIgnoreCase))
            {
                if (Request.Path.Value.Contains("export"))
                {
                    // Attempt to get the download link that should have been set in the job
                    key          = JobStorage.Current.GetConnection().GetJobParameter(jobId, ExportJob.S3_KEY_STATE_KEY);
                    downloadLink = JobStorage.Current.GetConnection().GetJobParameter(jobId, ExportJob.DOWNLOAD_LINK_STATE_KEY);
                    log.LogInformation($"Getting export job {jobId} downloadLink={downloadLink}");

                    if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(downloadLink))
                    {
                        log.LogWarning("S3Key or Downloadlink not set in background job, attempting to find it via the original request");
                        var filename = (jobData?.Job.Args[0] as ScheduleJobRequest).Filename ?? jobId;
                        key          = ExportJob.GetS3Key(jobId, filename);
                        downloadLink = exportJob.GetDownloadLink(jobId, filename);
                    }
                }
            }
            else if (status.Equals(Hangfire.States.DeletedState.StateName, StringComparison.OrdinalIgnoreCase))
            {
                var detailsJson = JobStorage.Current.GetConnection().GetJobParameter(jobId, ExportFailedState.EXPORT_DETAILS_KEY);
                log.LogDebug($"GetJobStatus: detailsJson={detailsJson}");
                if (!string.IsNullOrEmpty(detailsJson))
                {
                    details = JsonConvert.DeserializeObject <FailureDetails>(detailsJson);
                }
            }

            // Change behavior so it's not a breaking change for the UI.

            if (details != null &&
                status.Equals(Hangfire.States.DeletedState.StateName, StringComparison.OrdinalIgnoreCase) &&
                details.Result.Code != ContractExecutionStatesEnum.ExecutedSuccessfully)
            {
                status = Hangfire.States.FailedState.StateName;
            }

            var result = new JobStatusResult {
                Key = key, Status = status, DownloadLink = downloadLink, FailureDetails = details
            };

            log.LogInformation($"GetJobStatus: result={JsonConvert.SerializeObject(result)}");
            return(result);
        }
Beispiel #3
0
        public JobRequest ExecuteCallback(object parameter, object context)
        {
            var parentJob      = (string)parameter;
            var currentContext = (PerformContext)context;

            var jobData = JobStorage.Current.GetConnection()?.GetJobData(parentJob);
            var status  = jobData?.State;

            if (status == null)
            {
                throw new NullReferenceException("JobData.State cannot be null");
            }

            var details = new FailureDetails {
                Code = System.Net.HttpStatusCode.OK, Result = new ContractExecutionResult()
            };

            if (status.Equals(Hangfire.States.DeletedState.StateName, StringComparison.OrdinalIgnoreCase))
            {
                var detailsJson = JobStorage.Current.GetConnection().GetJobParameter(parentJob, ExportFailedState.EXPORT_DETAILS_KEY);

                log.LogDebug($"ExportEmailGenerator.ExecuteCallback: detailsJson={detailsJson}");

                if (!string.IsNullOrEmpty(detailsJson))
                {
                    details = JsonConvert.DeserializeObject <FailureDetails>(detailsJson);
                }
            }

            try
            {
                var key          = "";
                var downloadLink = "";

                if (details.Result.Code == ContractExecutionStatesEnum.ExecutedSuccessfully)
                {
                    key          = JobStorage.Current.GetConnection().GetJobParameter(parentJob, ExportJob.ExportJob.S3_KEY_STATE_KEY);
                    downloadLink = JobStorage.Current.GetConnection().GetJobParameter(parentJob, ExportJob.ExportJob.DOWNLOAD_LINK_STATE_KEY);
                }

                var projectName = currentContext.GetJobParameter <string>(Tags.PROJECTNAME_TAG);
                var recipients  = currentContext.GetJobParameter <string[]>(Tags.RECIPIENTS_TAG);

                var result = new JobStatusResult {
                    Key = key, Status = status, DownloadLink = downloadLink, FailureDetails = details
                };
                var emailParameters = new EmailModel {
                    FromName = "*****@*****.**", To = recipients, Subject = $"Roller Report {projectName} {DateTime.UtcNow:yyyy-MM-ddTHH-mm}"
                };

                if (string.IsNullOrEmpty(downloadLink))
                {
                    result.Status = Hangfire.States.FailedState.StateName;
                }

                emailParameters.SetContent(JsonConvert.SerializeObject(result));

                log.LogDebug($"Getting ready to send email { JsonConvert.SerializeObject(emailParameters) }");

                return(new JobRequest {
                    JobUid = Guid.Parse("7c2fc23d-ca84-490d-9240-8e2e622c2470"), SetupParameters = null, RunParameters = emailParameters
                });
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Failed to prepare email message");
                throw;
            }
        }