Skip to content

Instantly share code, notes, and snippets.

@jokull
Created May 1, 2013 21:30
Show Gist options
  • Select an option

  • Save jokull/5498543 to your computer and use it in GitHub Desktop.

Select an option

Save jokull/5498543 to your computer and use it in GitHub Desktop.
Example of an RQ exception handler that allows three failures before moving to the failed queue
queues = []
def retry_handler(job, exc_type, exc_value, traceback):
# Returning True moves the job to the failed queue (or continue to
# the next handler)
job.meta.setdefault('failures', 1)
job.meta['failures'] += 1
if job.meta['failures'] > 3 or isinstance(exc_type, (LookupError, CorruptImageError)):
job.save()
return True
job.status = Status.QUEUED
for queue_ in queues:
if queue_.name == job.origin:
queue_.enqueue_job(job, timeout=job.timeout)
break
else:
return True # Queue has disappeared, fail job
return False # Job is handled. Stop the handler chain.
queues.append(Queue(exc_handler=retry_handler))
@Alex-Poon

Copy link
Copy Markdown

Where does STATUS and queues come from?

@hithwen

hithwen commented Feb 11, 2014

Copy link
Copy Markdown

I found status: from rq.job import Status

@pythonjunkie

Copy link
Copy Markdown

queues come from Queue
from rq import Queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment