aind-codeocean-dispatch 0.4.0__py3-none-any.whl → 0.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aind_codeocean_dispatch/__init__.py +3 -0
- aind_codeocean_dispatch/automation.py +159 -0
- {aind_codeocean_dispatch-0.4.0.dist-info → aind_codeocean_dispatch-0.5.0.dist-info}/METADATA +18 -1
- aind_codeocean_dispatch-0.5.0.dist-info/RECORD +10 -0
- aind_codeocean_dispatch-0.4.0.dist-info/RECORD +0 -9
- {aind_codeocean_dispatch-0.4.0.dist-info → aind_codeocean_dispatch-0.5.0.dist-info}/WHEEL +0 -0
- {aind_codeocean_dispatch-0.4.0.dist-info → aind_codeocean_dispatch-0.5.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from importlib.metadata import PackageNotFoundError, version
|
|
4
4
|
|
|
5
|
+
from .automation import capture_finished_run, read_only_retries
|
|
5
6
|
from .capsule_git import capsule_head_commit, head_commit, run_owner_email
|
|
6
7
|
from .capture import capture_as, capture_named_by_data_description, derived_name
|
|
7
8
|
from .runner import (
|
|
@@ -28,6 +29,7 @@ __all__ = [
|
|
|
28
29
|
"Outcome",
|
|
29
30
|
"capsule_head_commit",
|
|
30
31
|
"capture_as",
|
|
32
|
+
"capture_finished_run",
|
|
31
33
|
"capture_named_by_data_description",
|
|
32
34
|
"derived_name",
|
|
33
35
|
"failed_without_results",
|
|
@@ -35,6 +37,7 @@ __all__ = [
|
|
|
35
37
|
"job_key",
|
|
36
38
|
"key_tag",
|
|
37
39
|
"raise_for_failures",
|
|
40
|
+
"read_only_retries",
|
|
38
41
|
"run_jobs",
|
|
39
42
|
"run_owner_email",
|
|
40
43
|
"succeeded",
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"""Capture a finished run as an internal data asset, from a Code Ocean post-run automation.
|
|
2
|
+
|
|
3
|
+
Code Ocean runs a pipeline's automation capsule after every run, passing the run as
|
|
4
|
+
``CO_SOURCE_COMPUTATION_ID`` and ``CO_SOURCE_EXIT_CODE``. A capsule whose ``code/run`` is
|
|
5
|
+
``python -m aind_codeocean_dispatch.automation`` captures that run whatever its results
|
|
6
|
+
hold: the name comes from a derived ``data_description.json`` when one declares it, and
|
|
7
|
+
otherwise from the first input asset and the capture time. It writes no external bucket
|
|
8
|
+
and no DocDB record, so it needs only a Code Ocean token, and it is no substitute for a
|
|
9
|
+
publishing automation that checks metadata before registering it.
|
|
10
|
+
|
|
11
|
+
Configured by the capsule's environment:
|
|
12
|
+
|
|
13
|
+
- ``CODEOCEAN_TOKEN`` (required) and ``CODEOCEAN_DOMAIN``.
|
|
14
|
+
- ``CAPTURE_TAGS``: comma-separated tags for the asset.
|
|
15
|
+
- ``CAPTURE_FAILED``: truthy to capture failed runs that left results.
|
|
16
|
+
- ``CAPTURE_PERMISSIONS``: a ``Permissions`` object as JSON, applied once the asset is Ready.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import json
|
|
22
|
+
import logging
|
|
23
|
+
import os
|
|
24
|
+
import time
|
|
25
|
+
from collections.abc import Sequence
|
|
26
|
+
from typing import TYPE_CHECKING
|
|
27
|
+
|
|
28
|
+
from aind_code_ocean_pipeline_utils import parse_truthy
|
|
29
|
+
from codeocean import CodeOcean
|
|
30
|
+
from codeocean.components import Permissions
|
|
31
|
+
from codeocean.computation import Computation
|
|
32
|
+
from codeocean.data_asset import DataAsset, DataAssetState
|
|
33
|
+
from urllib3.util import Retry
|
|
34
|
+
|
|
35
|
+
from .capture import capture_named_by_data_description
|
|
36
|
+
from .runner import succeeded
|
|
37
|
+
|
|
38
|
+
if TYPE_CHECKING:
|
|
39
|
+
from collections.abc import Mapping
|
|
40
|
+
|
|
41
|
+
__all__ = ["capture_finished_run", "main", "read_only_retries"]
|
|
42
|
+
|
|
43
|
+
logger = logging.getLogger(__name__)
|
|
44
|
+
|
|
45
|
+
DEFAULT_DOMAIN = "https://codeocean.allenneuraldynamics.org"
|
|
46
|
+
|
|
47
|
+
# Indirected so tests need not wait.
|
|
48
|
+
_sleep = time.sleep
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def read_only_retries() -> Retry:
|
|
52
|
+
"""Retry 429 and 5xx on GET only; a resent ``create_data_asset`` could duplicate the asset."""
|
|
53
|
+
return Retry(
|
|
54
|
+
total=5,
|
|
55
|
+
backoff_factor=2,
|
|
56
|
+
status_forcelist=[429, 500, 502, 503, 504],
|
|
57
|
+
allowed_methods=frozenset({"GET"}),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def capture_finished_run(
|
|
62
|
+
client: CodeOcean,
|
|
63
|
+
computation_id: str,
|
|
64
|
+
exit_code: int,
|
|
65
|
+
*,
|
|
66
|
+
tags: Sequence[str] = (),
|
|
67
|
+
capture_failed: bool = False,
|
|
68
|
+
permissions: Permissions | None = None,
|
|
69
|
+
poll_interval_s: float = 60.0,
|
|
70
|
+
) -> DataAsset | None:
|
|
71
|
+
"""Capture *computation_id*'s results and wait until the asset is Ready.
|
|
72
|
+
|
|
73
|
+
Parameters
|
|
74
|
+
----------
|
|
75
|
+
client : CodeOcean
|
|
76
|
+
Authenticated client; see :func:`read_only_retries` for a safe ``retries``.
|
|
77
|
+
computation_id, exit_code
|
|
78
|
+
The finished run, as Code Ocean passes it to the automation.
|
|
79
|
+
tags : Sequence[str], optional
|
|
80
|
+
Tags for the asset.
|
|
81
|
+
capture_failed : bool, optional
|
|
82
|
+
Capture a failed run too, provided it left results.
|
|
83
|
+
permissions : Permissions, optional
|
|
84
|
+
Applied once the asset is Ready; ``None`` leaves Code Ocean's default.
|
|
85
|
+
poll_interval_s : float, optional
|
|
86
|
+
Seconds between checks of the asset's state.
|
|
87
|
+
|
|
88
|
+
Returns
|
|
89
|
+
-------
|
|
90
|
+
DataAsset or None
|
|
91
|
+
The Ready asset, or ``None`` when the run was skipped.
|
|
92
|
+
|
|
93
|
+
Raises
|
|
94
|
+
------
|
|
95
|
+
RuntimeError
|
|
96
|
+
If the capture ends in the Failed state.
|
|
97
|
+
"""
|
|
98
|
+
source = client.computations.get_computation(computation_id)
|
|
99
|
+
if not (exit_code == 0 and succeeded(source)) and not capture_failed:
|
|
100
|
+
logger.info("computation %s did not succeed (exit code %d); not capturing", computation_id, exit_code)
|
|
101
|
+
return None
|
|
102
|
+
if source.has_results is False:
|
|
103
|
+
logger.info("computation %s left no results; nothing to capture", computation_id)
|
|
104
|
+
return None
|
|
105
|
+
params = capture_named_by_data_description(client, _fallback_name(client, source), tags)(source)
|
|
106
|
+
created = client.data_assets.create_data_asset(params)
|
|
107
|
+
logger.info("capturing computation %s as %s (%s)", computation_id, created.id, params.name)
|
|
108
|
+
ready = _wait_until_settled(client, created.id, poll_interval_s)
|
|
109
|
+
if ready.state == DataAssetState.Failed:
|
|
110
|
+
raise RuntimeError(f"capture {created.id} of computation {computation_id} failed")
|
|
111
|
+
if permissions is not None:
|
|
112
|
+
client.data_assets.update_permissions(ready.id, permissions)
|
|
113
|
+
logger.info("captured computation %s as %s", computation_id, ready.id)
|
|
114
|
+
return ready
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _fallback_name(client: CodeOcean, source: Computation) -> str:
|
|
118
|
+
"""Name a capture after the run's first input asset, or the run itself if it has none."""
|
|
119
|
+
if source.data_assets:
|
|
120
|
+
try:
|
|
121
|
+
return str(client.data_assets.get_data_asset(source.data_assets[0].id).name)
|
|
122
|
+
except Exception as exc: # noqa: BLE001 - the name is cosmetic; the capture is not
|
|
123
|
+
logger.warning("could not read input asset %s: %s", source.data_assets[0].id, exc)
|
|
124
|
+
return str(source.name)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _wait_until_settled(client: CodeOcean, asset_id: str, poll_interval_s: float) -> DataAsset:
|
|
128
|
+
while True:
|
|
129
|
+
asset = client.data_assets.get_data_asset(asset_id)
|
|
130
|
+
if asset.state in (DataAssetState.Ready, DataAssetState.Failed):
|
|
131
|
+
return asset
|
|
132
|
+
_sleep(poll_interval_s)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _client_from_env(env: Mapping[str, str]) -> CodeOcean:
|
|
136
|
+
return CodeOcean(
|
|
137
|
+
domain=env.get("CODEOCEAN_DOMAIN", DEFAULT_DOMAIN),
|
|
138
|
+
token=env["CODEOCEAN_TOKEN"],
|
|
139
|
+
retries=read_only_retries(),
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def main(env: Mapping[str, str] | None = None) -> None:
|
|
144
|
+
"""Capture the run Code Ocean names in the environment; see the module docstring."""
|
|
145
|
+
env = os.environ if env is None else env
|
|
146
|
+
logging.basicConfig(level=env.get("LOG_LEVEL", "INFO"))
|
|
147
|
+
raw_permissions = env.get("CAPTURE_PERMISSIONS")
|
|
148
|
+
capture_finished_run(
|
|
149
|
+
_client_from_env(env),
|
|
150
|
+
env["CO_SOURCE_COMPUTATION_ID"],
|
|
151
|
+
int(env["CO_SOURCE_EXIT_CODE"]),
|
|
152
|
+
tags=[tag.strip() for tag in env.get("CAPTURE_TAGS", "").split(",") if tag.strip()],
|
|
153
|
+
capture_failed=parse_truthy(env.get("CAPTURE_FAILED", "")),
|
|
154
|
+
permissions=Permissions.from_dict(json.loads(raw_permissions)) if raw_permissions else None,
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
if __name__ == "__main__":
|
|
159
|
+
main()
|
{aind_codeocean_dispatch-0.4.0.dist-info → aind_codeocean_dispatch-0.5.0.dist-info}/METADATA
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: aind-codeocean-dispatch
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: Launch, monitor, and capture batches of Code Ocean capsule runs
|
|
5
5
|
Project-URL: Repository, https://github.com/AllenNeuralDynamics/aind-codeocean-dispatch/
|
|
6
6
|
Project-URL: Issues, https://github.com/AllenNeuralDynamics/aind-codeocean-dispatch/issues
|
|
@@ -48,6 +48,23 @@ To develop the code, run:
|
|
|
48
48
|
uv sync
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
+
## Capturing runs from a post-run automation
|
|
52
|
+
|
|
53
|
+
A Code Ocean pipeline can name an automation capsule that runs after every run. A capsule
|
|
54
|
+
whose `code/run` is
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
#!/usr/bin/env bash
|
|
58
|
+
set -e
|
|
59
|
+
python -m aind_codeocean_dispatch.automation
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
captures each successful run as an internal data asset, named by the results'
|
|
63
|
+
`data_description.json` when it declares a derived name and otherwise after the first
|
|
64
|
+
input asset. It needs only `CODEOCEAN_TOKEN`; `CAPTURE_TAGS`, `CAPTURE_FAILED` and
|
|
65
|
+
`CAPTURE_PERMISSIONS` adjust it. It registers nothing in DocDB and writes no external
|
|
66
|
+
bucket, so pipelines that publish use `asset-creation-capsule` instead.
|
|
67
|
+
|
|
51
68
|
## Development
|
|
52
69
|
|
|
53
70
|
Please test your changes using the full linting and testing suite:
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
aind_codeocean_dispatch/__init__.py,sha256=LLDHa2K8ChYN1PeeflQJhb3ONuK9Wt8T7oDX1ZtHcT4,1159
|
|
2
|
+
aind_codeocean_dispatch/automation.py,sha256=_7yPA8dJee2zLOY1vcPhdfFLoqDtiNY60hdz6INj11Q,6120
|
|
3
|
+
aind_codeocean_dispatch/capsule_git.py,sha256=BnUiGY1blXAFqnNAztBbYX0h5U5Mlsm5M2sFsOa4ICk,2762
|
|
4
|
+
aind_codeocean_dispatch/capture.py,sha256=aj3dgLNtGRmKhUbVZeIcPjfbPP42YYiz6gwMGwZSlAg,3506
|
|
5
|
+
aind_codeocean_dispatch/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
6
|
+
aind_codeocean_dispatch/runner.py,sha256=heg62GPc8gx7loOicFgY1K46aP6jC_5jky4_8K_IzHI,29767
|
|
7
|
+
aind_codeocean_dispatch-0.5.0.dist-info/METADATA,sha256=wMZTyME-CGXIuy9kxLfeqcxDE3hcX0Eru2SAN4DeI9k,3445
|
|
8
|
+
aind_codeocean_dispatch-0.5.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
9
|
+
aind_codeocean_dispatch-0.5.0.dist-info/licenses/LICENSE,sha256=zMZxYbR1i_XxgyJP_rPSWKcxY7QtyxUOzbIpowHNjCs,1093
|
|
10
|
+
aind_codeocean_dispatch-0.5.0.dist-info/RECORD,,
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
aind_codeocean_dispatch/__init__.py,sha256=iWzsXeODLf8-XLxMDQl2FNT351Usrl-Nd11-zLdiJSc,1042
|
|
2
|
-
aind_codeocean_dispatch/capsule_git.py,sha256=BnUiGY1blXAFqnNAztBbYX0h5U5Mlsm5M2sFsOa4ICk,2762
|
|
3
|
-
aind_codeocean_dispatch/capture.py,sha256=aj3dgLNtGRmKhUbVZeIcPjfbPP42YYiz6gwMGwZSlAg,3506
|
|
4
|
-
aind_codeocean_dispatch/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
5
|
-
aind_codeocean_dispatch/runner.py,sha256=heg62GPc8gx7loOicFgY1K46aP6jC_5jky4_8K_IzHI,29767
|
|
6
|
-
aind_codeocean_dispatch-0.4.0.dist-info/METADATA,sha256=dki-UD71tzNI4GFQ6YQpQfJtbsuH5d9tTXj2OCAxrTQ,2797
|
|
7
|
-
aind_codeocean_dispatch-0.4.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
8
|
-
aind_codeocean_dispatch-0.4.0.dist-info/licenses/LICENSE,sha256=zMZxYbR1i_XxgyJP_rPSWKcxY7QtyxUOzbIpowHNjCs,1093
|
|
9
|
-
aind_codeocean_dispatch-0.4.0.dist-info/RECORD,,
|
|
File without changes
|
{aind_codeocean_dispatch-0.4.0.dist-info → aind_codeocean_dispatch-0.5.0.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|