View Source X3m.System.Scheduler behaviour (X3m System v0.8.5)

This behaviour should be used to schedule X3m.System.Message delivery at some point in time in the future. Implementation module should persist alarms so when process is respawned they can be reloaded into memory.

Not all scheduled alarms are kept in memory. They are loaded in bulks each in_memory_interval/0 milliseconds for next 2 * in_memory_interval/0 milliseconds.

If message with its X3m.System.Message.id is already in memory (and scheduled for delivery) it is ignored.

When message is processed, service_responded/2 callback is invoked. It should return either :ok or {:retry, in_milliseconds, X3m.System.Message} if returned message should be redelivered in specified number of milliseconds.

Summary

Callbacks

This optional callback defines timeout for response (in milliseconds). By default response is being waited for 5_000 milliseconds

This is optional callback that should return in which interval (in milliseconds) alarms should be loaded.

Load alarms callback is invoked on Scheduler's init with load_from as nil, and after that it is invoked each in_memory_interval/0 with load_from set to previous load_until value and new load_until will be load_from = 2 * in_memory_interval/0.

This callback is invoked when X3m.System.Message should be saved as an alarm. Time when it should be dispatched is set in its assigns.dispatch_at as DateTime.

This callback is invoked when scheduled message is processed.

Callbacks

Link to this callback

dispatch_timeout(t)

View Source (optional)
@callback dispatch_timeout(X3m.System.Message.t()) :: milliseconds :: pos_integer()

This optional callback defines timeout for response (in milliseconds). By default response is being waited for 5_000 milliseconds

Link to this callback

in_memory_interval()

View Source (optional)
@callback in_memory_interval() :: milliseconds :: pos_integer()

This is optional callback that should return in which interval (in milliseconds) alarms should be loaded.

Link to this callback

load_alarms(load_from, load_until, state)

View Source
@callback load_alarms(
  load_from :: nil | DateTime.t(),
  load_until :: DateTime.t(),
  state :: any()
) :: {:ok, [X3m.System.Message.t()]} | {:error, term()}

Load alarms callback is invoked on Scheduler's init with load_from as nil, and after that it is invoked each in_memory_interval/0 with load_from set to previous load_until value and new load_until will be load_from = 2 * in_memory_interval/0.

Link to this callback

save_alarm(t, aggregate_id, state)

View Source
@callback save_alarm(
  X3m.System.Message.t(),
  aggregate_id :: String.t(),
  state :: any()
) :: :ok | {:ok, X3m.System.Message.t()}

This callback is invoked when X3m.System.Message should be saved as an alarm. Time when it should be dispatched is set in its assigns.dispatch_at as DateTime.

3rd parameter (state) is the one that was set when Scheduler's start_link/1 was called.

If {:ok, X3m.System.Message.t()} is returned, than that message will be dispatched instead of original one. This can be used to inject something in message assigns during save_alarm.

Link to this callback

service_responded(t, state)

View Source
@callback service_responded(X3m.System.Message.t(), state :: any()) ::
  :ok | {:retry, in_ms :: non_neg_integer(), X3m.System.Message.t()}

This callback is invoked when scheduled message is processed.

It should return either :ok (and remove from persitance) so message delivery is not retried or amount of milliseconds in which delivery will be retried with potentially modifed X3m.System.Message. Its assigns can used to track number of retries for example.