https://dagster.io/ logo
#ask-community
Title
# ask-community
c

Charlie Bini

05/12/2022, 9:01 PM
is it possible to access the last run timestamp for a schedule within an op context?
y

yuhan

05/12/2022, 9:13 PM
It’s possible but not natively supported - whether it’s recommended or not depends on your use case blob smiley You can get runs from the run storage with an op context via
context.instance.get_runs
and filter it down to the last run to find the timestamp. for example, this is how we do it internally: https://github.com/dagster-io/dagster/blob/master/python_modules/dagster/dagster/core/execution/backfill.py#L290
The RunFilter and tag-based scheme are not designed to be user-facing, so if you would use it please keep in mind the APIs could change over time.
c

Charlie Bini

05/12/2022, 9:14 PM
nice, yeah I have an idea for a pretty solid use case, but I'm also wondering if it would be a better as a partitioned job
hi @yuhan circling back on this. I was able to get the most recent run, but I'm unsure about the best way to get the execution time. I see it under tags['.dagster/scheduled_execution_time'], is that the best place?
also seems like dagster-graphql could be an approved user-facing way to do this, but it looks like there's no way to run a generic query according to the docs
y

yuhan

05/18/2022, 11:18 PM
Are you looking for the timestamp when the run started or the total duration of how long the job has run?
c

Charlie Bini

05/18/2022, 11:18 PM
timestamp
y

yuhan

05/18/2022, 11:22 PM
Got it - you can use
context.instance.get_run_records
instead https://github.com/dagster-io/dagster/blob/master/python_modules/dagster/dagster/core/instance/__init__.py#L1252 it returns a list of RunRecord which has the
create_timestamp
and other info like start/end time of the run
c

Charlie Bini

05/18/2022, 11:35 PM
cool, will
limit: 1
yield the most recent run?
y

yuhan

05/18/2022, 11:37 PM
yup - the default is order by run id (which auto increments) and descending
👌 1
c

Charlie Bini

05/19/2022, 12:08 AM
awesome
here's what I'm doing with it:
Copy code
def gql_last_schedule_run(context):
    runs = context.instance.get_run_records(
        filters=RunsFilter(
            statuses=[DagsterRunStatus.SUCCESS],
            job_name=context.job_name,
            tags={
                "dagster/schedule_name": context.get_tag(key="dagster/schedule_name")
            },
        ),
        limit=1,
    )

    last_run = runs[0] if runs else None
    if last_run:
        return last_run.create_timestamp.astimezone(LOCAL_TIMEZONE)
    else:
        return TODAY
🙌 1
y

yuhan

05/19/2022, 12:10 AM
looks great to me!
2 Views