[Python] Unit testing GeventScheduler cron Job for DST

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    I have a bug in my system where the following cron job misses an hour run during DST finish or end, using GeventScheduler from apscheduler.schedulers.gevent

    scheduler = GeventScheduler(
    job_defaults={"coalesce": True, "max_instances": 1}, timezone=pytz.utc
    )
    # Every two minutes run do_something
    cron_job = CronTrigger("*", "*", "*", "*", "*", "*", "*/2", "10")
    run_job = scheduler.add_job(
    do_something, trigger=every_2_mins, id="do_something", name="do_something"
    )


    where do_something is a function.

    My guess is that because the machine it's running on is in Eastern time, when say on November 3rd at 2:00 AM, when times go back by one hour to 1:00 AM, the UTC scheduled time doesn't change.

    First time it's 2:00 AM --> Schedule to run at 2:02 AM EST = 6:02 AM UTC time Then, its 1:00 AM Eastern --> Still scheduled to run at 6:02 AM UTC time, but the conversion doesn't happen properly and the run is skipped for one hour, until Eastern time catches up.

    My questions are:

    1. Is my hypothesis correct?
    2. How can I reproduce this in a unit test, I've gone down a few paths with mocking time using magic mock, but that didn't work. From here, I can also change the timezone from utc to something else to see if it makes a difference.

    Continue reading...

Compartilhe esta Página