Closed marvin-robot closed 2 years ago
No need to read the above. This is the page with the wrong code snippet: https://docs.prefect.io/api/latest/utilities/context.html
The same link says that Prefect Cloud supplies the following..
, making it seem like Server does not.
Opened from the Prefect Public Slack Community
kpweiler: Hi there - I have a flow where I would like to (at flow run time) get a date (as a string) from a function defined in my codebase and provide it to ALL of the tasks in the flow. This sounds like a Parameter, but there are a couple problems with this:
kevin701: Hey <@UMSQA1W8Z>, you can:
kpweiler: thanks <@U01QEJ9PP53> - if I do #1 - every task that needs this date will have a graph connection the the parameter and the downstream task right?
kevin701: Context has `prefect.context.get(“today”). See https://docs.prefect.io/api/latest/utilities/context.html|this .
kevin701: Yep that’s right
kpweiler: ok - I think i’ll give the script storage a try - I’m using Docker Storage so it looks like that is supported
kpweiler: that’s gonna need a big refactor I guess though because I’m currently using the imperative API and some builder functions to build the flow. I guess I could generate the script with a Jinja Template
kevin701: Maybe pulling it from context is the easiest then?
kpweiler: yeah - having a look
The story here is that we have a flow we want to run once per day, but it might take longer than a day to complete. I could tell each individual task to ask what day it is - but if it goes past 24 hours - it’s going to run flow steps with the wrong date
kevin701: The context won’t change once the flow is running, but there is also
scheduled_start_time
kpweiler: Does this context time return in the system’s time zone or UTC?
kevin701: Ah good question. It might be UTC. Let me check
kevin701: It is UTC
kpweiler: ok - I actually think it’s a moot point because we’ll be running at 4PM Chicago time - which is variously 21 or 22 UTC, but the same date
kpweiler: if you re-run a flow from failed, do you get a new context?
kevin701: all of the fields are the same except the ones around timestamps which change.
scheduled_start_time
would be the same thoughkevin701: https://github.com/PrefectHQ/prefect/blob/master/src/prefect/engine/flow_runner.py#L180-L189|this is the list of things that change
kpweiler: cool yeah, so
scheduled_start_time
it iskpweiler: somewhat separately - is this documentation correct? https://docs.prefect.io/api/latest/utilities/context.html
I can’t seem to import
prefect.context
…kpweiler: I’m on
0.14.7
but the0.14.22
documentation looks to be the samekevin701: You
import prefect
and theninside the task
kpweiler: do you do the
import
in the task itself also?kevin701: no need. import at the top of the script.
kevin701:
kpweiler: yeah - i think that doc is incorrect - you can’t
just an FYI - nbd
kevin701: Ah crud I see your point.
kevin701: <@ULVA73B9P> open “Context documentation incorrectly has ‘import prefect.context’ which is invalid”
Original thread can be found here.