thiagogsr

thiagogsr

I recently upgraded Oban and Oban Pro to the latest version, 2.19.2 and 1.5.2 respectively.

I’ve faced a situation related to the unique constraint that crashed the queue and started creating hundreds of producers for the same queue.

There is a worker with the following unique configuration:

[
  fields: [:queue, :worker, :args],
  keys: [:conversation_id],
  states: [:scheduled, :executing, :retryable]
]

As you can see, the available state was not included in the states list.

There was a job on available state and other job was scheduled with the same args. The insert worked, but I think when it tried to update the scheduled job to executing it crashed everything. The fix was to manually delete the two jobs. The error was:

GenServer {Oban.Registry, {Oban, {:producer, "my_queue"}}} terminating
** (Postgrex.Error) ERROR 23505 (unique_violation) duplicate key value violates unique constraint "oban_jobs_unique_index"

    table: oban_jobs
    constraint: oban_jobs_unique_index

Key (uniq_key)=(KcFMKL8Lc5Yhu9w58TM27eZNdPcftdhRYWHIXYNaygM) already exists.
    (ecto_sql 3.12.1) lib/ecto/adapters/sql.ex:1096: Ecto.Adapters.SQL.raise_sql_call_error/1
    (ecto_sql 3.12.1) lib/ecto/adapters/sql.ex:994: Ecto.Adapters.SQL.execute/6
    (ecto 3.12.5) lib/ecto/repo/queryable.ex:232: Ecto.Repo.Queryable.execute/4
    (oban_pro 1.5.2) lib/oban/pro/engines/smart.ex:1113: Oban.Pro.Engines.Smart.fetch_jobs/2
    (ecto 3.12.5) lib/ecto/multi.ex:897: Ecto.Multi.apply_operation/5
    (elixir 1.18.2) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto 3.12.5) lib/ecto/multi.ex:870: anonymous fn/5 in Ecto.Multi.apply_operations/5
    (ecto_sql 3.12.1) lib/ecto/adapters/sql.ex:1400: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4

I believe the same could happen if there was an executing job and I tried to re-run a canceled/discarded job with the same args.

Is that expected?

For now, I changed my workers to include the available state to the unique configuration.

Checking the unique index, is on uniq_key on condition (uniq_key IS NOT NULL), and it’s interesting, because the two jobs in question only one had uniq_key, the other was empty, but both had it in the meta.

Showing Posts 1 to 3

sorentwo

sorentwo

Oban Core Team

This particular crash will only happen in the fetch_jobs/3 callback, because it’s not catching postgrex errors. All of the other state transition functions will catch that exception and correct the unique violation. It may cause a slowdown, as it has to fix issues one at a time, but it won’t crash the producer.

The upcoming Pro v1.5.3 patch will handle this issue.

The situation itself is expected, because it’s a partial set of unique states. The fact that it crashes the producer isn’t expected and certainly not desirable.

On Slack I suggested that there are only three safe/desirable unique configurations:

There are three viable unique state configurations:

  • successful - ~w(available scheduled executing retryable complete)a (the default)
  • incomplete - ~w(available scheduled executing retryable)a
  • comprehensive - ~w(available scheduled executing retryable complete cancelled discarded)a

Arguably, a setup that only uses scheduled for debouncing can also work as long as you refrain from using snooze because it makes the scheduled state reentrant.

Two jobs can’t have the same uniq_key, the index prevents that. The collision happens when the updated job is about to have that state. You can identify the conflict by checking the job’s meta, as you noted.

ericf

ericf

I have this same error when trying to upgrade using migrations, anything I can do outside of clearing jobs?

sorentwo

sorentwo

Oban Core Team

You can clear out the conflicts with a query, or remove uniqueness from the meta column for existing jobs entirely. Here’s an, admittedly inefficient, query to find and clear the duplicates:

with dupe_uniq as (
  select meta->'uniq_key' as key
  from oban_jobs
  group by meta->'uniq_key'
  having count(*) > 1
)
update oban_jobs
set meta = meta - 'uniq_key'
where meta->'uniq_key' in (select key from dupe_uniq);

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews