Merging config values from config.exs and prod.exs for using Oban

Hi there

I’m building an app that uses Oban, and specifically the Cron Plugin. Now I’ve got a couple of jobs that are executed periodically (like sending some mails, checking for stuff etc), and I wanted to change the config so that those jobs are not run by cron in dev.

So basically the idea was, instead of this in config.exs:

config :my_app, Oban,
  repo: ....
  plugins: [
    Oban.Plugin.Gossip,
    ....
    {Oban.Plugin.Cron, crontab: [some_cron_jobs_here]}
  ]

I’d simply not put the Cron plugin in the config.exs file, but instead add it only in the prod.exs file, like this:

config.exs:

config :my_app, Oban,
  repo: ....
  plugins: [
    Oban.Plugin.Gossip,
    ....
  ]

and in prod.exs add the cron jobs:

config :my_app, Oban,
  plugins: [
    {Oban.Plugin.Cron, crontab: [some_cron_jobs_here]}
  ] 

But that overrides the base config from config.exs and removes all the plugins configured there already.

Is there a way to split the config like this? I’m already aware that I can simply use if config_env() == :prod do in the config.exs file (that’s my solution now), but that’s just not so nice…

Thanks

You can build up the Oban config within your application.ex or runtime.exs to centralize it:

# Within runtime.exs

crontab =
  if config_env() == :dev do
    []
  else
    [some_jobs_here]
  end

config :my_app, Oban,
  plugins: [
    {Oban.Plugin.Cron, crontab: crontab}
  ] 

Note that you should include the crontab in test mode to have the options validated.

2 Likes

Thanks, that’s basically what I’m doing right now, just in the general config.exs file. Seems like there is no way to put this into the prod.exs file.

Anyway, doesn’t look too nice but isn’t worth the hassle to delve any deeper :slight_smile: