-
Notifications
You must be signed in to change notification settings - Fork 559
Bug/4295 improve schedule state consistency #4301
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
Json-Andriopoulos
wants to merge
3
commits into
develop
Choose a base branch
from
bug/4295-improve-schedule-state-consistency
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,8 @@ | |
| from datetime import datetime, timedelta, timezone | ||
| from typing import Optional, Union | ||
|
|
||
| from croniter import CroniterBadCronError, CroniterBadDateError, croniter | ||
|
|
||
|
|
||
| def utc_now(tz_aware: Union[bool, datetime] = False) -> datetime: | ||
| """Get the current time in the UTC timezone. | ||
|
|
@@ -136,3 +138,39 @@ def expires_in( | |
| if expires_at < now: | ||
| return expired_str | ||
| return seconds_to_human_readable(int((expires_at - now).total_seconds())) | ||
|
|
||
|
|
||
| def validate_cron_expression( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember that some of the orchestrators we use support a custom cron syntax (or at least supported). This is probably something that we have to take into account here? |
||
| expr: str, *, base_time: Optional[datetime] = None | ||
| ) -> bool: | ||
| """Validate a standard cron expression using croniter. | ||
|
|
||
| Args: | ||
| expr: Cron expression string to validate. | ||
| base_time: Base datetime for croniter parsing. If not provided, uses now. | ||
|
|
||
| Returns: | ||
| True if croniter can parse the expression, False otherwise. | ||
| """ | ||
| if not isinstance(expr, str) or not expr.strip(): | ||
| return False | ||
|
|
||
| fields = expr.strip().split() | ||
| if len(fields) != 5: | ||
| return False | ||
|
|
||
| try: | ||
| bt = base_time or datetime.now() | ||
|
|
||
| for token in fields: | ||
| if "-" in token: | ||
| parts = token.split("-") | ||
| if len(parts) != 2: | ||
| return False | ||
| elif int(parts[0]) >= int(parts[1]): | ||
| return False | ||
|
|
||
| croniter(expr, bt) | ||
| return True | ||
| except (CroniterBadCronError, CroniterBadDateError, ValueError): | ||
| return False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import uuid | ||
| from datetime import datetime, timedelta, timezone | ||
| from zoneinfo import ZoneInfo | ||
|
|
||
| import pytest | ||
| from pydantic import ValidationError | ||
|
|
||
| from zenml.models.v2.core.schedule import ScheduleRequest, ScheduleUpdate | ||
|
|
||
|
|
||
| def test_schedule_request_object_validations(): | ||
| ScheduleRequest( | ||
| project=uuid.uuid4(), | ||
| name="daily schedule", | ||
| cron_expression="* * * * *", | ||
| active=True, | ||
| orchestrator_id=uuid.uuid4(), | ||
| pipeline_id=uuid.uuid4(), | ||
| ) | ||
|
|
||
| # check cron validity | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| ScheduleRequest( | ||
| project=uuid.uuid4(), | ||
| name="daily schedule", | ||
| cron_expression="60 * * * *", | ||
| active=True, | ||
| orchestrator_id=uuid.uuid4(), | ||
| pipeline_id=uuid.uuid4(), | ||
| ) | ||
|
|
||
| # check missing schedule options | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| ScheduleRequest( | ||
| project=uuid.uuid4(), | ||
| name="daily schedule", | ||
| active=True, | ||
| orchestrator_id=uuid.uuid4(), | ||
| pipeline_id=uuid.uuid4(), | ||
| start_time=datetime.now(tz=timezone.utc), | ||
| ) | ||
|
|
||
| # check datetime utc conversions | ||
|
|
||
| schedule = ScheduleRequest( | ||
| interval_second=timedelta(minutes=60), | ||
| project=uuid.uuid4(), | ||
| name="daily schedule", | ||
| active=True, | ||
| orchestrator_id=uuid.uuid4(), | ||
| pipeline_id=uuid.uuid4(), | ||
| start_time=datetime( | ||
| year=2025, | ||
| month=1, | ||
| day=1, | ||
| hour=12, | ||
| minute=0, | ||
| tzinfo=ZoneInfo("Europe/Berlin"), | ||
| ), | ||
| end_time=datetime( | ||
| year=2025, | ||
| month=1, | ||
| day=10, | ||
| hour=12, | ||
| minute=0, | ||
| tzinfo=ZoneInfo("Europe/Berlin"), | ||
| ), | ||
| ) | ||
|
|
||
| assert schedule.start_time.hour == 11 | ||
| assert schedule.end_time.hour == 11 | ||
| assert schedule.start_time.tzinfo is None | ||
| assert schedule.end_time.tzinfo is None | ||
|
|
||
|
|
||
| def test_schedule_update_object_validations(): | ||
| ScheduleUpdate( | ||
| name="daily schedule", | ||
| cron_expression="* * * * *", | ||
| ) | ||
|
|
||
| # check cron validity | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| ScheduleUpdate( | ||
| name="daily schedule", | ||
| cron_expression="60 * * * *", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from zenml.utils.time_utils import validate_cron_expression | ||
|
|
||
|
|
||
| def test_valid_cron_expressions_pass() -> None: | ||
| valid = [ | ||
| "* * * * *", | ||
| "*/5 0 * * 1-5", | ||
| "0 12 * * 0", | ||
| "15,30,45 9-17 * * 1-5", | ||
| "0 0 1 1 *", | ||
| ] | ||
| for expr in valid: | ||
| assert validate_cron_expression(expr), f"Expected valid: {expr}" | ||
|
|
||
|
|
||
| def test_invalid_cron_expressions_fail() -> None: | ||
| invalid = [ | ||
| None, | ||
| "", | ||
| "* * * *", | ||
| "* * * * * *", | ||
| "60 * * * *", | ||
| "* 24 * * *", | ||
| "* * 0 * *", | ||
| "* * * 13 *", | ||
| "* * * * 8", | ||
| "*/0 * * * *", | ||
| "5-3 * * * *", | ||
| "MON * * * *", | ||
| "@daily", | ||
| "15,30,45 9-a * * 1-5", | ||
| ] | ||
| for expr in invalid: | ||
| assert not validate_cron_expression(expr), f"Expected invalid: {expr}" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also this adds two more dependencies (
cronitorandhumanize) to ZenML, I'm not sure it's worth it for this check?Wouldn't the following implementation achieve the same thing:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most probably I will need croniter either way for the following (if implemented):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of the alternative algorithm it aligns also with your comment regarding custom cron systax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For native scheduling, we can at least keep the dependency server-side only which I think is much nicer as it doesn't affect client envs and docker images built for pipeline execution.