-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timer.Cancel() #19
Comments
I believe the way to cancel a Timer from a Workflow inheriting class is sending the WorkflowAction obtained when calling The only Issue I have seen doing this is that, the configured Function in method OnCancelled of SchedullerTimer is not being executed after Timer Cancellation via CancelRequest.ForTimer method. I.E.
|
Here is a code snipped that could help to understand the problem `
} |
OnCancelled should execute when timer is cancelled. I will have a look.
workflow.OnError(e=>{
//Or log it
Console.WriteLine(e.Exception);
return ErrorAction.Continue;
}); You can read more about exception handling here
public TestWorkflow(){
ScheduleTimer("TimerTest")
.AfterActivity<TriggerTimerActivity>()
.FireAfter(TimeSpan.FromMinutes(2))
.OnFired(_ =>
{
return FailWorkflow("Failed Cancel Timer", "Failed to cancel TimerTest"); //Executed after timer's TimeSpan (2 minutes in this case)
})
.OnCancelled(_ =>
{
return CompleteWorkflow("Workflow Execution Completed"); //Not Executed After timer cancellation
}
).OnCancellationFailed(_=>Provide your custom action);
}
[WorkflowEvent(EventName.Signal)]
public WorkflowAction OnSignal(WorkflowSignaledEvent @event)
{
if ("cancel timer".EqualsIgnoreCase(@event.SignalName))
{
if (Timer("TimerTest").IsActive)
return CancelRequest.ForTimer("TimerTest");
}
return Ignore;
} |
Hi Gurmit! Just checked and the SWF console is showing the Timer as cancelled in the Events History tab, also I'm not getting any error logged on any of the Workflow Error handlers I've setup (OnError, OnPollingError, OnResponseError or OnFault). I also setup OnCancellationFailed in my timer but it seems that not even that function is being invoked when I cancel the Timer. |
I'm raising it on Amazon SWF forum but I doubt we will get a quick answer from there. I think you should use following workaround for now:
|
I have raised it on Amazon SWF https://forums.aws.amazon.com/thread.jspa?threadID=290071&tstart=0 |
Got it! n thanks for following up on this. Also let´s say we have a few more Activities that we were supposed to call after Timer Cancellation, rather than just closing the Workflow like in the previous example, what would you recommend us to do? `
} |
In following example OrderCancelled activity is scheduled when timer is cancelled while ShipOrder activity is scheduled when timer is successfully fired. [WorkflowDescription("1.0")]
public class OrderWorkflow : Workflow
{
public OrderWorkflow()
{
ScheduleTimer("GracePeriod").FireAfter(TimeSpan.FromMinutes(20))
.OnCancelled(_=>Ignore); //I will remove it in next version
.OnFired(_=>Jump.ToActivity<ShipOrder>());
ScheduleActivity<OrderCancelled>().AfterTimer("GracePeriod");
ScheduleActivity<ShipOrder>().AfterTimer("GracePeriod");
}
[WorkflowEvent(EventName.Signal)]
public WorkflowAction OnSignal(WorkflowSignaledEvent @event)
{
if ("cancel timer".EqualsIgnoreCase(@event.SignalName))
{
//You can combine multiple workflow actions together
if (Timer("GracePeriod").IsActive)
return CancelRequest.ForTimer("GracePeriod") + Jump.ToActivity<OrderCancelled>();
}
return Ignore;
}
} Hope that helps! |
Wow! that's a big surprise, I wasn't expecting to be able to return concatenated WorkflowActions. Again thanks for your help! |
Yes, it does you can read more about it here. Understanding of Deflow algorithm and workflow execution will give you a pretty good understanding on workflow is executed. |
I will update the Guflow library to align with Amazon SWF. You can find my response here. In my opinion it would be better to raise ScheduleDecisionTask event for TimerCancelled event. I will raise it a feature request if I come to know about the right place to raise it. Please use the suggested example above. I have updated it to make to more meaningful. |
Will do! Thanks |
I have released the new version 1.1.14. Please look at release notes for more details. In new version you will write the above workflow as below: [WorkflowDescription("1.0")]
public class OrderWorkflow : Workflow
{
public OrderWorkflow()
{
ScheduleTimer("GracePeriod").FireAfter(TimeSpan.FromMinutes(20))
.OnCancel(_=>Jump.ToActivity<OrderCancelled>())
.OnFired(_=>Jump.ToActivity<ShipOrder>());
ScheduleActivity<OrderCancelled>().AfterTimer("GracePeriod");
ScheduleActivity<ShipOrder>().AfterTimer("GracePeriod");
}
[WorkflowEvent(EventName.Signal)]
public WorkflowAction OnSignal(WorkflowSignaledEvent @event)
{
if ("cancel timer".EqualsIgnoreCase(@event.SignalName))
{
if (Timer("GracePeriod").IsActive)
return CancelRequest.ForTimer("GracePeriod");
}
return Ignore;
}
} |
I'm closing it now as this issue is resolved. |
Is it possible to cancel a Timer from the code?
The text was updated successfully, but these errors were encountered: