Hi Team,
I am working on a Temporal workflow that consists of five activities that need to be executed sequentially and all the activities are executing in synchronous way… The requirement is:
-
When an activity starts and completes, I need to wait for a certain duration before notifying the user to complete the next activity.
-
If the user completes the next activity before the threshold duration, the workflow should immediately proceed to the next activity instead of waiting for the full duration.
-
If the activity is not completed within the threshold duration, a notification should be sent to remind the user until activity got completed.
-
I tried implementing this using Workflow.sleep() and CancellationScope, but I was unable to achieve the desired behavior.
Approach Tried & Issues Faced
- Using
Workflow.sleep()
The problem with Workflow.sleep() is that it blocks execution until the full sleep duration completes, even if the user completes the activity before that.
Since Workflow.sleep() is not interruptible, I couldn’t find a way to wake it up early when the activity is completed within the threshold time.
- Using CancellationScope
I attempted to run Workflow.sleep() inside a CancellationScope and cancel it when the activity completes.
However, it seems that Workflow.sleep() does not respond to cancellation in the expected way.
Code snippets:
while (!firstActivity) {
Workflow.sleep(Duration.ofSeconds(10));
// notify user
super.notifyUsers("complete first activity");
}
Workflow.await(() -> firstActivity);
CancellationScope firstActivityScope = Workflow.newCancellationScope(() -> {
while (!firstActivity) {
Workflow.sleep(10000);
super.notifyUsers("complete first activity");
}
});
firstActivityScope .run();
Workflow.await(() -> firstActivity);
firstActivityScope .cancel();
Thanks in advance for your insights!