toy
toy

Reputation: 12141

How to get Job by id in RQ python?

So, basically I want to build a long-polling application which is using RQ on heroku. I have looked at this question Flask: passing around background worker job (rq, redis) but it doesn't help.

This is basically what I'm doing.

@app.route('/do_something', methods=['POST'])
def get_keywords():
    data_json = json.loads(request.data)
    text = urllib.unquote(data_json["sentence"])

    job = q.enqueue(keyword_extraction.extract, text)
    return job.key

@app.route('/do_something/<job_id>', methods=['GET'])
def get_keywords_results(job_id):
    job = Job().fetch(job_id)

    if(not job.is_finished):
        return "Not yet", 202
    else:
        return str(job.result)

Nothing is fancy, so when the POST request comes, it will queue the job and return job_id back to user immidiately, and then user will use the key to keep polling the result. However, I can't seem to get this to work as this line Job().fetch(job_id) returns

NoRedisConnectionException: Could not resolve a Redis connection.

Any help would be really appreciated.

Upvotes: 20

Views: 12884

Answers (3)

v25
v25

Reputation: 7621

In RQ version 0.13.0 I found when running:

j = q.enqueue(job_func)

j.key will be the the key preceded by rq:job:.

Therefor elsewhere in the framework when fetching the job I need to use:

j = q.fetch_job(key[7:])

Where j.result will be None or the return value of job_func.

Not sure if there's a better way to handle this...

Upvotes: 4

Alexis M&#233;taireau
Alexis M&#233;taireau

Reputation: 11215

This was due to a regression which is now fixed, see https://github.com/nvie/rq/issues/479 for the details.

You need to install from github master branch in order to resolve this, until this is released to PyPI.

Upvotes: 1

toy
toy

Reputation: 12141

I found this out already, in case anybody is interested. It has to be this one instead.

Job.fetch(job_id, connection=conn)

Upvotes: 22

Related Questions