protected override Task <JobResult> RunInternalAsync(CancellationToken token) { QueueEntry <T> queueEntry = null; try { queueEntry = _queue.Dequeue(); } catch (Exception ex) { if (!(ex is TimeoutException)) { Log.Error().Exception(ex).Message("Error trying to dequeue message: {0}", ex.Message).Write(); return(Task.FromResult(JobResult.FromException(ex))); } } if (queueEntry == null) { return(Task.FromResult(JobResult.Success)); } var lockValue = GetQueueItemLock(queueEntry); if (lockValue == null) { Log.Warn().Message("Unable to acquire lock for queue entry '{0}'.", queueEntry.Id).Write(); return(Task.FromResult(JobResult.FailedWithMessage("Unable to acquire lock for queue entry."))); } Log.Trace().Message("Processing queue entry '{0}'.", queueEntry.Id).Write(); using (lockValue) return(ProcessQueueItem(queueEntry)); }
private async Task <JobResult> TryRunAsync(JobRunContext context) { try { return(await RunInternalAsync(context).AnyContext()); } catch (Exception ex) { return(JobResult.FromException(ex)); } }
public static async Task <JobResult> TryRunAsync(this IJob job, CancellationToken cancellationToken = default(CancellationToken)) { try { return(await job.RunAsync(cancellationToken).AnyContext()); } catch (Exception ex) { return(JobResult.FromException(ex)); } }
public static JobResult TryRun(this IJob job, CancellationToken cancellationToken = default(CancellationToken)) { try { return(job.RunAsync(cancellationToken).GetAwaiter().GetResult()); } catch (Exception ex) { return(JobResult.FromException(ex)); } }
private Task <JobResult> TryRunAsync(CancellationToken token) { try { return(RunInternalAsync(token)); } catch (Exception ex) { return(Task.FromResult(JobResult.FromException(ex))); } }
protected override async Task <JobResult> RunInternalAsync(JobRunContext context) { var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(context.CancellationToken, TimeSpan.FromSeconds(30).ToCancellationToken()); QueueEntry <T> queueEntry; try { queueEntry = await _queue.DequeueAsync(linkedCancellationToken.Token).AnyContext(); } catch (Exception ex) { return(JobResult.FromException(ex, $"Error trying to dequeue message: {ex.Message}")); } if (queueEntry == null) { return(JobResult.Success); } if (context.CancellationToken.IsCancellationRequested) { Logger.Info().Message($"Job was cancelled. Abandoning queue item: {queueEntry.Id}").Write(); await queueEntry.AbandonAsync().AnyContext(); return(JobResult.Cancelled); } using (var lockValue = await GetQueueEntryLockAsync(queueEntry, context.CancellationToken).AnyContext()) { if (lockValue == null) { return(JobResult.SuccessWithMessage("Unable to acquire queue item lock.")); } #if DEBUG Logger.Trace().Message($"Processing queue entry '{queueEntry.Id}'.").Write(); #endif try { var result = await ProcessQueueEntryAsync(new JobQueueEntryContext <T>(queueEntry, lockValue, context.CancellationToken)).AnyContext(); if (!AutoComplete) { return(result); } if (result.IsSuccess) { await queueEntry.CompleteAsync().AnyContext(); } else { await queueEntry.AbandonAsync().AnyContext(); } return(result); } catch { await queueEntry.AbandonAsync().AnyContext(); throw; } } }
protected async override Task <JobResult> ProcessQueueItem(QueueEntry <WorkItemData> queueEntry) { var workItemDataType = Type.GetType(queueEntry.Value.Type); if (workItemDataType == null) { return(JobResult.FailedWithMessage("Could not resolve work item data type.")); } object workItemData; try { workItemData = _queue.Serializer.Deserialize(queueEntry.Value.Data, workItemDataType); } catch (Exception ex) { return(JobResult.FromException(ex, "Failed to parse work item data.")); } var handler = _handlers.GetHandler(workItemDataType); if (handler == null) { return(JobResult.FailedWithMessage("Handler for type {0} not registered.", workItemDataType.Name)); } var progressCallback = new Action <int, string>((progress, message) => _messageBus.Publish(new WorkItemStatus { WorkItemId = queueEntry.Value.WorkItemId, Progress = progress, Message = message, Type = queueEntry.Value.Type })); if (queueEntry.Value.SendProgressReports) { _messageBus.Publish(new WorkItemStatus { WorkItemId = queueEntry.Value.WorkItemId, Progress = 0, Type = queueEntry.Value.Type }); } try { await handler.HandleItem(new WorkItemContext(workItemData, progressCallback)); } catch (Exception ex) { return(JobResult.FromException(ex, "Error in handler {0}.", workItemDataType.Name)); } queueEntry.Complete(); if (queueEntry.Value.SendProgressReports) { _messageBus.Publish(new WorkItemStatus { WorkItemId = queueEntry.Value.WorkItemId, Progress = 100, Type = queueEntry.Value.Type }); } return(JobResult.Success); }
public async Task <JobResult> RunAsync(CancellationToken cancellationToken = default(CancellationToken)) { var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, TimeSpan.FromSeconds(30).ToCancellationToken()); IQueueEntry <WorkItemData> queueEntry; try { queueEntry = await _queue.DequeueAsync(linkedCancellationTokenSource.Token).AnyContext(); } catch (Exception ex) { return(JobResult.FromException(ex, $"Error trying to dequeue work item: {ex.Message}")); } return(await ProcessAsync(queueEntry, cancellationToken).AnyContext()); }
public async Task <JobResult> RunAsync(CancellationToken cancellationToken = default) { IQueueEntry <WorkItemData> queueEntry; using (var timeoutCancellationTokenSource = new CancellationTokenSource(30000)) using (var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCancellationTokenSource.Token)) { try { queueEntry = await _queue.DequeueAsync(linkedCancellationTokenSource.Token).AnyContext(); } catch (Exception ex) { return(JobResult.FromException(ex, $"Error trying to dequeue work item: {ex.Message}")); } } return(await ProcessAsync(queueEntry, cancellationToken).AnyContext()); }
public async Task <JobResult> RunAsync(CancellationToken cancellationToken = default(CancellationToken)) { var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, TimeSpan.FromSeconds(30).ToCancellationToken()); IQueueEntry <WorkItemData> queueEntry; try { queueEntry = await _queue.DequeueAsync(linkedCancellationTokenSource.Token).AnyContext(); } catch (Exception ex) { return(JobResult.FromException(ex, $"Error trying to dequeue work item: {ex.Message}")); } if (queueEntry == null) { return(JobResult.Success); } if (cancellationToken.IsCancellationRequested) { _logger.Info("Job was cancelled. Abandoning {queueEntryType} work item: {queueEntryId}", queueEntry.Value.Type, queueEntry.Id); await queueEntry.AbandonAsync().AnyContext(); return(JobResult.CancelledWithMessage($"Abandoning {queueEntry.Value.Type} work item: {queueEntry.Id}")); } Type workItemDataType = null; try { workItemDataType = Type.GetType(queueEntry.Value.Type); } catch (Exception ex) { await queueEntry.AbandonAsync().AnyContext(); return(JobResult.FromException(ex, $"Could not resolve {workItemDataType.Name} work item data type.")); } if (workItemDataType == null) { await queueEntry.AbandonAsync().AnyContext(); return(JobResult.FailedWithMessage($"Could not resolve {workItemDataType.Name} work item data type.")); } object workItemData; try { workItemData = await _queue.Serializer.DeserializeAsync(queueEntry.Value.Data, workItemDataType).AnyContext(); } catch (Exception ex) { await queueEntry.AbandonAsync().AnyContext(); return(JobResult.FromException(ex, $"Failed to parse {workItemDataType.Name} work item data.")); } var handler = _handlers.GetHandler(workItemDataType); if (handler == null) { await queueEntry.CompleteAsync().AnyContext(); return(JobResult.FailedWithMessage($"Handler for type {workItemDataType.Name} not registered.")); } if (queueEntry.Value.SendProgressReports) { await _messageBus.PublishAsync(new WorkItemStatus { WorkItemId = queueEntry.Value.WorkItemId, Progress = 0, Type = queueEntry.Value.Type }).AnyContext(); } var lockValue = await handler.GetWorkItemLockAsync(workItemData, cancellationToken).AnyContext(); if (lockValue == null) { await queueEntry.AbandonAsync().AnyContext(); handler.Log.Trace("Unable to acquire work item lock."); return(JobResult.Success); } var progressCallback = new Func <int, string, Task>(async(progress, message) => { if (handler.AutoRenewLockOnProgress) { await queueEntry.RenewLockAsync().AnyContext(); } if (handler.AutoRenewLockOnProgress) { await lockValue.RenewAsync().AnyContext(); } await _messageBus.PublishAsync(new WorkItemStatus { WorkItemId = queueEntry.Value.WorkItemId, Progress = progress, Message = message, Type = queueEntry.Value.Type }).AnyContext(); }); try { handler.LogProcessingQueueEntry(queueEntry, workItemDataType, workItemData); await handler.HandleItemAsync(new WorkItemContext(workItemData, JobId, lockValue, cancellationToken, progressCallback)).AnyContext(); if (!queueEntry.IsAbandoned && !queueEntry.IsCompleted) { await queueEntry.CompleteAsync().AnyContext(); handler.LogAutoCompletedQueueEntry(queueEntry, workItemDataType, workItemData); } if (queueEntry.Value.SendProgressReports) { await _messageBus.PublishAsync(new WorkItemStatus { WorkItemId = queueEntry.Value.WorkItemId, Progress = 100, Type = queueEntry.Value.Type }).AnyContext(); } return(JobResult.Success); } catch (Exception ex) { if (!queueEntry.IsAbandoned && !queueEntry.IsCompleted) { await queueEntry.AbandonAsync().AnyContext(); } handler.Log.Error(ex, "Error processing {0} work item queue entry ({1}).", workItemDataType.Name, queueEntry.Id); return(JobResult.FromException(ex, $"Error in handler {workItemDataType.Name}.")); } finally { await lockValue.ReleaseAsync().AnyContext(); } }
public async Task <JobResult> ProcessAsync(IQueueEntry <WorkItemData> queueEntry, CancellationToken cancellationToken) { if (queueEntry == null) { return(JobResult.Success); } if (cancellationToken.IsCancellationRequested) { await queueEntry.AbandonAsync().AnyContext(); return(JobResult.CancelledWithMessage($"Abandoning {queueEntry.Value.Type} work item: {queueEntry.Id}")); } Type workItemDataType; try { workItemDataType = Type.GetType(queueEntry.Value.Type); } catch (Exception ex) { await queueEntry.AbandonAsync().AnyContext(); return(JobResult.FromException(ex, $"Abandoning {queueEntry.Value.Type} work item: {queueEntry.Id}: Could not resolve work item data type.")); } if (workItemDataType == null) { await queueEntry.AbandonAsync().AnyContext(); return(JobResult.FailedWithMessage($"Abandoning {queueEntry.Value.Type} work item: {queueEntry.Id}: Could not resolve work item data type.")); } object workItemData; try { workItemData = _queue.Serializer.Deserialize(queueEntry.Value.Data, workItemDataType); } catch (Exception ex) { await queueEntry.AbandonAsync().AnyContext(); return(JobResult.FromException(ex, $"Abandoning {queueEntry.Value.Type} work item: {queueEntry.Id}: Failed to parse {workItemDataType.Name} work item data.")); } var handler = _handlers.GetHandler(workItemDataType); if (handler == null) { await queueEntry.CompleteAsync().AnyContext(); return(JobResult.FailedWithMessage($"Completing {queueEntry.Value.Type} work item: {queueEntry.Id}: Handler for type {workItemDataType.Name} not registered.")); } if (queueEntry.Value.SendProgressReports) { await ReportProgressAsync(handler, queueEntry).AnyContext(); } var lockValue = await handler.GetWorkItemLockAsync(workItemData, cancellationToken).AnyContext(); if (lockValue == null) { if (handler.Log.IsEnabled(LogLevel.Information)) { handler.Log.LogInformation("Abandoning {TypeName} work item: {Id}: Unable to acquire work item lock.", queueEntry.Value.Type, queueEntry.Id); } await queueEntry.AbandonAsync().AnyContext(); return(JobResult.Success); } var progressCallback = new Func <int, string, Task>(async(progress, message) => { if (handler.AutoRenewLockOnProgress) { try { await Task.WhenAll( queueEntry.RenewLockAsync(), lockValue.RenewAsync() ).AnyContext(); } catch (Exception ex) { if (handler.Log.IsEnabled(LogLevel.Error)) { handler.Log.LogError(ex, "Error renewing work item locks: {Message}", ex.Message); } } } await ReportProgressAsync(handler, queueEntry, progress, message).AnyContext(); if (handler.Log.IsEnabled(LogLevel.Information)) { handler.Log.LogInformation("{TypeName} Progress {Progress}%: {Message}", workItemDataType.Name, progress, message); } }); try { handler.LogProcessingQueueEntry(queueEntry, workItemDataType, workItemData); await handler.HandleItemAsync(new WorkItemContext(workItemData, JobId, lockValue, cancellationToken, progressCallback)).AnyContext(); if (!queueEntry.IsAbandoned && !queueEntry.IsCompleted) { await queueEntry.CompleteAsync().AnyContext(); handler.LogAutoCompletedQueueEntry(queueEntry, workItemDataType, workItemData); } if (queueEntry.Value.SendProgressReports) { await ReportProgressAsync(handler, queueEntry, 100).AnyContext(); } return(JobResult.Success); } catch (Exception ex) { if (!queueEntry.IsAbandoned && !queueEntry.IsCompleted) { await queueEntry.AbandonAsync().AnyContext(); return(JobResult.FromException(ex, $"Abandoning {queueEntry.Value.Type} work item: {queueEntry.Id}: Error in handler {workItemDataType.Name}.")); } return(JobResult.FromException(ex, $"Error processing {queueEntry.Value.Type} work item: {queueEntry.Id} in handler: {workItemDataType.Name}")); } finally { await lockValue.ReleaseAsync().AnyContext(); } }
public async Task <JobResult> RunAsync(CancellationToken cancellationToken = new CancellationToken()) { var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, TimeSpan.FromSeconds(30).ToCancellationToken()); IQueueEntry <T> queueEntry; try { queueEntry = await _queue.DequeueAsync(linkedCancellationToken.Token).AnyContext(); } catch (Exception ex) { return(JobResult.FromException(ex, $"Error trying to dequeue message: {ex.Message}")); } if (queueEntry == null) { return(JobResult.Success); } if (cancellationToken.IsCancellationRequested) { _logger.Info(() => $"Job was cancelled. Abandoning {_queueEntryName} queue item: {queueEntry.Id}"); await queueEntry.AbandonAsync().AnyContext(); return(JobResult.CancelledWithMessage($"Abandoning {_queueEntryName} queue item: {queueEntry.Id}")); } var lockValue = await GetQueueEntryLockAsync(queueEntry, cancellationToken).AnyContext(); if (lockValue == null) { await queueEntry.AbandonAsync().AnyContext(); _logger.Trace("Unable to acquire queue entry lock."); return(JobResult.Success); } try { LogProcessingQueueEntry(queueEntry); var result = await ProcessQueueEntryAsync(new QueueEntryContext <T>(queueEntry, lockValue, cancellationToken)).AnyContext(); if (!AutoComplete || queueEntry.IsCompleted || queueEntry.IsAbandoned) { return(result); } if (result.IsSuccess) { await queueEntry.CompleteAsync().AnyContext(); LogAutoCompletedQueueEntry(queueEntry); } else { await queueEntry.AbandonAsync().AnyContext(); _logger.Warn(() => $"Auto abandoned {_queueEntryName} queue entry ({queueEntry.Id})."); } return(result); } catch (Exception ex) { _logger.Error(ex, () => $"Error processing {_queueEntryName} queue entry ({queueEntry.Id})."); if (!queueEntry.IsCompleted && !queueEntry.IsAbandoned) { await queueEntry.AbandonAsync().AnyContext(); } throw; } finally { await lockValue.ReleaseAsync().AnyContext(); } }
protected async override Task <JobResult> ProcessQueueEntryAsync(JobQueueEntryContext <WorkItemData> context) { var workItemDataType = Type.GetType(context.QueueEntry.Value.Type); if (workItemDataType == null) { return(JobResult.FailedWithMessage("Could not resolve work item data type.")); } object workItemData; try { workItemData = await _queue.Serializer.DeserializeAsync(context.QueueEntry.Value.Data, workItemDataType).AnyContext(); } catch (Exception ex) { return(JobResult.FromException(ex, "Failed to parse work item data.")); } var handler = _handlers.GetHandler(workItemDataType); if (handler == null) { return(JobResult.FailedWithMessage($"Handler for type {workItemDataType.Name} not registered.")); } if (context.QueueEntry.Value.SendProgressReports) { await _messageBus.PublishAsync(new WorkItemStatus { WorkItemId = context.QueueEntry.Value.WorkItemId, Progress = 0, Type = context.QueueEntry.Value.Type }).AnyContext(); } using (var lockValue = await handler.GetWorkItemLockAsync(workItemData, context.CancellationToken).AnyContext()) { if (lockValue == null) { return(JobResult.SuccessWithMessage("Unable to acquire work item lock.")); } var progressCallback = new Func <int, string, Task>(async(progress, message) => { if (handler.AutoRenewLockOnProgress && lockValue != null) { await lockValue.RenewAsync().AnyContext(); } await _messageBus.PublishAsync(new WorkItemStatus { WorkItemId = context.QueueEntry.Value.WorkItemId, Progress = progress, Message = message, Type = context.QueueEntry.Value.Type }).AnyContext(); }); try { _logger.Info("Processing {0} work item queue entry ({1}).", workItemDataType.Name, context.QueueEntry.Id); await handler.HandleItemAsync(new WorkItemContext(context, workItemData, JobId, lockValue, progressCallback)).AnyContext(); } catch (Exception ex) { await context.QueueEntry.AbandonAsync().AnyContext(); _logger.Error("Error processing {0} work item queue entry ({1}).", workItemDataType.Name, context.QueueEntry.Id); return(JobResult.FromException(ex, $"Error in handler {workItemDataType.Name}.")); } await context.QueueEntry.CompleteAsync().AnyContext(); _logger.Info("Completed {0} work item queue entry ({1}).", workItemDataType.Name, context.QueueEntry.Id); if (context.QueueEntry.Value.SendProgressReports) { await _messageBus.PublishAsync(new WorkItemStatus { WorkItemId = context.QueueEntry.Value.WorkItemId, Progress = 100, Type = context.QueueEntry.Value.Type }).AnyContext(); } return(JobResult.Success); } }