Skip to content

Commit

Permalink
Renamed SignalAttribute to SignalEventAttribute. Added support for Wo…
Browse files Browse the repository at this point in the history
…rkflowRestartFailedEvent
  • Loading branch information
gurmitteotia committed Nov 10, 2018
1 parent a877216 commit c978e6e
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 136 deletions.
14 changes: 14 additions & 0 deletions Guflow.Tests/Decider/EventGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,20 @@ public IEnumerable<HistoryEvent> ChildWorkflowCancelRequestFailedEventGraph(Iden
return historyEvents;
}

public HistoryEvent WorkflowRestartFailedEventGraph(string cause)
{
var eventIds = EventIds.GenericEventIds(ref _currentEventId);
return new HistoryEvent
{
EventId = eventIds.EventId(EventIds.Generic),
EventType = EventType.ContinueAsNewWorkflowExecutionFailed,
ContinueAsNewWorkflowExecutionFailedEventAttributes = new ContinueAsNewWorkflowExecutionFailedEventAttributes()
{
Cause = cause,
}
};
}

private class EventIds
{
public const string Completion = "Completion";
Expand Down
10 changes: 10 additions & 0 deletions Guflow.Tests/Decider/WorkflowHistoryEventsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ public void Workflow_failure_failed_event_is_interpreted()
Assert.That(newEvents, Is.EqualTo(new[] { new WorkflowFailureFailedEvent(eventGraph) }));
}

[Test]
public void Workflow_restart_failed_event_is_interpreted()
{
var eventGraph = _builder.WorkflowRestartFailedEventGraph("cause");
var events = new WorkflowHistoryEvents(new[] { eventGraph });
var newEvents = events.NewEvents();

Assert.That(newEvents, Is.EqualTo(new[] { new WorkflowRestartFailedEvent(eventGraph) }));
}

[Test]
public void External_workflow_cancel_request_failed_event_is_interpreted()
{
Expand Down
62 changes: 62 additions & 0 deletions Guflow.Tests/Decider/WorkflowRestartFailedEventTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// /Copyright (c) Gurmit Teotia. Please see the LICENSE file in the project root folder for license information.

using Guflow.Decider;
using NUnit.Framework;

namespace Guflow.Tests.Decider
{
[TestFixture]
public class WorkflowRestartFailedEventTests
{
private WorkflowRestartFailedEvent _failedEvent;
private EventGraphBuilder _builder;

[SetUp]
public void Setup()
{
_builder = new EventGraphBuilder();
_failedEvent = new WorkflowRestartFailedEvent(_builder.WorkflowRestartFailedEventGraph("cause"));
}

[Test]
public void Populate_properties_from_event_graph()
{
Assert.That(_failedEvent.Cause, Is.EqualTo("cause"));
}

[Test]
public void By_default_fails_workflow()
{
var decisions = _failedEvent.Interpret(new EmptyWorkflow()).Decisions();

Assert.That(decisions, Is.EqualTo(new[]{new FailWorkflowDecision("FAILED_TO_RESTART_WORKFLOW", "cause")}));
}

[Test]
public void Can_return_custom_action()
{
var decisions = _failedEvent.Interpret(new WorkflowWithCompleteAction("result")).Decisions();

Assert.That(decisions, Is.EqualTo(new[] { new CompleteWorkflowDecision("result") }));
}

[WorkflowDescription("1.0")]
private class EmptyWorkflow : Workflow
{
}

[WorkflowDescription("1.0")]
private class WorkflowWithCompleteAction : Workflow
{
private readonly string _result;

public WorkflowWithCompleteAction(string result)
{
_result = result;
}

[WorkflowEvent(EventName.RestartFailed)]
public WorkflowAction OnError() => CompleteWorkflow(_result);
}
}
}
18 changes: 9 additions & 9 deletions Guflow.Tests/Decider/WorkflowSignaledEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public WorkflowWithSignalAttribute(WorkflowAction workflowAction)
_workflowAction = workflowAction;
}

[Signal]
[SignalEvent]
protected WorkflowAction Signal1(WorkflowSignaledEvent workflowSignalEvent)
{
return _workflowAction;
Expand All @@ -177,7 +177,7 @@ public WorkflowWithSignalName(WorkflowAction workflowAction)
_workflowAction = workflowAction;
}

[Signal(Name= "Signal1")]
[SignalEvent(Name= "Signal1")]
protected WorkflowAction OnSignal(WorkflowSignaledEvent workflowSignalEvent)
{
return _workflowAction;
Expand All @@ -192,12 +192,12 @@ public WorkflowWithMatchingSignalMethodAndName(WorkflowAction workflowAction)
_workflowAction = workflowAction;
}

