interactions.py
interactions.py copied to clipboard
[FEAT] Intervall Trigger with fixed starting point
Problem Description
Sometimes you want to trigger something every hour at full hour. Therefor you have to create 24 TimeTriggers. Don´t want to think about every quarter hour. 😮
It would be nice if you can preperate a trigger with a starting point or smth like that.
Proposed Solution
I tinkered around a bit and created a Trigger Class based on the OrTrigger. At first you give a general TimeTrigger and then some intervall informations (hours, minutes, seconds) It creates the self.triggers list with the initial Timetrigger and for each delta an extra Timetrigger
Some code i figured out:
class TimeIntervallTrigger(OrTrigger):
_t = int | float
def __init__(self, trigger: TimeTrigger, seconds: _t = 0, minutes: _t = 0, hours: _t = 0) -> None:
self.intervall_delta = timedelta(hours=hours, minutes=minutes, seconds=seconds)
self.triggers: list[BaseTrigger] = self._create_triggerlist(trigger)
self.current_trigger: BaseTrigger = None
def _create_triggerlist(self, trigger: TimeTrigger):
now = datetime.now()
target = datetime(
now.year,
now.month,
now.day,
trigger.target_time[0],
trigger.target_time[1],
trigger.target_time[2],
tzinfo=trigger.tz,
)
triggers: list[TimeTrigger] = []
while target.date() == now.date():
triggers.append(TimeTrigger(hour=target.hour, minute=target.minute, seconds=target.second, utc=target.tzinfo))
target = target + self.intervall_delta
return triggers
in the program you can use it like that: (it prints every 10 seconds; at 00:00:10, 00:00:20, 00:00:30...)
@Task.create(
TimeIntervallTrigger(
trigger=TimeTrigger(hour=0, minute=0, seconds=0),
seconds=10,
)
)
async def cron_print():
print(f"cron at {datetime.now()}")
The code is not optimized yet. As alternative to a class I think about a function which returns a Trigger list to use in a regular OrTrigger.
Alternatives Considered
As alternative to a class I think about a function which returns a Trigger list to use in a regular OrTrigger.
Additional Information
What are your thoughts about it?
Code of Conduct
- [X] I agree to follow the contribution requirements.
I actually do like this idea - I have a similar pattern in my code. Not fully sure of how it should be designed though (or if the code snippet here is the best way it could be designed), but in general I'm all for this.
I dislike the nomenclature you've opted for, but overall its a good suggestion. I'd welcome a pr for it
thanks for the positiv feedback. I also think about an extra argument (offset) for IntervallTrigger but cannot figure it out how to resolve it. The nomenclature was not the main aspect here and will be optimize. Would you prefer the class solution or only a function which creates multiple Triggers?
I will create a PR by time