Пример #1
0
        private async Task HandleAsync(SchedulerBatch <T> document)
        {
            if (document.RetryCount > schedulerOptions.ExecutionRetries.Length)
            {
                await schedulerStore.CompleteAsync(document.Id);

                return;
            }

            var canRetry = document.RetryCount < schedulerOptions.ExecutionRetries.Length;

            try
            {
                var isConfirmed = true;

                if (Debugger.IsAttached)
                {
                    isConfirmed = await onSuccess(document.Jobs, !canRetry, default);
                }
                else
                {
                    using (var timeout = new CancellationTokenSource(schedulerOptions.Timeout))
                    {
                        isConfirmed = await onSuccess(document.Jobs, !canRetry, timeout.Token);
                    }
                }

                if (isConfirmed)
                {
                    await schedulerStore.CompleteAsync(document.Id);
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Failed to handle job.");

                if (canRetry)
                {
                    var wait = Duration.FromMilliseconds(schedulerOptions.ExecutionRetries[document.RetryCount]);

                    var nextTime = document.DueTime.Plus(wait);

                    await schedulerStore.RetryAsync(document.Id, nextTime);
                }
                else
                {
                    try
                    {
                        await onError(document.Jobs, ex, default);
                    }
                    catch (Exception ex2)
                    {
                        log.LogError(ex2, "Failed to handle job.");
                    }
                }
            }
        }
Пример #2
0
        private async Task HandleAsync(SchedulerBatch <T> document)
        {
            var canRetry = document.RetryCount < schedulerOptions.ExecutionRetries.Length;

            try
            {
                if (currentHandler != null)
                {
                    using (var timeout = new CancellationTokenSource(schedulerOptions.Timeout))
                    {
                        await currentHandler.HandleAsync(document.Jobs, !canRetry, timeout.Token);
                    }
                }

                await schedulerStore.CompleteAsync(document.Id);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "HandleJob")
                             .WriteProperty("status", "Failed"));

                if (canRetry)
                {
                    var wait = Duration.FromMilliseconds(schedulerOptions.ExecutionRetries[document.RetryCount]);

                    var nextTime = document.DueTime.Plus(wait);

                    await schedulerStore.RetryAsync(document.Id, nextTime);
                }
                else if (currentHandler != null)
                {
                    try
                    {
                        await currentHandler.HandleExceptionAsync(document.Jobs, ex);
                    }
                    catch (Exception ex2)
                    {
                        log.LogFatal(ex2, w => w
                                     .WriteProperty("action", "HandleJobException")
                                     .WriteProperty("status", "Failed"));
                    }
                }
            }
        }