Skip to content

Commit

Permalink
Inroduce OnActorMethodFailedAsync virtual method for overriding (#1014)
Browse files Browse the repository at this point in the history
Signed-off-by: Vlad Rudenko <vladislav.rudenko@gmail.com>
  • Loading branch information
vlardn authored Aug 16, 2023
1 parent f4e02df commit 667dcaf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
31 changes: 28 additions & 3 deletions src/Dapr.Actors/Runtime/Actor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -101,10 +101,11 @@ internal async Task OnPostActorMethodAsyncInternal(ActorMethodContext actorMetho
await this.SaveStateAsync();
}

internal Task OnInvokeFailedAsync()
internal async Task OnActorMethodFailedInternalAsync(ActorMethodContext actorMethodContext, Exception e)
{
await this.OnActorMethodFailedAsync(actorMethodContext, e);
// Exception has been thrown by user code, reset the state in state manager.
return this.ResetStateAsync();
await this.ResetStateAsync();
}

internal Task ResetStateAsync()
Expand Down Expand Up @@ -190,6 +191,30 @@ protected virtual Task OnPostActorMethodAsync(ActorMethodContext actorMethodCont
return Task.CompletedTask;
}

/// <summary>
/// Override this method for performing any action when invoking actor method has thrown an exception.
/// This method is invoked by actor runtime when invoking actor method has thrown an exception.
/// </summary>
/// <param name="actorMethodContext">
/// An <see cref="ActorMethodContext"/> describing the method that was invoked by actor runtime prior to this method.
/// </param>
/// <param name="e">Exception thrown by either actor method or by OnPreActorMethodAsync/OnPostActorMethodAsync overriden methods.</param>
/// <returns>
/// Returns a <see cref="Task">Task</see> representing post-actor-method operation.
/// </returns>
/// /// <remarks>
/// This method is invoked by actor runtime prior to:
/// <list type="bullet">
/// <item><description>Invoking an actor interface method when a client request comes.</description></item>
/// <item><description>Invoking a method when a reminder fires.</description></item>
/// <item><description>Invoking a timer callback when timer fires.</description></item>
/// </list>
/// </remarks>
protected virtual Task OnActorMethodFailedAsync(ActorMethodContext actorMethodContext, Exception e)
{
return Task.CompletedTask;
}

/// <summary>
/// Registers a reminder with the actor.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Dapr.Actors/Runtime/ActorManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -355,9 +355,9 @@ private async Task<T> DispatchInternalAsync<T>(ActorId actorId, ActorMethodConte
// PostActivate will save the state, its not invoked when actorFunc invocation throws.
await actor.OnPostActorMethodAsyncInternal(actorMethodContext);
}
catch (Exception)
catch (Exception e)
{
await actor.OnInvokeFailedAsync();
await actor.OnActorMethodFailedInternalAsync(actorMethodContext, e);
throw;
}
finally
Expand Down

0 comments on commit 667dcaf

Please sign in to comment.