Content-Length: 276900 | pFad | https://github.com/interactions-py/interactions.py/issues/1560

ED [FEAT] Intervall Trigger with fixed starting point · Issue #1560 · interactions-py/interactions.py · GitHub
Skip to content

[FEAT] Intervall Trigger with fixed starting point #1560

Closed
@Donbur4156

Description

@Donbur4156

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

  • I agree to follow the contribution requirements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requeston holdThis issue/PR is on hold pending higher priority issues/PRs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/interactions-py/interactions.py/issues/1560

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy