question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AndTrigger does not work with IntervalTrigger

See original GitHub issue

Recently, I learn how to use apscheduler, and I find something interesting. From the latest doc’s example, AndTrigger can be used as:

from apscheduler.triggers.combining import AndTrigger
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.triggers.cron import CronTrigger


trigger = AndTrigger([IntervalTrigger(hours=2),
                      CronTrigger(day_of_week='sat,sun')])
scheduler.add_job(job_function, trigger)

Actually, it doesn’t work. I have read https://github.com/agronholm/apscheduler/issues/281 and https://github.com/agronholm/apscheduler/issues/309, and know that the time generated by the two triggers never coincide.

So, if I sepcify a start_date like

import datetime
from tzlocal import get_localzone

from apscheduler.triggers.combining import AndTrigger
from apscheduler.triggers.interval import IntervalTrigger

tz = get_localzone()
now = datetime.datetime.now(tz=tz)

trigger = AndTrigger([IntervalTrigger(hours=2, start_date=now),
                      IntervalTrigger(hours=3, start_date=now)])
next_run_time = trigger.get_next_fire_time(None, now + datetime.timedelta(seconds=10))

print(now)
print(next_run_time)

It works as expected.

2019-02-24 22:23:26.052845+09:00
2019-02-25 04:23:26.052845+09:00

But when schedule a job, it does work.

import datetime
from tzlocal import get_localzone

from apscheduler.triggers.combining import AndTrigger
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.schedulers.blocking import BlockingScheduler

tz = get_localzone()
now = datetime.datetime.now(tz=tz)


def tick():
    print('tick')


scheduler = BlockingScheduler()
trigger = AndTrigger([IntervalTrigger(seconds=2, start_date=now),
                      IntervalTrigger(seconds=3, start_date=now)])
scheduler.add_job(tick, trigger)
scheduler.start()

The real problem it that AndTrigger may not support IntervalTrigger

In _process_jobs, it will calculate run times .

https://github.com/agronholm/apscheduler/blob/ab991eb29eedb0943356c4d7ddca8320e7845965/apscheduler/schedulers/base.py#L970-L972

https://github.com/agronholm/apscheduler/blob/ab991eb29eedb0943356c4d7ddca8320e7845965/apscheduler/job.py#L123-L137

https://github.com/agronholm/apscheduler/blob/ab991eb29eedb0943356c4d7ddca8320e7845965/apscheduler/triggers/combining.py#L53-L62

https://github.com/agronholm/apscheduler/blob/ab991eb29eedb0943356c4d7ddca8320e7845965/apscheduler/triggers/interval.py#L52-L66

In the loop of calculating next_run_time in AndTrigger, we only change the value of now. But in IntervalTrigger, if we pass previous_fire_time which is not None, it will just add the interval. So it caused a dead loop.

Similarly, When we combine IntervalTrigger and CronTrigger, if previous_fire_time is not None, only CronTrigger will walk.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:24 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
agronholmcommented, Jul 19, 2019

Something like this is coming to APScheduler 4.0.

1reaction
agronholmcommented, Sep 29, 2020

I’ve posted issue #465 to track v4.0 development so I will stop posting updates here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

apscheduler.triggers.combining - Read the Docs
The trigger is considered to be finished when any of the given triggers has ... AndTrigger from apscheduler.triggers.interval import IntervalTrigger from ...
Read more >
Python Aps Scheduler run in interval between two weekdays ...
trigger1 is executed monday to fri from 0:00 to 19:00 every day (not executed from ... trigger1 = AndTrigger([IntervalTrigger(minutes=1), ...
Read more >
Introduction to APScheduler - Better Programming
Advanced Python Scheduler (APScheduler) is a Python library that lets you schedule your Python code to be executed later, either just once or...
Read more >
APScheduler Documentation - Read the Docs
If, for some reason, pip won't work, you can manually download the ... the right scheduler, job store(s), executor(s) and trigger(s).
Read more >
How to use the apscheduler.triggers.interval.IntervalTrigger ...
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found