Temporal Sleep feature for scheduled date time

Hi Temporal team:

We are exploring sleep feature.
We have below operations:
1.We determine a wake-up timestamp
2.We determine current time
3.We calculate a duration to wake-up time
4.We call sleep with duration
5.Sleep calculates a wake-up timestamp
6.Sleep creates a future-dated task

  1. By looking at the event history the sleep() is not actually sleeping for a duration.
    Rather, it calculates a wake-up timestamp and creates a wake-up task for that time (No.5, No.6).
    Is it possible we can directly use the wake-up timestamp and avoid the non-deterministic calculation of a duration?

  2. Is it possible we could bundle above operation No.1,No.2,No.3,No.4 as an atomic operation? for example in an activity? Or is there any other way we could achieve this? Or do something as question 1, directly setting wake up timestamp?
    As it could be system down after No.3 but before No.4. If it sleeps for that duration when system is back, the wake-up time is not accurate: actual sleep duration = calculated duration + system down time.

Best regards,
Jx

1.We determine a wake-up timestamp

iI this is not passed in as workflow input would calculate it in activity / local activity

2.We determine current time

Use Workflow.currentTimeMillis(), not system clock, also dont use any time zone conversions that might get system timezone from worker process.

For last question yes the service timer is created for the set duration when your workflow code calls Workflow.sleep. If step 1 is done via activity (or passed as workflow input), and steps 2,3 are done in workflow code then they would be executed in same workflow task as workflow.sleep, meaning if one of your workers that is processing it goes down, your workflow task would time out and this duration would then be calculated again on another worker. If all your workers crash for some time, when they come back up same thing would happen (duration recalculated within same workflow task as workflow.sleep).

Hi tihomir,

Just to clarify:

long futureTimstamp = activity1.getDuration();
long duration = futureTimstamp - Workflow.currentTimeMillis();
// System is down here
Workflow.sleep(duration);

Will it recalculate the duration in this situation? Or it will wait for system come back and use the duration calculated before.
If it is this case:
Sucess case: actual sleep time = previous duration;
System time case: actual sleep time = previous duration + down time;
Or:
Sucess case: actual sleep time = previous duration;
System time case: actual sleep time = recalculated duration + down time;

Best regards,
Jx

Temporal will recalculate duration in this case as it is going to reexecute the code starting from the line that follows the activity1.getDuration()

Just to add,

long duration = futureTimstamp - Workflow.currentTimeMillis();
// System is down here
Workflow.sleep(duration);

are executed in same workflow task, so if this task fails or times out (worker issues lets say) this workflow task as Maxim mentioned would be executed again. I dont understand if futureTimstamp is a specific time stamp like a time+date or a duration, the activity is called getDuration so assume its duration like 3hrs.