Can't start Oban as cron every minute

Hi, it is my config to start my oban every minute like this:

    def start_oban_in_runtime() do
      opts =
        [
          repo: MishkaInstaller.repo,
          queues: [compile_events: [limit: 1]],
          plugins: [
            Oban.Plugins.Pruner,
            {Oban.Plugins.Cron,
              crontab: [
                {"* * * * *", MishkaInstaller.DepUpdateJob}
              ]
            }
          ]
        ]
      DynamicSupervisor.start_child(MishkaInstaller.RunTimeObanSupervisor, {Oban, opts})
    end

and the module:

defmodule MishkaInstaller.DepUpdateJob do
  use Oban.Worker
  require Logger

  @spec perform(Oban.Job.t()) :: :ok
  def perform(_) do
    IO.inspect("test cron")
    Logger.warn("This is the test of you DepUpdateJob")
    :ok
  end
end

it works for a normal job queue, but for cron I can not run it
Thank you in advance


My application:

defmodule MishkaInstaller.Application do
    use Application
    @impl true
    def start(_type, _args) do
      children = [
        ..
        {DynamicSupervisor, [ strategy: :one_for_one, name: PluginStateOtpRunner]},
        {DynamicSupervisor, [strategy: :one_for_one, name: MishkaInstaller.RunTimeObanSupervisor]},
        ..
      ] 
      opts = [strategy: :one_for_one, name: MishkaInstaller.Supervisor]
      Supervisor.start_link(children, opts)
    end
  end
  

I have read this post, but I could not fix my problem

extra data:

%Oban.Config{
  dispatch_cooldown: 5,
  engine: Oban.Queue.BasicEngine,
  get_dynamic_repo: nil,
  log: false,
  name: Oban,
  node: "shahryars-iMac",
  notifier: Oban.Notifiers.Postgres,
  peer: Oban.Peers.Postgres,
  plugins: [
    {Oban.Plugins.Cron,
     [crontab: [{"* * * * *", MishkaInstaller.DepUpdateJob}]]},
    Oban.Plugins.Pruner,
    Oban.Plugins.Stager
  ],
  prefix: "public",
  queues: [compile_events: [limit: 1]],
  repo: Repo,
  shutdown_grace_period: 15000,
  testing: :disabled
}

I haven’t really dug into this very far, but the top of the module documentation suggests this might be a pro feature.

https://hexdocs.pm/oban/Oban.Plugins.Cron.html

1 Like

You might be able to just set up a little genserver that does this for you when you start the Oban supervisor.

Process.send_after(self(), 60_000, :enqueue_jobs)

Or maybe there’s a way you can configure this at boot and still get your job done.

1 Like

I think I found what is my problem, when I define a queues list after that I should put a name for my new job even it is a crontab

The config:

queues: [compile_events: [limit: 1], update_events: [limit: 1]],
          plugins: [
            Oban.Plugins.Pruner,
            {Oban.Plugins.Cron,
              crontab: [
                {"* * * * *", MishkaInstaller.DepUpdateJob}
              ]
            }
          ]

The module:

defmodule MishkaInstaller.DepUpdateJob do
  use Oban.Worker, queue: :update_events, max_attempts: 1

This plugin is very big and has many facilities, if is there any sample code it would help the new people to implement the basic stuff very fast and after that documents can help to have a custom config

I am not native English language, but I try to learn it and create some tutorials for it on youtube

The issue, as you discovered, was purely that you didn’t specify a queue for the worker (it defaults to default) and you weren’t running the default queue.

That’s the most common cause of jobs not running in my experience. People also don’t realize that Cron only inserts jobs, it doesn’t run them directly.

1 Like

I think with some sample codes in real-simple app, it can be easy to understand, especially for new people. Your library is very fantastic and covers many things, thank you for all your efforts.

If you have time, could you see this comment, please?

I meant the queued tasks (available)

It’s all stored in the oban_jobs table, which you can query from a psql console, or from Ecto:

select * from oban_jobs where state = ‘available’
1 Like