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

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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
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
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
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53578 245
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement