Using gen_server timeout message for periodic actions

Is it ok to use gen_server timeout message for periodic actions? Is there some reason to use send_after instead ?

defmodule Scheduler do
      use GenServer

      def start_link(args) do
        GenServer.start_link(__MODULE__, args)
      end

      # GenServer API

      def init(_opts) do
        {:ok, [], 1000}
      end

      def handle_info(:timeout,state) do
        # do something
        {:noreply, state, 1000}
      end
end

If a message arrives to the GenServer the timeout is automatically reset. Due to this we usually use a timer or send_after instead. Not saying its wrong just why we usually don’t.

I can suugest to use task_after. It provides more flexible solution for periodic actions.

1 Like

TaskAfter is useful for actions where you know the system will remain up for. For proper long-term Periodic actions I’d recommend Quantum, which is not as good for one-shot delayed actions like TaskAfter is but is great for doing something, say, once an hour or once a day or so. :slight_smile:

1 Like

Oh, I see, thank you :slightly_smiling_face: