RQ调度程序发送多封电子邮件

时间:2016-08-01 17:44:26

标签: python django

我正在使用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)
)

我只需要在一天内运行一次这个调度程序。

但它反复发送邮件。我认为这个调度程序多次调用broadcast_approved_jobs。知道为什么吗?

1 个答案:

答案 0 :(得分:1)

(拿2) 这是我使用的功能,不是我写的,但我忘记了发现它的地方。即使您的调度程序被多次调用,它也至少会删除任何现有的作业。

   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)
    )