Skip to content

Add intervals for scheduled task #498

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

so-saf
Copy link

@so-saf so-saf commented Aug 18, 2025

Hi!

Following up on PR #399, which I left unfinished half a year ago:

In this PR, I want to add intervals for scheduled tasks.

List of changes:

  • Added a new interval field to ScheduledTask.
  • Implemented the SchedulerLoop class—an abstraction over the scheduler loop.
  • Added a new loop_interval argument to SchedulerArgs.
  • Moved the skip_first_run mechanism into SchedulerLoop.run().
  • The teskiq.api.scheduler.run_scheduler_tasks function now uses SchedulerLoop.
  • Implemented the schedule_by_interval method in the AsyncTaskiqDecoratedTask and AsyncKicker classes.
  • Wrote tests for the new logic.
  • Updated the documentation.

The main idea is to iterate through all tasks every loop_interval and check if they need to be executed.

Example:

@broker.task(schedule=[{"interval": 5, "args": [1]}])  
async def add_one(value: int) -> int:  
    await asyncio.sleep(0.5)  
    return value + 1  

Logs:

[2025-08-18 11:07:50,547][INFO   ][run:run_scheduler:396] Starting scheduler.
[2025-08-18 11:07:50,551][INFO   ][run:run_scheduler:398] Startup completed.
[2025-08-18 11:07:51,001][INFO   ][run:send:160] Sending task test:add_one with schedule_id 5b32c74a07d447d68f506b089616d526.
[2025-08-18 11:07:56,002][INFO   ][run:send:160] Sending task test:add_one with schedule_id 5b32c74a07d447d68f506b089616d526.
[2025-08-18 11:08:01,002][INFO   ][run:send:160] Sending task test:add_one with schedule_id 5b32c74a07d447d68f506b089616d526.
[2025-08-18 11:08:06,002][INFO   ][run:send:160] Sending task test:add_one with schedule_id 5b32c74a07d447d68f506b089616d526.
[2025-08-18 11:08:11,001][INFO   ][run:send:160] Sending task test:add_one with schedule_id 5b32c74a07d447d68f506b089616d526.
[2025-08-18 11:08:16,001][INFO   ][run:send:160] Sending task test:add_one with schedule_id 5b32c74a07d447d68f506b089616d526.
[2025-08-18 11:08:21,001][INFO   ][run:send:160] Sending task test:add_one with schedule_id 5b32c74a07d447d68f506b089616d526.

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.

1 participant