Redis on Heroku not respond

263 Views Asked by At

I'm pretty new to Redis queuing job task. I tried to do some function on the queue by using Redis To Go Add-on. But after I push some task on queue it seems to disappear.

Here is my code

import Flask
import redis
from rq import Queue

REDIS_URL = os.getenv('REDISTOGO_URL', None)
if REDIS_URL!=None:
    r = redis.from_url(REDIS_URL)
else:
    r = redis.Redis()

q = Queue(connection=r)

def function(pam1):
    print("Checkpoint1")
    return 0

@main.route('/name', methods=['GET', 'POST'])
def displayname():
    job = q.enqueue(function, pam1=0)
    return job.id

Additional info on queue: There still give the job id eg.7344337b-cca0-442a-b43a-abcdc88f012c.

but no sign of "Checkpoint1" on heroku log at all.

2

There are 2 best solutions below

0
Maitree Hirunteeyakun On BEST ANSWER

Thanks to @v25. The real reason that Redis not responds because I don't have a WORKER. After get thu see the Heroku guide on launching a worker now my problem solved.

5
v25 On

I'm not sure about Heroku's logging implementation, but assuming you're looking at the wrong log (or something) you could try this...

Instead of printing 'Checkpoint', increment the value of a custom redis key:

def function(pam1):
    r.incr('job_ran', 1)
    return 0

Then create a route where you can check this value:

@app.route('/show_count')
def show_count():
    n = r.get('job_ran')

    if n is None:
        n = 0
    else:
        n = n.decode('utf-8')

    return f'Job has run {n} times'

Now enqueue a few jobs, and check the /show_count route to make sure it has incremented accordingly.