Beispiel #1
0
        public async Task <JobResult> ProcessAsync(IQueueEntry <T> queueEntry, CancellationToken cancellationToken)
        {
            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();
            }
        }
Beispiel #2
0
        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();
            }
        }
Beispiel #3
0
        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> ProcessAsync(IQueueEntry <T> queueEntry, CancellationToken cancellationToken)
        {
            if (queueEntry == null)
            {
                return(JobResult.Success);
            }

            using (_logger.BeginScope(s => s
                                      .Property("QueueEntryId", queueEntry.Id)
                                      .PropertyIf("CorrelationId", queueEntry.CorrelationId, !String.IsNullOrEmpty(queueEntry.CorrelationId))
                                      .Property("QueueEntryName", _queueEntryName))) {
                _logger.LogInformation("Processing queue entry: id={QueueEntryId} type={QueueEntryName} attempt={QueueEntryAttempt}", queueEntry.Id, _queueEntryName, queueEntry.Attempts);

                if (cancellationToken.IsCancellationRequested)
                {
                    if (_logger.IsEnabled(LogLevel.Information))
                    {
                        _logger.LogInformation("Job was cancelled. Abandoning {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                    }

                    await queueEntry.AbandonAsync().AnyContext();

                    return(JobResult.CancelledWithMessage($"Abandoning {_queueEntryName} queue entry: {queueEntry.Id}"));
                }

                var lockValue = await GetQueueEntryLockAsync(queueEntry, cancellationToken).AnyContext();

                if (lockValue == null)
                {
                    await queueEntry.AbandonAsync().AnyContext();

                    _logger.LogTrace("Unable to acquire queue entry lock.");
                    return(JobResult.Success);
                }

                bool isTraceLogLevelEnabled = _logger.IsEnabled(LogLevel.Trace);
                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
                    {
                        if (result.Error != null || result.Message != null)
                        {
                            _logger.LogError(result.Error, "{QueueEntryName} queue entry {Id} returned an unsuccessful response: {ErrorMessage}", _queueEntryName, queueEntry.Id, result.Message ?? result.Error?.Message);
                        }

                        if (isTraceLogLevelEnabled)
                        {
                            _logger.LogTrace("Processing was not successful. Auto Abandoning {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                        }
                        await queueEntry.AbandonAsync().AnyContext();

                        if (_logger.IsEnabled(LogLevel.Warning))
                        {
                            _logger.LogWarning("Auto abandoned {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                        }
                    }

                    return(result);
                } catch (Exception ex) {
                    _logger.LogError(ex, "Error processing {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);

                    if (!queueEntry.IsCompleted && !queueEntry.IsAbandoned)
                    {
                        await queueEntry.AbandonAsync().AnyContext();
                    }

                    throw;
                } finally {
                    if (isTraceLogLevelEnabled)
                    {
                        _logger.LogTrace("Releasing Lock for {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                    }
                    await lockValue.ReleaseAsync().AnyContext();

                    if (isTraceLogLevelEnabled)
                    {
                        _logger.LogTrace("Released Lock for {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                    }
                }
            }
        }
        public async Task <JobResult> ProcessAsync(IQueueEntry <T> queueEntry, CancellationToken cancellationToken)
        {
            if (queueEntry == null)
            {
                return(JobResult.Success);
            }

            if (cancellationToken.IsCancellationRequested)
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("Job was cancelled. Abandoning {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                }

                await queueEntry.AbandonAsync().AnyContext();

                return(JobResult.CancelledWithMessage($"Abandoning {_queueEntryName} queue entry: {queueEntry.Id}"));
            }

            var lockValue = await GetQueueEntryLockAsync(queueEntry, cancellationToken).AnyContext();

            if (lockValue == null)
            {
                await queueEntry.AbandonAsync().AnyContext();

                _logger.LogTrace("Unable to acquire queue entry lock.");
                return(JobResult.Success);
            }

            bool isTraceLogLevelEnabled = _logger.IsEnabled(LogLevel.Trace);

            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
                {
                    if (isTraceLogLevelEnabled)
                    {
                        _logger.LogTrace("Processing was not successful. Auto Abandoning {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                    }
                    await queueEntry.AbandonAsync().AnyContext();

                    if (_logger.IsEnabled(LogLevel.Warning))
                    {
                        _logger.LogWarning("Auto abandoned {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                    }
                }

                return(result);
            } catch (Exception ex) {
                if (_logger.IsEnabled(LogLevel.Error))
                {
                    _logger.LogError(ex, "Error processing {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                }

                if (!queueEntry.IsCompleted && !queueEntry.IsAbandoned)
                {
                    await queueEntry.AbandonAsync().AnyContext();
                }

                throw;
            } finally {
                if (isTraceLogLevelEnabled)
                {
                    _logger.LogTrace("Releasing Lock for {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                }
                await lockValue.ReleaseAsync().AnyContext();

                if (isTraceLogLevelEnabled)
                {
                    _logger.LogTrace("Released Lock for {QueueEntryName} queue entry: {Id}", _queueEntryName, queueEntry.Id);
                }
            }
        }
Beispiel #6
0
        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();
            }
        }