简体   繁体   中英

How can I schedule Celery Group to run at specific time?

I have following normal function which creates Celery Group and tries to run all the subtasks in the Group at specific time:

def run_sms_task(smstask):

    if smstask:
        phones = []
        for user in smstask.userlist.users.all():
           phones.append(user.profile.phone)

        tasks = []
        for phone in phones:
            tasks.append(send_sms_async.s(phone, smstask.text))
        job = group(tasks)

        result = job.apply_async(eta=smstask.starts_at)
        result.save()
        return result.id

    return None

All the subtasks are fired when I call this function and not at the defined 'starts_at'. What is wrong? Thanks!

PS For testing reasons I have wrote a function which works fine for me if to launch tasks separately:

def run_sms_task_test1(smstask):

    if smstask:
        phones = []
        for user in smstask.userlist.users.all():
           phones.append(user.profile.phone)

        tasks = []
        for phone in phones:
            send_sms_async.apply_async([phone, smstask.text], eta=smstask.starts_at)

    return None

May be this is timezone issue.

Try to use following solution

from pytz import timezone
from datetime import datetime

london_tz = pytz.timezone('Europe/London')
london_dt = london_tz.localize(datetime.datetime(year, month, day, hour, min))
starts_at = london_dt.astimezone(pytz.UTC)
result = job.apply_async(eta=starts_at)

Hope this is helps you.

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