Hey team, does anybody know if it's possible to fi...
# ask-community
n
Hey team, does anybody know if it's possible to find out what worker a flow run is running on from within the flow itself?? We can't manage to see anything relevant in the prefect client, but maybe we're missing something, any help would be awesome cheers
n
hmm there's probably a (somewhat indirect) way to do this using the event that gets emitted when a worker submits a flow run, but what are you trying to do once you find out that worker? perhaps there's a simpler way than that
n
Ahh sorry we're not using prefect cloud, so don't have access to events. We basically have several workers scattered across different hosts, all we want is to know where the flow is running at runtime
n
ah i see. what about setting some unique env var on the host (corresponding to the worker) that you could read from at runtime?
n
I was looking at this earlier - is there a way to make environment variables on the workers host available at runtime?? We're using docker work pools, so the only way I can see for setting env variables is through the work pool itself, which complicates things since we have multiple workers per work pool
n
yeah you can do something like this
Copy code
# global pull step (implicitly applied to deployments that dont have one)
pull:
- prefect.deployments.steps.git_clone:
    repository: <https://github.com/zzstoatzz/prefect-monorepo>
    branch: main

deployments:
- name: host-env-test
  entrypoint: src/demo_project/read_env.py:read_env
  work_pool:
    name: docker-work
    job_variables:
      image: "{{ build_image.image }}"
      env: 
        MAMBA_ROOT_PREFIX: "{{ $MAMBA_ROOT_PREFIX }}"
where
Copy code
src/demo_project/read_env.py:read_env
is just
Copy code
import os

from prefect import flow


@flow(log_prints=True)
def read_env():
    print(os.environ["MAMBA_ROOT_PREFIX"])
and I'm running a docker worker
Copy code
prefect worker start -p docker-work
with docker desktop on my machine, where my machine has this
MAMBA_ROOT_PREFIX
env var so the worker logs look like
Copy code
18:28:48.067 | INFO    | prefect.flow_runs.worker - Completed submission of flow run 'xxx'
00:28:48.898 | INFO    | prefect.flow_runs.runner - Opening process...
00:28:50.140 | INFO    | prefect.deployment - Cloned repository '<https://github.com/zzstoatzz/prefect-monorepo>' into 'prefect-monorepo-main'
00:28:50.517 | INFO    | Flow run 'devout-butterfly' - /Users/nate/micromamba
00:28:51.254 | INFO    | Flow run 'devout-butterfly' - Finished in state Completed()
00:28:51.703 | INFO    | prefect.flow_runs.runner - Process for flow run 'devout-butterfly' exited cleanly
the actual repo if you're interested
hmm actually I just realized im not sure this is exactly what you want, bc the env var is coming from my machine at deployment time, which is probably not the machine running the worker in your case, but hopefully its still helpful
n
Thanks for this, but yeah your last message is right I think
Is there any way we can map environment variables from the host to the docker container at runtime?? I know we can do it via the work pool but that doesn't really help here
If not, is there any capacity to add this sort of functionality to either a) the
prefect worker start
command, or b) via the docker work pool type??
n
hmm yeah sorry for the birdwalk
Is there any way we can map environment variables from the host to the docker container at runtime?? I know we can do it via the work pool but that doesn't really help here
off the top im not sure, I can look more at this later. but if not, that sounds like a reasonable feature request on prefect-docker
n
Sounds good, thanks for getting back to me here. If nothing comes to mind let me know and I'll fill one out
👍 1