Inside a schedule job, how can I add the date or t...
# ask-ai
t
Inside a schedule job, how can I add the date or time to the run key of the run request?
s
To add the date or time to the run key of a
RunRequest
inside a scheduled job, you can utilize the
ScheduleEvaluationContext
to access the scheduled execution time and format it as needed. Here's a step-by-step explanation based on the documentation: 1. Define a resource that formats the date or time. In the example provided by the documentation, a
DateFormatter
resource is created that takes a
datetime
object and returns a string formatted according to the specified format. 2. Use the
ScheduleEvaluationContext
within your schedule function to get the
scheduled_execution_time
, which is the time at which the schedule is intended to run. 3. Format the
scheduled_execution_time
using the
DateFormatter
resource to create a string representation of the date or time. 4. Include the formatted date or time in the
run_key
when creating the
RunRequest
. Here is an example from the documentation that demonstrates this process:
Copy code
python from dagster import schedule, ScheduleEvaluationContext, ConfigurableResource, job, RunRequest from datetime import datetime  class DateFormatter(ConfigurableResource):     format: str      def strftime(self, dt: datetime) -> str:         return dt.strftime(self.format)  @job def process_data():     ...  @schedule(job=process_data, cron_schedule="* * * * *") def process_data_schedule(     context: ScheduleEvaluationContext,     date_formatter: DateFormatter, ):     formatted_date = date_formatter.strftime(context.scheduled_execution_time)      return RunRequest(         run_key=formatted_date,         tags={"date": formatted_date},     )  # Assuming the resource is defined elsewhere with a specific format, e.g., "%Y-%m-%d"
In this example, the
process_data_schedule
function formats the
scheduled_execution_time
using the
DateFormatter
resource and sets the
run_key
of the
RunRequest
to this formatted date. This way, each run request generated by the schedule will have a unique run key corresponding to the date and time it was scheduled to run.