因此,假设我想每 4 小时发送一堆电子邮件或重新创建站点地图或其他任何内容,我将如何在 Phoenix 或仅使用 Elixir 做到这一点?
有一个不需要任何外部依赖项的简单替代方案:
defmodule MyApp.Periodically do use GenServer def start_link(_opts) do GenServer.start_link(__MODULE__, %{}) end def init(state) do schedule_work() # Schedule work to be performed at some point {:ok, state} end def handle_info(:work, state) do # Do the work you desire here schedule_work() # Reschedule once more {:noreply, state} end defp schedule_work() do Process.send_after(self(), :work, 2 * 60 * 60 * 1000) # In 2 hours end end
现在在您的监督树中:
children = [ MyApp.Periodically ] Supervisor.start_link(children, strategy: :one_for_one)