Search code examples
pythonflaskredispython-rq

python-rq Queue.job_ids Always Empty


My rq tasks are running correctly, but none of the functions that get all the jobs works --

$ pip3 freeze | egrep -i "rq|redis"
redis==2.10.6
rq==0.12.0

$ flask shell
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
App: app [production]
Instance: .../flask/instance
>>> from redis import Redis
>>> import rq
>>> q = rq.Queue('example-rq', connection=Redis.from_url('redis://'))
>>> job = q.enqueue('app.tasks.example', 100)
>>> job.get_id()
'93c0e279-3ce7-48c6-8f97-ace8a29ada70'
>>> q.job_ids
[]
>>> q.get_job_ids()
[]
>>> q.jobs
[]

the example task is very basic --

$ cat app/tasks.py 
from time import sleep
from rq import get_current_job

# duration in second
def example(duration):
    job = get_current_job()
    print("Starting task example " + job.get_id())
    for i in range(duration):
        job.meta['progress'] = 100.0 * i / duration
        job.save_meta()
        if i%5 == 0:
            print(i)
        sleep(1)
    job.meta['progress'] = 100
    job.save_meta()
    print("Completed task example")

What could have gone wrong? BTW, this is under Ubuntu 18.04


Solution

  • I believe I just had the same issue as you. When you enqueue the job, the rq worker immediately takes the job off of the queue, so when you call q.job_ids, it returns an empty list because there are no longer any queued jobs. If you were to enqueue 2 jobs in a row, the rq worker would take the 1st job off the queue, and then you'd see the 2nd job_id from the subsequent q.job_ids call.

    Note: if you want to get the job_id of jobs that the worker(s) are processing, use StartedJobRegistry (see Get *all* current jobs from python-rq)