Can you please provide an example of a function to...
# ask-ai
t
Can you please provide an example of a function to be passed as the should_execute argument in a ScheduleDefinition
s
Certainly! The
should_execute
argument in a
ScheduleDefinition
is a function that determines whether a schedule should execute or skip at the time of schedule evaluation. This function takes a
ScheduleEvaluationContext
and returns a boolean (
True
if the schedule should execute). Here is an example of a
should_execute
function that you can pass to a
ScheduleDefinition
. This function checks if the current day is a weekday (Monday through Friday) and only allows the schedule to execute on weekdays:
Copy code
python
from dagster import ScheduleDefinition, ScheduleEvaluationContext
import datetime

def should_execute_on_weekdays(context: ScheduleEvaluationContext) -> bool:
    # Get the scheduled execution time from the context
    scheduled_execution_time = context.scheduled_execution_time

    # Check if the scheduled_execution_time is a weekday (Monday is 0 and Sunday is 6)
    return scheduled_execution_time.weekday() < 5

# Example of creating a ScheduleDefinition with the should_execute function
weekday_schedule = ScheduleDefinition(
    job=my_job,  # Replace with your job definition
    cron_schedule="0 0 * * *",  # This cron schedule runs daily at midnight
    should_execute=should_execute_on_weekdays,
    # ... other parameters
)
In this example,
should_execute_on_weekdays
is a function that checks the
scheduled_execution_time
from the
ScheduleEvaluationContext
to determine if it is a weekday. If it is, the function returns
True
, allowing the schedule to execute. If it is not a weekday (i.e., it's Saturday or Sunday), the function returns
False
, and the schedule will skip execution for that day. Please replace
my_job
with your actual job definition when using this example in your code. Dagster Docs button Schedules | Dagster button [Content Gap] schedule should_execute ex... button Consider providing a convenient way in D... button Thank you for your feedback!
t
Boolean return!