假设我想每 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)