aaronrenner
Calculating the age of an oban job
I’m trying to put together some alerts for the age of Oban jobs. I’d like to calculate the age of the job when it finishes by looking at the (attempted_at + duration) - first_scheduled_at time. I thought about using inserted_at in place of first_scheduled_at , but that doesn’t yield the proper calculation if it was a scheduled job.
Would this be the way to go about calculating job age and would you be up for adding db fields to be able to do observability around the age of a job?
(reposting this from elixir-slack)
Marked As Solved
sorentwo
Thanks for reposting your question here.
Replicating my answers from Slack:
- We’re very conservative about adding new columns and favor using
metafor extra information like this. - If you want something consistent right away, then stash a timestamp in
metawhen you insert the job and use that for your calculation.
The issue with scheduled_at is that snoozing or retrying overwrites it. So, thinking about this a little more after our earlier, I realized that Oban can preserve the original timestamp itself (at least Pro can). This commit just landed in the v1.4 branch:
Record orig scheduling time on snooze and retry
Snoozing and rescheduling a job on error overwrites the
scheduled_at
timestamp, which makes it impossible to determine how long a job was
truly waited before execution.Now snoozing or erroring will inject the original
scheduled_at
timestamp as unix seconds into the job’s meta asorig_scheduled_at.
You’ll be able to get a reliable scheduled_at for the age calculation:
case job.meta do
%{"orig_scheduled_at" => unix_at} -> DateTime.from_unix(unix_at)
_ -> job.scheduled_at
end
Also Liked
sorentwo
Heads up, this change is out in Oban Pro v1.4.8 ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