[Signal(Name = "Signal1")]
[SignalEvent(Name = "Signal1")]
protected WorkflowAction OnSignal(WorkflowSignaledEvent workflowSignalEvent)
{
return _workflowAction;
}
[Signal]
[SignalEvent]
protected WorkflowAction Signal1(string details)
{
return WorkflowAction.Empty;
Expand All @@ -211,12 +211,12 @@ public WorkflowWithTwoSignalMethodsForSameSignal(WorkflowAction workflowAction)
_workflowAction = workflowAction;
}

[Signal]
[SignalEvent]
protected WorkflowAction Signal1(WorkflowSignaledEvent workflowSignalEvent)
{
return _workflowAction;
}
[Signal]
[SignalEvent]
protected WorkflowAction Signal1(string details)
{
return WorkflowAction.Empty;
Expand All @@ -231,12 +231,12 @@ public WorkflowWithTwoSignalMethodsForSameSignalName(WorkflowAction workflowActi
_workflowAction = workflowAction;
}

[Signal(Name = "signal1")]
[SignalEvent(Name = "signal1")]
protected WorkflowAction OnSignal(WorkflowSignaledEvent workflowSignalEvent)
{
return _workflowAction;
}
[Signal(Name = "signal1")]
[SignalEvent(Name = "signal1")]
protected WorkflowAction OnSignal(string details)
{
return WorkflowAction.Empty;
Expand All @@ -250,7 +250,7 @@ public WorkflowWithNotMatchingSignalMethod(WorkflowAction workflowAction)
_workflowAction = workflowAction;
}

[Signal(Name = "SignalNotMatching")]
[SignalEvent(Name = "SignalNotMatching")]
protected WorkflowAction OnSignal(WorkflowSignaledEvent workflowSignalEvent)
{
return Ignore;
Expand Down
57 changes: 30 additions & 27 deletions Guflow/Decider/Cancel/WorkflowCancellationFailedEvent.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
// Copyright (c) Gurmit Teotia. Please see the LICENSE file in the project root for license information.
using Amazon.SimpleWorkflow.Model;

namespace Guflow.Decider
{
// Copyright (c) Gurmit Teotia. Please see the LICENSE file in the project root for license information.
using Amazon.SimpleWorkflow.Model;

namespace Guflow.Decider
{
/// <summary>
/// Raised when SWF fails to cancel this workflow.
/// </summary>
public class WorkflowCancellationFailedEvent : WorkflowEvent
{
private readonly CancelWorkflowExecutionFailedEventAttributes _eventAttributes;
internal WorkflowCancellationFailedEvent(HistoryEvent cancellationFailedEvent)
: base(cancellationFailedEvent.EventId)
{
_eventAttributes = cancellationFailedEvent.CancelWorkflowExecutionFailedEventAttributes;
}

public string Cause { get { return _eventAttributes.Cause; } }

internal override WorkflowAction Interpret(IWorkflow workflow)
{
return workflow.WorkflowAction(this);
}

internal override WorkflowAction DefaultAction(IWorkflowDefaultActions defaultActions)
{
return defaultActions.FailWorkflow("FAILED_TO_CANCEL_WORKFLOW", Cause);
}
}
/// </summary>
public class WorkflowCancellationFailedEvent : WorkflowEvent
{
private readonly CancelWorkflowExecutionFailedEventAttributes _eventAttributes;
internal WorkflowCancellationFailedEvent(HistoryEvent cancellationFailedEvent)
: base(cancellationFailedEvent.EventId)
{
_eventAttributes = cancellationFailedEvent.CancelWorkflowExecutionFailedEventAttributes;
}

/// <summary>
/// Gets the reason on why workflow could not be cancelled.
/// </summary>
public string Cause => _eventAttributes.Cause;

internal override WorkflowAction Interpret(IWorkflow workflow)
{
return workflow.WorkflowAction(this);
}

internal override WorkflowAction DefaultAction(IWorkflowDefaultActions defaultActions)
{
return defaultActions.FailWorkflow("FAILED_TO_CANCEL_WORKFLOW", Cause);
}
}
}
69 changes: 36 additions & 33 deletions Guflow/Decider/Cancel/WorkflowCancellationRequestedEvent.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
// Copyright (c) Gurmit Teotia. Please see the LICENSE file in the project root for license information.
using Amazon.SimpleWorkflow.Model;

namespace Guflow.Decider
{
// Copyright (c) Gurmit Teotia. Please see the LICENSE file in the project root for license information.
using Amazon.SimpleWorkflow.Model;

namespace Guflow.Decider
{
/// <summary>
/// Raised when this workflow as received the request to cancel itself.
/// </summary>
public class WorkflowCancellationRequestedEvent : WorkflowEvent
{
private readonly WorkflowExecutionCancelRequestedEventAttributes _eventAttributes;

internal WorkflowCancellationRequestedEvent(HistoryEvent cancellationRequestedEvent)
: base(cancellationRequestedEvent.EventId)
{
_eventAttributes = cancellationRequestedEvent.WorkflowExecutionCancelRequestedEventAttributes;
}

public string Cause => _eventAttributes.Cause;

public string ExternalWorkflowRunid => _eventAttributes.ExternalWorkflowExecution?.RunId;

public string ExternalWorkflowId => _eventAttributes.ExternalWorkflowExecution?.WorkflowId;

internal override WorkflowAction Interpret(IWorkflow workflow)
{
return workflow.WorkflowAction(this);
}

internal override WorkflowAction DefaultAction(IWorkflowDefaultActions defaultActions)
{
return defaultActions.CancelWorkflow(Cause);
}
}
/// Raised when this workflow has received the request to cancel itself.
/// </summary>
public class WorkflowCancellationRequestedEvent : WorkflowEvent
{
private readonly WorkflowExecutionCancelRequestedEventAttributes _eventAttributes;

internal WorkflowCancellationRequestedEvent(HistoryEvent cancellationRequestedEvent)
: base(cancellationRequestedEvent.EventId)
{
_eventAttributes = cancellationRequestedEvent.WorkflowExecutionCancelRequestedEventAttributes;
}

/// <summary>
/// Reason why this cancellation request is generated.
/// </summary>
public string Cause => _eventAttributes.Cause;

public string ExternalWorkflowRunid => _eventAttributes.ExternalWorkflowExecution?.RunId;

public string ExternalWorkflowId => _eventAttributes.ExternalWorkflowExecution?.WorkflowId;

internal override WorkflowAction Interpret(IWorkflow workflow)
{
return workflow.WorkflowAction(this);
}

internal override WorkflowAction DefaultAction(IWorkflowDefaultActions defaultActions)
{
return defaultActions.CancelWorkflow(Cause);
}
}
}
57 changes: 31 additions & 26 deletions Guflow/Decider/EventName.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
// Copyright (c) Gurmit Teotia. Please see the LICENSE file in the project root for license information.
namespace Guflow.Decider
{
// Copyright (c) Gurmit Teotia. Please see the LICENSE file in the project root for license information.
namespace Guflow.Decider
{
/// <summary>
/// Represent a workflow specific event which can use in <see cref="WorkflowEventAttribute"/>.
/// </summary>
public enum EventName
{
/// </summary>
public enum EventName
{
/// <summary>
/// Raised when workflow is started.
/// </summary>
WorkflowStarted,
/// </summary>
WorkflowStarted,
/// <summary>
/// Raised when this workflow has received a signal
/// </summary>
Signal,
/// </summary>
Signal,
/// <summary>
/// Raised when this workflow failed to deliver the signal to other workflow.
/// </summary>
SignalFailed,
/// </summary>
SignalFailed,
/// <summary>
/// Raised when this workflow has received the request to cancel itself.
/// </summary>
CancelRequest,
/// </summary>
CancelRequest,
/// <summary>
/// Raised when this workflow fails to send the cancel request to external workflow.
/// </summary>
CancelRequestFailed,
/// </summary>
CancelRequestFailed,
/// <summary>
/// Raised when SWF fails to process CompleteWorkflow action.
/// </summary>
CompletionFailed,
/// </summary>
CompletionFailed,
/// <summary>
/// Raised when SWF fails to process the FailWorkflow action.
/// </summary>
FailureFailed,
/// </summary>
FailureFailed,
/// <summary>
/// Raised when SWF fails to process the CancelWorkflow action.
/// </summary>
CancellationFailed,

/// </summary>
CancellationFailed,

/// <summary>
/// Raised when SWF fails to process RecordMarker action.
/// </summary>
RecordMarkerFailed
}
/// </summary>
RecordMarkerFailed,

/// <summary>
/// Raised when SWF fails to restart the current workflow. This event can be raised in response to RestartWorkflow action.
/// </summary>
RestartFailed
}
}
Loading

0 comments on commit c978e6e

Please sign in to comment.