Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/TickerQ/Src/TickerExecutionTaskHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ private async Task RunContextFunctionAsync(InternalFunctionContext context, bool
catch (Exception ex)
{
lastException = ex;

context.SetProperty(x => x.ExceptionDetails, SerializeException(ex));

if (_serviceProvider.GetService(typeof(ITickerExceptionHandler)) is ITickerExceptionHandler handler)
await handler.HandleExceptionAsync(ex, context.TickerId, context.Type);

await _internalTickerManager.UpdateTickerAsync(context, cancellationToken);

context.ResetUpdateProps();
}
}

Expand Down Expand Up @@ -292,11 +301,6 @@ private async Task RunContextFunctionAsync(InternalFunctionContext context, bool
_tickerQInstrumentation.LogJobFailed(context.TickerId, context.FunctionName, lastException, context.RetryCount);
_tickerQInstrumentation.LogJobCompleted(context.TickerId, context.FunctionName, stopWatch.ElapsedMilliseconds, false);

var handler = _serviceProvider.GetService(typeof(ITickerExceptionHandler)) as ITickerExceptionHandler;

if (handler != null)
await handler.HandleExceptionAsync(lastException, context.TickerId, context.Type);

await _internalTickerManager.UpdateTickerAsync(context, cancellationToken);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TickerQ.Tests/SoftSchedulerNotifyDebounceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void NotifySafely_AlwaysInvokes_ForZeroValue()
debounce.NotifySafely(0);
debounce.NotifySafely(0);

callCount.Should().BeGreaterOrEqualTo(2);
callCount.Should().BeGreaterThanOrEqualTo(2);
}

[Fact]
Expand Down
48 changes: 47 additions & 1 deletion tests/TickerQ.Tests/TickerExecutionTaskHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task ExecuteTaskAsync_SetsElapsedTime_OnSuccess()

await _handler.ExecuteTaskAsync(context, isDue: false);

context.ElapsedTime.Should().BeGreaterOrEqualTo(0);
context.ElapsedTime.Should().BeGreaterThanOrEqualTo(0);
}

[Fact]
Expand Down Expand Up @@ -136,6 +136,52 @@ await exceptionHandler.Received(1).HandleExceptionAsync(
Arg.Is(context.Type));
}

[Fact]
public async Task ExecuteTaskAsync_CallsExceptionHandler_ForEachFailedAttempt()
{
var exceptionHandler = Substitute.For<ITickerExceptionHandler>();
var services = new ServiceCollection();
services.AddSingleton(_internalManager);
services.AddSingleton(_instrumentation);
services.AddSingleton(exceptionHandler);
var sp = services.BuildServiceProvider();
var handler = new TickerExecutionTaskHandler(sp, _clock, _instrumentation, _internalManager);

var context = CreateContext(ct: (_, _, _) => throw new InvalidOperationException("boom"));
context.Retries = 2;
context.RetryIntervals = [0, 0];

await handler.ExecuteTaskAsync(context, isDue: false);

await exceptionHandler.Received(3).HandleExceptionAsync(
Arg.Any<Exception>(),
Arg.Is(context.TickerId),
Arg.Is(context.Type));
}

[Fact]
public async Task ExecuteTaskAsync_UpdatesExceptionDetails_OnFailedAttemptBeforeRetriesComplete()
{
var context = CreateContext(ct: (_, _, _) => throw new InvalidOperationException("boom"));
context.Retries = 1;
context.RetryIntervals = [0];
var observedUpdates = new List<(TickerStatus Status, string ExceptionDetails)>();

_internalManager
.When(x => x.UpdateTickerAsync(Arg.Any<InternalFunctionContext>(), Arg.Any<CancellationToken>()))
.Do(callInfo =>
{
var ctx = callInfo.Arg<InternalFunctionContext>();
observedUpdates.Add((ctx.Status, ctx.ExceptionDetails));
});

await _handler.ExecuteTaskAsync(context, isDue: false);

observedUpdates.Should().Contain(x =>
x.Status == TickerStatus.InProgress &&
!string.IsNullOrWhiteSpace(x.ExceptionDetails));
}

#endregion

#region Cancellation Path
Expand Down
Loading