Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change start_time source in CronTrigger.next_fire() #1718

Merged
merged 1 commit into from
Aug 3, 2024

Conversation

mifuyutsuki
Copy link
Contributor

Pull Request Type

  • Feature addition
  • Bugfix
  • Documentation update
  • Code refactor
  • Tests improvement
  • CI/CD pipeline enhancement
  • Other: [Replace with a description]

Description

Changes the source of start_time in CronTrigger.next_fire() to fix a bug causing possible multiple firing.

This fix ensures that the next firing time of the task trigger would always advance after the task is fired. Before the fix, it is possible for the next fire to not advance, due to using datetime.now() for the reference start time and the possibility that the task fires just moments before the supposed start time. See attached issue for illustration.

Changes

  • Change start time of croniter in CronTrigger.next_fire() from datetime.now() to self.last_call_time.

Related Issues

#1717

Test Scenarios

The following creates a cron-triggered task every minute to print the current time to standard output.

from interactions import Client, Intents, Task, listen, CronTrigger
from datetime import datetime
bot = Client(intents=Intents.DEFAULT)

@listen()
async def on_startup():
  test_task.start()

@Task.create(CronTrigger("* * * * *"))
async def test_task():
  print(datetime.now().isoformat())

bot.start("token")

Example output before fix:

2024-07-17T01:24:00.017566
2024-07-17T01:25:00.001133
2024-07-17T01:27:00.009860
2024-07-17T01:27:59.998935
2024-07-17T01:27:59.999933
2024-07-17T01:28:00.000932
2024-07-17T01:29:00.019318
2024-07-17T01:30:00.005986
2024-07-17T01:30:59.999633
2024-07-17T01:31:00.000635
2024-07-17T01:32:00.005908

Example output after fix:

2024-07-17T01:34:00.000212
2024-07-17T01:35:00.010656
2024-07-17T01:36:00.006000
2024-07-17T01:36:59.993675
2024-07-17T01:38:00.008853
2024-07-17T01:39:00.020538
2024-07-17T01:40:00.002984
2024-07-17T01:41:00.017267
2024-07-17T01:42:00.019774
2024-07-17T01:42:59.995745
2024-07-17T01:43:59.997304

Python Compatibility

  • I've ensured my code works on Python 3.10.x
  • I've ensured my code works on Python 3.11.x

Checklist

  • I've run the pre-commit code linter over all edited files
  • I've tested my changes on supported Python versions
  • I've added tests for my code, if applicable
  • I've updated / added documentation, where applicable

Replaces the croniter start_time in CronTrigger from `datetime.now()` to `self.last_call_time.astimezone()` to fix a bug causing possible multiple firing.

Fixes interactions-py#1717
@silasary silasary merged commit bdc52f1 into interactions-py:unstable Aug 3, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants