简体   繁体   中英

RQ scheduler sending multiple emails

I am using Django RQ scheduler

scheduled_tasks.py

from redis import Redis
from rq_scheduler import Scheduler
from datetime import datetime

scheduler = Scheduler(connection=Redis())  # Get a scheduler for the "default" queue

# scheduler = django_rq.get_scheduler("default")
now = datetime.now()
start = now.replace(hour=8, minute=00, second=0, microsecond=0)
scheduler.schedule(
    scheduled_time=start,  # Time for first execution, in UTC timezone
    func=broadcast_approved_jobs,  # Function to be queued
    interval=86400  # Time before the function is called again, in seconds
    repeat=None  # Repeat this number of times (None means repeat forever)
)

I need to run this scheduler only once in a day.

But its sending mails repeatedly. I think this scheduler is calling broadcast_approved_jobs multiple times. Any idea why?

(take 2) This is the function I'm using, not written by me, but I forget where I found it. Even if your scheduler is being called multiple times, it will at least remove any existing jobs.

   def schedule_once(scheduled_time, func, args=None, kwargs=None,
            interval=None, repeat=None, result_ttl=None, timeout=None, queue_name=None):
        """
        Schedule job once or reschedule when interval changes
        """


        if not func in functions or not interval in functions[func] \
                or len(functions[func]) > 1:
            # clear all scheduled jobs for this function
            map(scheduler.cancel, filter(lambda x: x.func == func, jobs))

            # schedule with new interval
            scheduler.schedule(scheduled_time, func, interval=interval, repeat=repeat)




    schedule_once(
        scheduled_time=datetime.utcnow(), # Time for first execution, in UTC timezone
        func=readmail,                     # Function to be queued
        interval=120,                   # Time before the function is called again, in seconds
        repeat=0                      # Repeat this number of times (None means repeat forever)
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM