Skip to content

Commit 358c11b

Browse files
authored
Added skip-first-run option for scheduler. (#253)
1 parent 78a47bc commit 358c11b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

docs/guide/scheduling-tasks.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,15 @@ For the `time` field of `ScheduledTask` we use timezone information from datetim
8080

8181
For `cron` tasks, we have an additional field called `cron_offset` that can be used to specify
8282
an offset of the cron task. An offset can be a string like `Europe/Berlin` or an instance of the `timedelta` class.
83+
84+
## Skipping first run
85+
86+
By default, when you start the scheduler it will get all tasks from the schedule source and check whether they should have been executed in this minute. If tasks should have been executed, they will be executed.
87+
88+
This behaviour might be not convinient for some developers. For example, if you have a task that should be executed on every minute, it will be executed once you start the scheduler, even if it was executed a few seconds ago.
89+
90+
To avoid this behaviour, you can pass the `--skip-first-run` flag to the `taskiq scheduler` command. In this case, the scheduler will wait until the start of the next minute and then start executing tasks.
91+
92+
```bash:no-line-numbers
93+
taskiq scheduler module:scheduler --skip-first-run
94+
```

taskiq/cli/scheduler/args.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class SchedulerArgs:
1616
configure_logging: bool = True
1717
fs_discover: bool = False
1818
tasks_pattern: str = "tasks.py"
19+
skip_first_run: bool = False
1920

2021
@classmethod
2122
def from_cli(cls, args: Optional[Sequence[str]] = None) -> "SchedulerArgs":
@@ -66,4 +67,13 @@ def from_cli(cls, args: Optional[Sequence[str]] = None) -> "SchedulerArgs":
6667
dest="configure_logging",
6768
help="Use this parameter if your application configures custom logging.",
6869
)
70+
parser.add_argument(
71+
"--skip-first-run",
72+
action="store_true",
73+
dest="skip_first_run",
74+
help=(
75+
"Skip first run of scheduler. "
76+
"This option skips running tasks immediately after scheduler start."
77+
),
78+
)
6979
return cls(**parser.parse_args(args).__dict__)

taskiq/cli/scheduler/run.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,15 @@ async def run_scheduler(args: SchedulerArgs) -> None:
206206
logger.info("Starting scheduler.")
207207
await scheduler.startup()
208208
logger.info("Startup completed.")
209-
209+
if args.skip_first_run:
210+
next_minute = datetime.utcnow().replace(second=0, microsecond=0) + timedelta(
211+
minutes=1,
212+
)
213+
delay = next_minute - datetime.utcnow()
214+
delay_secs = int(delay.total_seconds())
215+
logger.info(f"Skipping first run. Waiting {delay_secs} seconds.")
216+
await asyncio.sleep(delay.total_seconds())
217+
logger.info("First run skipped. The scheduler is now running.")
210218
try:
211219
await run_scheduler_loop(scheduler)
212220
except asyncio.CancelledError:

0 commit comments

Comments
 (0)