pavancse17

pavancse17

Handling failed Oban jobs

Hey Devs,

I have been using Oban — Oban v2.5.0 (hexdocs.pm) in one of my phoenix application. My requirement is like when job fails after trying couple of times based on the max attempts that has been configured to that job. I need to update that job has been failed in DB and give an option to retry the job. I am able to accomplish this using Oban.Telemetry — Oban v2.5.0 (hexdocs.pm) events & Oban.retry. But is there a better way like a call back that can be written within the worker that will be called when job fails?

Marked As Solved

sorentwo

sorentwo

Oban Core Team

Nope. That is exactly how you’d handle that!

Now, I’m not sure why you’re double-tracking records in the DB, but that’s entirely your prerogative :slightly_smiling_face:

Also Liked

sorentwo

sorentwo

Oban Core Team

If you mean that you want to automatically retry it based on some criteria, then telemetry + retry is the exact mechanism you need. The telemetry events run within the job’s process and are provided in lieu of various hooks.

If this is something you want to do periodically, you can use a cron job that retries for you. Or, if it is something that is more manual based on some inspection, you can retry using the database queries (or the web dashboard).

pavancse17

pavancse17

I guess my question is not clearly framed. My requirement is like below:

  • I have a table called job_a_tracker with fields like id, failed_job_id, status and the job called heavy_job.
  • Before i trigger the heavy_job I will create a record in job_a_tracker with status="started" & then trigger it.
  • Now when job completes successfully I can very well update the status in job_a_tracker by keeping it as a last statement in the worker.
  • But tricky part is when job gets failed. I need to save the failed_job_id and update status as Failed.

Right now I attached something like this in application.ex file:

:telemetry.attach(
      "oban-logger",
      [:oban, :job, :exception],
      &MyApp.JobTracker.handle_event/4,
      nil
    )
defmodule MyApp.JobTracker do
  require Logger
  def handle_event(
        [:oban, :job, :exception],
        %{duration: duration},
        %{
          attempt: attempt,
          max_attempts: max_attempts,
          worker: "MyApp.HeavyJob"
        } = meta,
        nil
      ) do
# update record with failed_job_id = meta.id & status = "Failed"
end
end

Is there a better way to handle this ?

sorentwo

sorentwo

Oban Core Team

Absolutely. The job is stored in the database within the oban_jobs table and has a state field on it. You can link to them or even set up foreign keys to track the job. The telemetry events will also let you immediately notify a user that the job succeeded or failed.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
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 Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement