假设我想每4小时发送一堆电子邮件或重新创建站点地图或其他东西,我在凤凰城或只有Elixir如何做到这一点?
当前回答
Quantum很棒,我们在工作中使用它作为phoenix前端的cron替代品,我们还可以实时添加作业,这非常简洁。
其他回答
Quantum很棒,我们在工作中使用它作为phoenix前端的cron替代品,我们还可以实时添加作业,这非常简洁。
有一个简单的替代方案,不需要任何外部依赖:
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)
通常我们使用Oban来实现这一点,但这取决于任务的优先级。 如果您只想运行一个应该在特定时间段后运行的作业。那么你也可以使用Genserver。
Genservers在应用程序启动时启动。你可以使用周期进程Process.send_after(self(),:work, time)和添加handle_info来处理你想做的工作。当我需要向我的项目添加长轮询时,我使用了这个。
我用的是量子库Quantum- Elixir。 遵循以下指示。
#your_app/mix.exs
defp deps do
[{:quantum, ">= 1.9.1"},
#rest code
end
#your_app/mix.exs
def application do
[mod: {AppName, []},
applications: [:quantum,
#rest code
]]
end
#your_app/config/dev.exs
config :quantum, :your_app, cron: [
# Every minute
"* * * * *": fn -> IO.puts("Hello QUANTUM!") end
]
所有的设置。运行以下命令启动服务器。
iex -S mix phoenix.server
Crontab lib &:timer, send_after, GenState机器或GenServer。
通常我们在elixir模块中定义cron表达式,然后在init期间在该模块中解析。 https://hexdocs.pm/crontab/readme.html
我们使用这个来安排一个计时器。 Process.send_after(self(),:消息,时间) 或者:timer.send_interval / 2 它返回计时器ref,它可以存储在state中,也可以被ref取消。