Conversation
* centralize on one get_run() * use dotenv for Tiled API key
There was a problem hiding this comment.
Pull request overview
This PR moves Tiled access toward a from_uri-based approach and starts threading a Tiled API key through Prefect flows/tasks to enable running workflows outside NSLS-II profile-based infrastructure.
Changes:
- Added
get_run()(Tiledfrom_uri) and updated workflows/exporters to fetch runs via this helper and accept anapi_keyparameter. - Removed Tiled profile mounts/env from the Prefect Docker deployment configuration.
- Updated end-of-run workflow to pass
api_keythrough to downstream exporters/logging.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
data_validation.py |
Adds from_uri-based get_run() and updates validation flow to use it. |
xrf_hdf5_exporter.py |
Switches run loading to get_run() and adds api_key plumb-through. |
xanes_exporter.py |
Switches run loading to get_run() and adds api_key parameters across exporters. |
logscan.py |
Starts switching to get_run() and adds api_key parameters (but still has profile/Secret globals). |
end_of_run_workflow.py |
Uses get_run() and passes api_key to downstream flows. |
prefect.yaml |
Removes Tiled profile env/volume from the deployment container configuration. |
Comments suppressed due to low confidence (3)
logscan.py:86
logscan()acceptsapi_keybut doesn't pass it intologscan_detailed(), so the nested call will run withapi_key=Noneand may fail to authenticate against Tiled. Thread theapi_keyargument through (logscan_detailed(ref, api_key=api_key)).
def logscan(ref, api_key=None):
logger = get_run_logger()
logger.info("Start writing logfile...")
logscan_detailed(ref)
logger.info("Finish writing logfile.")
logscan.py:11
- This module still initializes
Secret.load(...)andfrom_profile(...)at import time (and definestiled_client_raw) even though the actual code path now usesget_run(...)instead. With the Prefect deployment no longer mounting/setting Tiled profiles, this import-timefrom_profile('nsls2', ...)is likely to fail before the flow runs. Remove these globals (and theSecret/from_profileimports) or migrate them tofrom_uriand only construct clients inside functions when needed.
from prefect.blocks.system import Secret
from tiled.client import from_profile
from data_validation import get_run
api_key = Secret.load("tiled-srx-api-key", _sync=True).get()
tiled_client = from_profile("nsls2", api_key=api_key)["srx"]
tiled_client_raw = tiled_client["raw"]
xanes_exporter.py:340
xanes_exporter()acceptsapi_keyand uses it to determinescan_type, but it does not passapi_keyintoxas_step_exporter()/xas_fly_exporter(). As a result those tasks will run withapi_key=Noneand may fail when they callget_run(...). Passapi_keythrough to the selected exporter task.
def xanes_exporter(ref, api_key=None):
logger = get_run_logger()
logger.info("Start writing file with xanes_exporter...")
# Get scan type
scan_type = (
get_run(ref, api_key=api_key).start.get("scan", {}).get("type", "unknown")
)
# Redirect to correction function - or pass
if scan_type == "XAS_STEP":
logger.info("Starting xanes step-scan exporter.")
xas_step_exporter(ref)
logger.info("Finished writing file with xanes step-scan exporter.")
elif scan_type == "XAS_FLY":
logger.info("Starting xanes fly-scan exporter.")
xas_fly_exporter(ref)
logger.info("Finished writing file with xanes fly-scan exporter.")
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
xrf_hdf5_exporter.py
Outdated
| def export_xrf_hdf5(scanid, api_key=None): | ||
| logger = get_run_logger() | ||
|
|
||
| logger.info(f"{pyxrf.__file__ = }") | ||
|
|
||
| logger.info(f"{dask.__file__ = }") | ||
|
|
||
| # Load header for our scan | ||
| h = tiled_client_raw[scanid] | ||
| h = get_run(scanid, api_key=api_key) |
There was a problem hiding this comment.
api_key is now optional, but this task later assigns it into os.environ['TILED_API_KEY']. If api_key is None, os.environ will raise a TypeError (env values must be strings) and the export will fail. Either require api_key (raise a clear error if missing) or guard the env assignment and/or fall back to reading the key from the environment.
end_of_run_workflow.py
Outdated
| @flow | ||
| @slack | ||
| def end_of_run_workflow(stop_doc): | ||
| def end_of_run_workflow(stop_doc, api_key=None): |
There was a problem hiding this comment.
The @slack decorator wraps the flow with an inner end_of_run_workflow(stop_doc) that does not accept/forward api_key. With the flow signature now including api_key, calling the flow with api_key=... will raise TypeError: unexpected keyword argument. Update the decorator wrapper to accept *args, **kwargs and pass them through to func(*args, **kwargs) (and ideally use functools.wraps to preserve the signature).
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* unused code to get Tiled client - replace by using get_run() from data_validation()
* output more info for xas_fly_exporter as it seems to have a better structure
Update toward the template repo: