2

I am unable to get rq_scheduler working. Here is a simple example:

app.py

from flask import Flask
import datetime
from redis import Redis
from rq import Queue
from rq_scheduler import Scheduler

from tasks import example

app=Flask(__name__)
app.secret_key='abc'

app.redis = Redis.from_url('redis://')
app.task_queue = Queue('test', connection=app.redis)
scheduler = Scheduler(queue=app.task_queue,connection=app.redis)

#app.task_queue.enqueue('tasks.example',2)
#scheduler.enqueue_at(datetime.datetime(2020,4,16,10,46), example, 2) 
scheduler.enqueue_in(datetime.timedelta(seconds=1), example, 2) 

if __name__=='__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

tasks.py

import time

def example(seconds):
    print('Starting task')
    for i in range(seconds):
        print(i)
        time.sleep(1)
    print('Task completed')

In the app directory in terminal, I start the following in separate tabs:

$redis-server

$rq worker test

$rqscheduler

$python app.py

The first queue.enqueue works fine. Both scheduler tasks do nothing. What is wrong?

f923
  • 41
  • 2
  • 5

1 Answers1

2

I suspect that you may be getting confused because rqscheduler by default checks for new jobs every one minute. You can tweak this with the -i flag to set the interval in seconds, and also add the -v flag for more verbose output:

rqscheduler -i 1 -v

However I also noticed another issue with the above Flask code...

Probably due to the dev server spawning a separate process I was finding that the scheduler.enqueue_in function was enqueuing the job twice. This probably wouldn't be an issue if the enqueue_in function was called inside a view function. However where you have it placed this actually runs when the application is started.

So when launching with the dev server this gets executed twice. This will then run once every time the autoreloader senses a code change: So after starting the dev server, then saving a change to the code, 3 jobs total have been enqueued.

For the purpose of testing this, it may be advisable just to have a simple python script which doesn't actually run the Flask app:

# enqueue_test.py

from redis import Redis
from rq import Queue
from rq_scheduler import Scheduler

from tasks import example

r = Redis.from_url('redis://localhost:6379')
q = Queue('test', connection=r)
scheduler = Scheduler(queue=q, connection=r)
scheduler.enqueue_in(datetime.timedelta(seconds=1), example, 2) 
v25
  • 7,096
  • 2
  • 20
  • 36