python-3.xflaskpython-rq

How to check if a similar scheduled job exists in python-rq?


Below is the function called for scheduling a job on server start.

But somehow the scheduled job is getting called again and again, and this is causing too many calls to that respective function.

Either this is happening because of multiple function calls or something else? Suggestions please.

def redis_schedule():
    with current_app.app_context():
        redis_url = current_app.config["REDIS_URL"]
        with Connection(redis.from_url(redis_url)):
            q = Queue("notification")
            from ..tasks.notification import send_notifs
            task = q.enqueue_in(timedelta(minutes=5), send_notifs)

Solution

  • Refer - https://python-rq.org/docs/job_registries/

    Needed to read scheduled_job_registry and retrieve jobids. Currently below logic works for me as I only have a single scheduled_job. But in case of multiple jobs, I will need to loop these jobids to find the right job exists or not.

    def redis_schedule():
        with current_app.app_context():
            redis_url = current_app.config["REDIS_URL"]
            with Connection(redis.from_url(redis_url)):
                q = Queue("notification")
    
                if len(q.scheduled_job_registry.get_job_ids()) == 0:
                    from ..tasks.notification import send_notifs
                    task = q.enqueue_in(timedelta(seconds=30), send_notifs)