Hello,
Suppose that a system (let’s say OTP 24) configured with multi-time warp mode (Erlang -- Time and Time Correction in Erlang).
I need to start a specific task at clock wall UTC 12:00:00 and continuing executing that task periodically each 15 minutes. (12:00:00 12:15:00 12:30:00 12:45:00 … and so on).
How could I make it possible ?
One approach would be: when starting my gen_server or gen_statem, I calculate the delta time from the erlang:system_time/1 converted to calendar calendar:system_time_to_universal_time/1 like this:
Ts = erlang:system_time(millisecond),
Time = calendar:system_time_to_universal_time(Ts, millisecond)}.
Then, the next step is to find how many seconds is necessary to the next quarter hour (15min),
{_Hour, Minute, Second} = Time,
MSeconds = Minute * 60,
TSeconds = MSeconds + Second,
CurrentQuarterSeconds = erlang:round(math:ceil(TSeconds / Period) * Period),
NextQuarter = CurrentQuarterSeconds - TSeconds.
Now, I know a specific point in time to start. Getting the current time and adding it to the NextQuarter should work as expected:
Now = erlang:monotonic_time(millisecond),
T = Now + NextQuarter.
erlang:start_timer(T, self(), quarter_timeout, [{abs, true}]).
When I receive the first quarter_timeout message, then I just need to schedule another start_timer:
Now = erlang:monotonic_time(millisecond),
T = Now + timer:seconds(900),
erlang:start_timer(T, self(), quarter_timeout, [{abs, true}]).
Done, from now and beyond my process will receive a ‘quarter_timeout’ message each 15 minutes.
But, I guess it will not work like that. For sure I’m missing a couple of things here. Like milliseconds when I calculate the next periods.
Any thoughts ?