4

I'm running a job using rq with q.enqueue_call(...) and in this job I'm raising an exception with raise Exception('URL not found')

I was wondering how one would get the exception message from this job ('URL not found') after the job has failed.

I can get the stack trace with

print(get_failed_queue(connection=conn).jobs[-1].exc_info)

though can't seem to get the short error message itself.

I thought job.result (after getting the job with job = Job.fetch(job_key, connection=conn)) would work though this returns None

Madeleine Smith
  • 411
  • 1
  • 6
  • 12

2 Answers2

1

This might help

$ redis-cli
> hget rq:job:name exc_info
"x\x9c\r..."
$ python
> import zlib
> zlib.decompress("x\x9c\r...")
0

As far as I know you can't get just the last error message: you either can get the stack trace, or the value returned by the function. From the documentation here:

Python functions may have return values, so jobs can have them, too. If a job returns a non-None return value, the worker will write that return value back to the job’s Redis hash under the result key. The job’s Redis hash itself will expire after 500 seconds by default after the job is finished.

The exc_info contains the following:

When an exception is thrown inside a job, it is caught by the worker, serialized and stored under the job’s Redis hash’s exc_info key. A reference to the job is put on the failed queue.

The best way I have found to get the error message is to take the exc_info and parse it - usually on raise or something like that - and then get the last value from the split. Something like this returns the last error raised:

job.__dict__["exc_info"].split("raise")[-1]
OrionTheHunter
  • 276
  • 1
  • 7