Elixir Tips

Elixir Tips

  • Parker Selbert

    sorentwo

    sorentwo

    Oban - Unique Keys

    By default, job uniqueness is based on teh queue, state, and args. Did you know you can restrict checking args to only a subset of keys? https://hexdocs.pm/oban/Oban.html#module-unique-jobs
    # Configure uniqueness only based on the id :id field
    defmodule MyApp.BusinessWorker do
      use Oban.Worker, unique: [keys: [:id]]
    
      # ...
    end
    
    # With an existing job:
    %{id: 1, type: "business", url: "https://example.com"}
    |> MyApp.BusinessWorker.new()
    |> Oban.insert()
    
    # Inserting another job with a different type won't work
    %{id: 1, type: "solo", url: "https://example.com"}
    |> MyApp.BusinessWorker.new()
    |> Oban.insert()
    
    # Inserting another job with a different type won't work
    %{id: 2, type: "business", url: "https://example.com"}
    |> MyApp.BusinessWorker.new()
    |> Oban.insert()
    
    17 upvotes