Q:

Getting the runtime of a delayed job in rails?

I've got a delayed job running and I want to saved the runtime when it finishes, the worker logs the runtime to the logs like this...
XXXXXJob completed after 1.6372

I was wondering how you access this statistic from inside the delayed job class?
I've tried self.runtime but that's throwing an error.
Cheers.

A:

I'm afraid delayed_job gem doesn't provide API for accessing that stats. Looks like here is the place were they calculate running time and that's what is happening there:
runtime = Benchmark.realtime do
  invoke_job
  destroy
end
logger.info "* [JOB] #{name} completed after %.4f" % runtime

So runtime is calculated using Benchmark module and logged, but it's not stored anywhere for later usage.
Though, you still can calculate runtime of the job yourself by wrapping its logic with similar Benchmark.realtime call or something like that.

