如何在 Elixir 或 Phoenix 框架中每隔几个小时将代码安排到 运行?

How can I schedule code to run every few hours in Elixir or Phoenix framework?

假设我想每 4 小时发送一堆电子邮件或重新创建站点地图或其他任何内容,我将如何在 Phoenix 或仅使用 Elixir 中执行此操作?

您可以使用 erlcron。你这样用

job = {{:weekly, :thu, {2, :am}},
  {:io, :fwrite, ["It's 2 Thursday morning~n"]}}

:erlcron.cron(job)

A job 是一个 2 元素元组。第一个元素是表示作业计划的元组,第二个元素是函数或 MFA(模块、函数、Arity)。在上面的示例中,我们 运行 :io.fwrite("It's 2 Thursday morning") 每周四凌晨 2 点。

希望对您有所帮助!

有一个不需要任何外部依赖的简单替代方案:

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)

Quantum 允许您在运行时创建、查找和删除作业。

此外,您可以在创建 cronjob 时将参数传递给任务函数,如果您对 UTC 不满意,甚至可以修改时区。

如果您的应用 运行 作为多个独立实例(例如 Heroku),则有由 PostgreSQL 或 Redis 支持的作业处理器,它们也支持任务调度:

奥班:https://github.com/sorentwo/oban

Exq: https://github.com/akira/exq

托尼克:https://github.com/joakimk/toniq

积分:https://github.com/edgurgel/verk

我使用了 Quantum 库 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 

除了可以使用Process.send_after,还可以使用:timer.apply_interval.

Quantum is great, we use it at work as a cron replacement with a phoenix front-end and we also add jobs in real-time 非常整洁。

我找到 :timer.send_interval/2 slightly more ergonomic to use with a GenServer than Process.send_after/4 (used in ).

不必在每次处理通知时都重新安排时间,:timer.send_interval/2 设置了一个时间间隔,您可以在该时间间隔内无休止地收到消息——无需像接受的答案那样不断调用 schedule_work() .

defmodule CountingServer do
  use GenServer

  def init(_) do
    :timer.send_interval(1000, :update)
    {:ok, 1}
  end

  def handle_info(:update, count) do
    IO.puts(count)
    {:noreply, count + 1}
  end
end

每 1000 毫秒(即每秒一次),将调用 IntervalServer.handle_info/2,打印当前 count,并更新 GenServer 的状态(count + 1),为您提供输出喜欢:

1
2
3
4
[etc.]