rq / rq-scheduler

A lightweight library that adds job scheduling capabilities to RQ (Redis Queue)
MIT License
1.45k stars 230 forks source link

Instantiate a Scheduler using an RQ Queue doesn't work #228

Closed tadrian88 closed 1 year ago

tadrian88 commented 4 years ago

Your method described in the readme.txt file to instantiate a Instantiate a Scheduler using an RQ Queue does not work. Code referred to below:

queue = Queue('bar', connection=Redis())
scheduler = Scheduler(queue=queue)

Here is my setup and how I made it to work:

  1. Initial setup: -----This is how I've initialized the Scheduler-----------
    listen = config.REDIS_CHANNEL
    dsf_redis = redis_url(host=config.REDIS_HOST, port=6380, db=0, password=config.REDIS_PASSWORD, ssl=True)
    sync_queue = Queue(name=listen + "sync", connection=dsf_redis)
    sync_scheduler = Scheduler(queue=sync_queue)

--------- This is how scheduler.py created the Scheduler class---------------------

class Scheduler(object):
    scheduler_key = 'rq:scheduler'
    scheduled_jobs_key = 'rq:scheduler:scheduled_jobs'
    queue_class = Queue
    job_class = Job

    def __init__(self, queue_name='default', queue=None, interval=60, connection=None,
                 job_class=None, queue_class=None):
        from rq.connections import resolve_connection
        self.connection = resolve_connection(connection)

After I ran this code, I got the following error: 'Could not resolve a Redis connection'. This is because connection parameter is set by default to NONE and the resolve_connection function throws this error.

I've manage to fix this by giving the resolve_connection function the queue.connection parameter ( self.connection = resolve_connection(queue.connection)), where queue is the Queue instance passed as argument to the Scheduler (sync_scheduler = Scheduler(queue=sync_queue))

sv8083 commented 4 years ago

You need to provide redis connection details as well while instantiating scheduler sync_scheduler = Scheduler(queue=sync_queue, connection=dsf_redis)

erikvanzijst commented 1 year ago

I ran into this also and created a PR to fix the README.