0

I'm using Oban for the ongoing tasks.

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

I now need to run W1 and W2 more frequently that every minute - around once in every 10...30 seconds. Since cron doesn't support higher frequency than 1/min, how would I get around this limitation? Preferably without hacks, unless absolutely necessary.

I don't consider switching from Oban to other library.

1 Answer 1

1

I don't believe Oban.Plugins.Cron supports granularity finer than one minute, by glancing at the current code.

One way you could do this is by having a process (like a GenServer) in your application that uses Process.send_after/3 or :timer.send_interval/2 to periodically queue the Oban jobs you want. This is essentially what Oban.Plugins.Cron is doing. You'll probably want to pay attention to making sure the jobs are unique (documentation).

Some very simplified code:

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
2
  • But then I'll have to turn off the standard mechanism by which Oban will insert W1 and W2 jobs, right? Remove these 2 lines from config {"* * * * *", MyApp.Workers.W1}, and {"* * * * *", MyApp.Workers.W2},?
    – Kon
    Feb 1 at 13:35
  • Yes, absolutely. I should have made that clear, my bad 🙃 Feb 1 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.