Elixir Tips

Elixir Tips

  • Parker Selbert

    sorentwo

    sorentwo

    Oban - Unique jobs

    Did you know that Oban lets you specify constraints to prevent enqueuing duplicate jobs? Uniqueness is enforced as jobs are inserted, dynamically and atomically. https://hexdocs.pm/oban/Oban.html#module-unique-jobs
    # Configure 60 seconds of uniqueness within the worker
    defmodule MyApp.BusinessWorker do
      use Oban.Worker, unique: [period: 60]
    
      # ...
    end
    
    # Manually override the unique period for a single job
    MyApp.BusinessWorker.new(%{id: 1}, unique: [period: 120])
    
    # Override a job to have an infinite unique period, which lasts
    # as long as jobs are persisted
    MyApp.BusinessWorker.new(%{id: 1}, unique: [period: :infinity])
    
    19 upvotes