How to clear Django RQ jobs from a queue?

放肆的年华 提交于 2019-12-07 03:19:40

问题


I feel a bit stupid for asking, but it doesn't appear to be in the documentation for RQ. I have a 'failed' queue with thousands of items in it and I want to clear it using the Django admin interface. The admin interface lists them and allows me to delete and re-queue them individually but I can't believe that I have to dive into the django shell to do it in bulk.

What have I missed?


回答1:


The Queue class has an empty() method that can be accessed like:

import django_rq
q = django_rq.get_failed_queue()
q.empty()

However, in my tests, that only cleared the failed list key in Redis, not the job keys itself. So your thousands of jobs would still occupy Redis memory. To prevent that from happening, you must remove the jobs individually:

import django_rq
q = django_rq.get_failed_queue()
while True:
    job = q.dequeue()
    if not job:
        break
    job.delete()  # Will delete key from Redis

As for having a button in the admin interface, you'd have to change django-rq/templates/django-rq/jobs.html template, who extends admin/base_site.html, and doesn't seem to give any room for customizing.




回答2:


The redis-cli allows FLUSHDB, great for my local environment as I generate a bizzallion jobs.

With a working Django integration I will update. Just adding $0.02.




回答3:


As @augusto-men method seems not to work anymore, here is another solution:

You can use the the raw connection to delete the failed jobs. Just iterate over rq:job keys and check the job status.

from django_rq import get_connection
from rq.job import Job

# delete failed jobs
con = get_connection('default')
for key in con.keys('rq:job:*'):
    job_id = key.decode().replace('rq:job:', '')
    job = Job.fetch(job_id, connection=con)
    if job.get_status() == 'failed':
        con.delete(key)
con.delete('rq:failed:default')  # reset failed jobs registry


来源:https://stackoverflow.com/questions/18104563/how-to-clear-django-rq-jobs-from-a-queue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!