nietaki

nietaki

Oban.Pro.Worker after_process callback running before the job is completed?

I was trying to do a trick with Oban Pro, where I have some uniqueness constraints with only one job of the same “type” running at a time, and the after_process callback of the job would in some cases insert a subsequent job of the same “type”.

However, it looks like trying to insert the job in the callback conflicts with the freshly completed job that caused the callback.

Here’s the (simplified) code:

  def after_process(
        :complete,
        %Oban.Job{args: %mod{},
        _result
      ) do
    opts = [
      unique: [
        period: 30, #very short for experiments
        fields: [:worker, :queue],
        states: [:available, :scheduled, :executing, :retryable]
      ]
    ]
    job = mod.new(%{some: "args"})
    {:ok, job} = Oban.insert(job)
    IO.inspect job
    %{conflict?: false} = job

    :ok
  end

When I run it in its context the logs show the uniqueness conflict was triggered:

%Oban.Job{
  __meta__: #Ecto.Schema.Metadata<:built, "oban_jobs">,
  # many lines cut
  conflict?: true,
  replace: nil,
  unique: %{
    timestamp: :inserted_at,
    keys: [],
    period: 30,
    fields: [:worker, :queue],
    states: [:available, :scheduled, :executing, :retryable]
  },
  unsaved_error: nil
}

[error] [Oban.Pro.Worker] hook error: ** (MatchError) no match of right hand side value: %Oban.Job{__meta__: #Ecto.Schema.Metadata<:built, "oban_jobs">, id: 4....

When I remove :executing from the states list, the job does get inserted without errors.

Do I understand correctly that the hooks aren’t guaranteed to run after Oban sees the job as :complete?

Does that mean the “correct” way of going about what I’m trying to do is dynamically appending to the workflow the “current” job was a part of, as part of the job itself?

First Post!

sorentwo

sorentwo

Oban Core Team

You’re correct, there isn’t a guarantee that the job is marked completed when the hook runs. That’s because acking is async and there’s a 0-5ms delay before it’s updated.

An easy way around this is to override the unique options when you’re inserting the next job from after_process so it doesn’t include the executing state:

new(args, unique: [states: states])

Or you can disable uniqueness entirely when inserting with unique: nil.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement