Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/triggers/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def example_task(trigger_time: datetime, x: int = 1) -> str:

custom_trigger = flyte.Trigger(
"custom_cron",
flyte.Cron("0 0 * * *"), # Runs every day
flyte.Cron("0 0 * * *", timezone="Europe/London"), # Runs once every day at midnight London time
inputs={"start_time": flyte.TriggerTime, "x": 1},
)

Expand Down
2 changes: 1 addition & 1 deletion src/flyte/_internal/runtime/trigger_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def _to_schedule(m: Union[Cron, FixedRate], kickoff_arg_name: str | None = None) -> common_pb2.Schedule:
if isinstance(m, Cron):
return common_pb2.Schedule(
cron_expression=m.expression,
cron_expression=m.timezone_expression,
kickoff_time_input_arg=kickoff_arg_name,
)
elif isinstance(m, FixedRate):
Expand Down
11 changes: 9 additions & 2 deletions src/flyte/_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from dataclasses import dataclass
from datetime import datetime
from typing import Any, Dict, Mapping, Union
from typing import Any, Dict, Literal, Mapping, Union

import rich.repr

DEFAULT_TIMEZONE = "America/Los_Angeles"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be utc - no time zone to preserve current behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah easy to default to UTC



class _trigger_time:
"""
Expand Down Expand Up @@ -33,9 +35,14 @@ class Cron:
"""

expression: str
timezone: Literal["America/Los_Angeles", "Europe/London", "Australia/Sydney", "America/New_York"] = DEFAULT_TIMEZONE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why these 4?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be extended to all the timezones


@property
def timezone_expression(self) -> str:
return f"{self.timezone} {self.expression}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it must be

Suggested change
return f"{self.timezone} {self.expression}"
return f"CRON_TZ={self.timezone} {self.expression}"

for example:
CRON_TZ=America/Chicago * * * * *


def __str__(self):
return f"Cron Trigger: {self.expression}"
return f"Cron Trigger: {self.timezone_expression}"


@rich.repr.auto
Expand Down
21 changes: 15 additions & 6 deletions tests/flyte/internal/runtime/test_trigger_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
process_default_inputs,
to_task_trigger,
)
from flyte._trigger import DEFAULT_TIMEZONE
from flyte.types import TypeEngine


Expand All @@ -22,15 +23,15 @@ def test_cron_without_kickoff_arg(self):
cron = Cron("0 * * * *")
schedule = _to_schedule(cron)

assert schedule.cron_expression == "0 * * * *"
assert schedule.cron_expression == f"{DEFAULT_TIMEZONE} 0 * * * *"
assert schedule.kickoff_time_input_arg == ""

def test_cron_with_kickoff_arg(self):
"""Test Cron schedule with kickoff argument"""
cron = Cron("0 0 * * *")
schedule = _to_schedule(cron, kickoff_arg_name="trigger_time")

assert schedule.cron_expression == "0 0 * * *"
assert schedule.cron_expression == f"{DEFAULT_TIMEZONE} 0 0 * * *"
assert schedule.kickoff_time_input_arg == "trigger_time"

def test_fixed_rate_without_start_time(self):
Expand Down Expand Up @@ -65,7 +66,15 @@ def test_cron_expression_variations(self):
for expr in expressions:
cron = Cron(expr)
schedule = _to_schedule(cron)
assert schedule.cron_expression == expr
assert schedule.cron_expression == f"{DEFAULT_TIMEZONE} {expr}"

def test_cron_with_timezone(self):
"""Test Cron schedule with custom timezone"""
cron = Cron("0 * * * *", timezone="Europe/London")
schedule = _to_schedule(cron)

assert schedule.cron_expression == "Europe/London 0 * * * *"
assert schedule.kickoff_time_input_arg == ""


class TestProcessDefaultInputs:
Expand Down Expand Up @@ -199,7 +208,7 @@ async def test_basic_cron_trigger(self):
assert result.name == "test_trigger"
assert result.spec.active is True
assert result.automation_spec.type == common_pb2.TriggerAutomationSpec.Type.TYPE_SCHEDULE
assert result.automation_spec.schedule.cron_expression == "0 * * * *"
assert result.automation_spec.schedule.cron_expression == f"{DEFAULT_TIMEZONE} 0 * * * *"

@pytest.mark.asyncio
async def test_fixed_rate_trigger(self):
Expand Down Expand Up @@ -440,7 +449,7 @@ async def test_automation_spec_cron(self):
result = await to_task_trigger(trigger, "test_task", task_inputs, [])

assert result.automation_spec.type == common_pb2.TriggerAutomationSpec.Type.TYPE_SCHEDULE
assert result.automation_spec.schedule.cron_expression == "*/5 * * * *"
assert result.automation_spec.schedule.cron_expression == f"{DEFAULT_TIMEZONE} */5 * * * *"
assert result.automation_spec.schedule.kickoff_time_input_arg == ""

@pytest.mark.asyncio
Expand Down Expand Up @@ -494,7 +503,7 @@ async def test_automation_spec_with_kickoff_arg(self):

result = await to_task_trigger(trigger, "test_task", task_inputs, [])

assert result.automation_spec.schedule.cron_expression == "0 12 * * *"
assert result.automation_spec.schedule.cron_expression == f"{DEFAULT_TIMEZONE} 0 12 * * *"
assert result.automation_spec.schedule.kickoff_time_input_arg == "scheduled_at"


Expand Down
Loading