Insert job into Oban using exec command after mix release

I’d like to be able to seed data for my app when it’s deployed to a local Docker server. So I thought I might take advantage of Oban and create a worker to do the work. To kick the job off, I have the following in my release.ex file:

  def seed do
    Application.load(@app)

    Oban.insert(TestApp.Workers.ExampleWorker.new(%{}))
  end

In my Dockerfile, I have the following command:

CMD ["sh", "-c", "/app/bin/test_app eval TestApp.Release.migrate && /app/bin/test_app eval TestApp.Release.migrate  && /app/bin/test_app start"]

Hoiwever, when the container starts, I see the following error:

2024-03-20 20:42:15 ** (ArgumentError) unknown registry: Oban.Registry
2024-03-20 20:42:15     (elixir 1.15.7) lib/registry.ex:1400: Registry.key_info!/1
2024-03-20 20:42:15     (elixir 1.15.7) lib/registry.ex:590: Registry.lookup/2
2024-03-20 20:42:15     lib/oban/registry.ex:59: Oban.Registry.lookup/2
2024-03-20 20:42:15     lib/oban/registry.ex:32: Oban.Registry.config/1
2024-03-20 20:42:15     lib/oban.ex:327: Oban.insert/3
2024-03-20 20:42:15     (sentient_mvp 1.6.2) lib/test_app/release.ex:56: TestApp.Release.seed/0
2024-03-20 20:42:15     nofile:1: (file)

I tried using Application.start(@app). But that didn’t work either. Is there something I can do to make this work?

You need to start the :oban application, and any dependencies, first:

Application.ensure_all_loaded(:oban)

However, Worker.new returns a regular Ecto changeset, so you can also use MyRepo.insert(Worker.new(%{})) without starting Oban at all.

2 Likes

Thanks for this suggestion! It set me in the right direction. Here’s what my code looks like now:

  def seed do
    load_app()

    Application.ensure_all_started(:postgrex)
    Application.ensure_all_started(:ecto)

    Repo.start_link(pool_size: 2)
    Repo.insert(ReleaseSeeder.new(%{}))
  end