public async Task <object> RetryParticipationSyncImmediately( CrmData data, Configurations requestWideSettings, bool requestConsumerId = false) { var jobId = await Task.Run(() => BackgroundJob.Enqueue <ICrmConsumerProvider>( iCrmProvider => iCrmProvider.CreateParticipationAsync( data, requestWideSettings, requestConsumerId))); var result = new object(); IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi(); JobDetailsDto jobDetails = monitoringApi.JobDetails(jobId); SucceededJobDto jobDto = monitoringApi.SucceededJobs(0, int.MaxValue) .First() //.FirstOrDefault(job => job.Key == "Key") .Value; if (jobDto != null) { result = jobDto.Result; return(JsonConvert.DeserializeObject <CrmResponse>(result.ToString())); } return(null); }
/// <summary> /// Starts a new job and waits for its result /// </summary> /// <typeparam name="TResult"></typeparam> /// <typeparam name="TJob"></typeparam> /// <param name="methodCall"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <TResult> StartWaitAsync <TResult, TJob>([InstantHandle, NotNull] Expression <Func <TJob, Task> > methodCall, CancellationToken cancellationToken = default) { //todo find a way to mark this job as a Continuation when using backgroundJobClient.Enqueue var jobId = backgroundJobClient.Enqueue(methodCall); while (true) { cancellationToken.ThrowIfCancellationRequested(); var jobDetails = monitoringApi.JobDetails(jobId); string currentState = jobDetails.History[0].StateName; if (!runningStates.Contains(currentState)) { if (currentState == SucceededState.StateName) { return(GetResult <TResult>(jobId)); } else if (currentState == FailedState.StateName) { return(ThrowError <TResult>(jobId)); } else { throw new InvalidOperationException($"The job must be in the state '{SucceededState.StateName}' or '{FailedState.StateName}' but is in '{currentState}'"); } } await Task.Delay(100, cancellationToken); } }
private async Task WaitUntilJobCompletedAsync(string jobId, int maxWaitInMilliseconds = 5000) { IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi(); var sw = Stopwatch.StartNew(); JobDetailsDto jobDetails = null; while ((jobDetails == null || jobDetails.History.All(s => s.StateName != "Succeeded")) && (sw.Elapsed.TotalMilliseconds < maxWaitInMilliseconds || Debugger.IsAttached)) { await Task.Delay(25); jobDetails = monitoringApi.JobDetails(jobId); if (monitoringApi.FailedCount() > 0) { break; } } FailedJobDto failedJob = monitoringApi .FailedJobs(0, int.MaxValue) .Select(j => j.Value) .FirstOrDefault(); if (failedJob != null) { throw new InvalidOperationException($"Job failed: {failedJob.ExceptionDetails}."); } _client.Delete(jobId); }
/// <summary> /// Returns the status of the job with the given ID. /// </summary> public static Task <JobStatus> GetJobStatusAsync(IMonitoringApi monitoringApi, string jobId) { // TODO: Make this actually asynchronous once HangFire supports it. var jobDetails = monitoringApi.JobDetails(jobId) .History .OrderByDescending(h => h.CreatedAt) .FirstOrDefault(); var jobState = GetJobState(jobDetails); var enteredState = jobDetails?.CreatedAt ?? DateTime.MinValue; return(Task.FromResult(new JobStatus(jobState, enteredState))); }
/// <summary> /// Returns the status of the job with the given ID. /// </summary> public static Task <JobStatus> GetJobStatusAsync(IMonitoringApi monitoringApi, string jobId) { var jobDetails = monitoringApi.JobDetails(jobId)? .History .OrderByDescending(h => h.CreatedAt) .FirstOrDefault(); if (jobDetails == null) { return(Task.FromResult(new JobStatus(JobState.NotFound, DateTime.MinValue))); } var jobState = GetJobState(jobDetails); return(Task.FromResult(new JobStatus(jobState, jobDetails.CreatedAt))); }
public JobStatus Status(string id) { IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi(); var details = monitoringApi.JobDetails(id); var status = new JobStatus() { status = details.History[0].StateName, }; if (details.History[0].Data.ContainsKey("Result")) { status.result = details.History[0].Data["Result"]; } return(status); }
public HttpResponseMessage Get(Guid id) { //2 design options-- query the queue or query the side effect of the action. IMonitoringApi monitor = JobStorage.Current.GetMonitoringApi(); JobDetailsDto dto = monitor.JobDetails(QueueMap[id]); //if (QueueResults.ContainsKey(id) && QueueResults[id]!=null && QueueResults[id].Url!="") if (dto.History.Any(x => x.StateName == "Succeeded")) { HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.SeeOther); message.Headers.Location = new Uri(QueueResults[id].Url); return(message); } else { HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK); message.Content = new StringContent("Not ready yet, try again in a few seconds."); return(message); } }