簡體   English   中英

如何讓 Oban 比每分鍾跑一次更頻繁?

[英]How to make Oban run more frequently than once in a minute?

我正在使用 Oban 來完成正在進行的任務。

# .....
    {Oban.Plugins.Cron,
     crontab: [
       {"* * * * *", MyApp.Workers.W1},
       {"* * * * *", MyApp.Workers.W2},
     ]},

我現在需要比每分鍾更頻繁地運行W1W2 - 大約每 10...30 秒一次。 由於 cron 不支持高於 1/min 的頻率,我將如何繞過此限制? 最好沒有黑客,除非絕對必要。

我不考慮從 Oban 轉到其他圖書館。

通過瀏覽當前代碼,我認為Oban.Plugins.Cron不支持小於一分鍾的粒度。

您可以這樣做的一種方法是在您的應用程序中使用一個進程(如 GenServer),該進程使用Process.send_after/3:timer.send_interval/2定期對您想要的 Oban 作業進行排隊。 這基本上就是Oban.Plugins.Cron正在做的事情。 您可能需要注意確保作業是唯一的(文檔)。

一些非常簡化的代碼:

defmodule MyApp.Scheduler do
  use GenServer

  def start_link(_) do
    GenServer.start_link(__MODULE__, :no_args)
  end

  @impl true
  def init(:no_args) do
    :timer.send_interval(:timer.seconds(30), {:schedule_job, MyApp.Workers.W1, 30})
    :timer.send_interval(:timer.seconds(20), {:schedule_job, MyApp.Workers.W2, 20})
    {:ok, :no_state}
  end

  @impl true
  def handle_info({:schedule_job, worker, unique_seconds}, state) do
    %{my: :params}
    |> Oban.Job.new(worker: worker, unique: [period: unique_seconds])
    |> Oban.insert!()

    {:noreply, state}
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM