aind-codeocean-dispatch 0.5.0__tar.gz → 0.7.0__tar.gz
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-0.5.0 → aind_codeocean_dispatch-0.7.0}/.copier-answers.yml +1 -1
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/CHANGELOG.md +12 -0
- aind_codeocean_dispatch-0.7.0/CLAUDE.md +10 -0
- aind_codeocean_dispatch-0.7.0/CONTRIBUTING.md +61 -0
- aind_codeocean_dispatch-0.7.0/PKG-INFO +182 -0
- aind_codeocean_dispatch-0.7.0/README.md +162 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/pyproject.toml +17 -2
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/src/aind_codeocean_dispatch/__init__.py +2 -0
- aind_codeocean_dispatch-0.7.0/src/aind_codeocean_dispatch/diagnostics.py +118 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/src/aind_codeocean_dispatch/runner.py +167 -16
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/tests/fakes.py +52 -4
- aind_codeocean_dispatch-0.7.0/tests/test_diagnostics.py +110 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/tests/test_runner.py +195 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/uv.lock +246 -4
- aind_codeocean_dispatch-0.5.0/CLAUDE.md +0 -48
- aind_codeocean_dispatch-0.5.0/PKG-INFO +0 -90
- aind_codeocean_dispatch-0.5.0/README.md +0 -73
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/.github/ISSUE_TEMPLATE/user-story.md +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/.github/dependabot.yml +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/.github/workflows/bump-call.yml +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/.github/workflows/ci-call.yml +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/.github/workflows/publish-call.yml +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/.gitignore +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/.python-version +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/LICENSE +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/scripts/run_linters_and_checks.sh +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/src/aind_codeocean_dispatch/automation.py +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/src/aind_codeocean_dispatch/capsule_git.py +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/src/aind_codeocean_dispatch/capture.py +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/src/aind_codeocean_dispatch/py.typed +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/tests/__init__.py +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/tests/test_automation.py +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/tests/test_capsule_git.py +0 -0
- {aind_codeocean_dispatch-0.5.0 → aind_codeocean_dispatch-0.7.0}/tests/test_capture.py +0 -0
|
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## v0.7.0 (2026-12-01)
|
|
9
|
+
|
|
10
|
+
### Feat
|
|
11
|
+
|
|
12
|
+
- preserve structured worker diagnostics in dispatch outcomes (#4)
|
|
13
|
+
|
|
14
|
+
## v0.6.0 (2026-10-21)
|
|
15
|
+
|
|
16
|
+
### Feat
|
|
17
|
+
|
|
18
|
+
- report each job's outcome as it finishes and resume running computations by key
|
|
19
|
+
|
|
8
20
|
## v0.5.0 (2026-10-15)
|
|
9
21
|
|
|
10
22
|
### Feat
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
Instructions for coding agents in this repository. [CONTRIBUTING.md](CONTRIBUTING.md) holds setup,
|
|
4
|
+
checks, tests and conventions, and applies to agents as to people: read it before running or
|
|
5
|
+
changing anything. [README.md](README.md) documents use.
|
|
6
|
+
|
|
7
|
+
- Run every command through `uv`; never bare `pip` or `python`.
|
|
8
|
+
- Before calling a change done, run `./scripts/run_linters_and_checks.sh -c`.
|
|
9
|
+
- Keep this file to what only an agent needs; development practice belongs in CONTRIBUTING.md and
|
|
10
|
+
usage in README.md.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Contributing to AIND Code Ocean dispatch
|
|
2
|
+
|
|
3
|
+
This document is for people changing aind-codeocean-dispatch: setting up, running the checks and tests,
|
|
4
|
+
and the conventions a change follows. Using the package is documented in [README.md](README.md).
|
|
5
|
+
|
|
6
|
+
## Setup
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
uv sync
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
`uv sync` creates `.venv` holding the package and the dev group. `uv` manages the environment and
|
|
13
|
+
the lockfile, `uv.lock`: run commands with `uv run`, add a dependency with `uv add <package>` and a
|
|
14
|
+
development tool with `uv add --group dev <package>`, and never install with bare `pip`. The
|
|
15
|
+
jupyter group adds a kernel (`uv sync --group jupyter`).
|
|
16
|
+
|
|
17
|
+
## Checks
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
./scripts/run_linters_and_checks.sh -c
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Without `-c` the script only formats. With it, the script also runs ruff's linter, mypy, interrogate,
|
|
24
|
+
codespell and pytest with coverage, passing any arguments after `--` to pytest. It runs in the
|
|
25
|
+
existing `.venv` and does not sync it.
|
|
26
|
+
|
|
27
|
+
The same checks, one at a time:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
uv run --frozen ruff format
|
|
31
|
+
uv run --frozen ruff check
|
|
32
|
+
uv run --frozen mypy
|
|
33
|
+
uv run --frozen interrogate -v src
|
|
34
|
+
uv run --frozen codespell --check-filenames
|
|
35
|
+
uv run --frozen pytest --cov aind_codeocean_dispatch
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Tests
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
uv run pytest # the whole suite
|
|
42
|
+
uv run pytest tests/test_example.py # one file
|
|
43
|
+
uv run pytest -k "version" # tests whose names match
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Conventions
|
|
47
|
+
|
|
48
|
+
- Source lives in `src/aind_codeocean_dispatch/` and tests in `tests/`; hatchling builds the
|
|
49
|
+
wheel.
|
|
50
|
+
- ruff sets a line length of 120 and numpy-style docstrings, and exempts tests from the
|
|
51
|
+
docstring and annotation rules. interrogate fails below 30%
|
|
52
|
+
docstring coverage of `src`.
|
|
53
|
+
- mypy checks `src/aind_codeocean_dispatch` in strict mode.
|
|
54
|
+
- Comments and docstrings say what the code does not: why it is written this way, and what breaks
|
|
55
|
+
otherwise. They carry no history; the reason for a change belongs in its commit message.
|
|
56
|
+
- `scratch/` is gitignored and excluded from the linters, for work that is not kept.
|
|
57
|
+
- Commit messages follow [Conventional Commits](https://www.conventionalcommits.org/) (`feat:`,
|
|
58
|
+
`fix:`, `docs:`, `chore:`): an imperative title, and a body giving what changed and why. After CI
|
|
59
|
+
passes on `main`, the bump workflow uses commitizen to bump the version from them and update
|
|
60
|
+
`CHANGELOG.md`.
|
|
61
|
+
- A change a user would notice updates README.md in the same commit.
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: aind-codeocean-dispatch
|
|
3
|
+
Version: 0.7.0
|
|
4
|
+
Summary: Launch, monitor, and capture batches of Code Ocean capsule runs
|
|
5
|
+
Project-URL: Repository, https://github.com/AllenNeuralDynamics/aind-codeocean-dispatch/
|
|
6
|
+
Project-URL: Issues, https://github.com/AllenNeuralDynamics/aind-codeocean-dispatch/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/AllenNeuralDynamics/aind-codeocean-dispatch/blob/main/CHANGELOG.md
|
|
8
|
+
Author: Allen Institute for Neural Dynamics
|
|
9
|
+
Author-email: Galen Lynch <galen@galenlynch.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.11
|
|
14
|
+
Requires-Dist: aind-code-ocean-pipeline-utils>=0.7.1
|
|
15
|
+
Requires-Dist: codeocean>=0.13
|
|
16
|
+
Provides-Extra: diagnostics
|
|
17
|
+
Requires-Dist: aind-code-ocean-pipeline-utils[diagnostics]<1,>=0.10.0; extra == 'diagnostics'
|
|
18
|
+
Requires-Dist: aind-diagnostics<1,>=0.2.0; extra == 'diagnostics'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# AIND Code Ocean dispatch
|
|
22
|
+
|
|
23
|
+

|
|
24
|
+
[](https://pypi.org/project/aind-codeocean-dispatch/)
|
|
25
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
26
|
+
[](LICENSE)
|
|
27
|
+
[](https://github.com/astral-sh/ruff)
|
|
28
|
+
[](https://github.com/astral-sh/uv)
|
|
29
|
+
[](https://github.com/copier-org/copier)
|
|
30
|
+
|
|
31
|
+
Launch, monitor, and capture batches of Code Ocean capsule runs
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
If you choose to clone the repository, you can install the package by running the following command from the root directory of the repository:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install .
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Otherwise, you can use pip:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install aind-codeocean-dispatch
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
### Running a batch of runs
|
|
50
|
+
|
|
51
|
+
`run_jobs` launches a batch of capsule or pipeline runs, waits for them from one polling
|
|
52
|
+
loop, and captures each successful run as a data asset:
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from codeocean import CodeOcean
|
|
56
|
+
from codeocean.computation import DataAssetsRunParam, NamedRunParam, RunParams
|
|
57
|
+
|
|
58
|
+
from aind_codeocean_dispatch import Job, capture_as, job_key, raise_for_failures, run_jobs
|
|
59
|
+
|
|
60
|
+
client = CodeOcean(domain="https://codeocean.allenneuraldynamics.org", token=token)
|
|
61
|
+
|
|
62
|
+
jobs = []
|
|
63
|
+
for stream in streams:
|
|
64
|
+
params = RunParams(
|
|
65
|
+
capsule_id=WORKER_CAPSULE_ID,
|
|
66
|
+
data_assets=[DataAssetsRunParam(id=raw_asset_id, mount="raw")],
|
|
67
|
+
named_parameters=[NamedRunParam(param_name="stream", value=stream)],
|
|
68
|
+
)
|
|
69
|
+
jobs.append(
|
|
70
|
+
Job(
|
|
71
|
+
label=stream,
|
|
72
|
+
run_params=params,
|
|
73
|
+
capture=capture_as(f"{session}_{stream}_sorted", tags=["sorted"]),
|
|
74
|
+
key=job_key(params),
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
outcomes = run_jobs(client, jobs, launch_interval_s=1, start_retries=3, reuse=True)
|
|
79
|
+
raise_for_failures(outcomes)
|
|
80
|
+
asset_ids = {o.job.label: o.asset_id for o in outcomes}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
A failed job does not raise; each `Outcome` carries its `status`, `asset_id`, every
|
|
84
|
+
`computation_ids` attempt, and on failure an `error` and a `failure` kind.
|
|
85
|
+
`raise_for_failures` turns failures into one `DispatchFailed`.
|
|
86
|
+
|
|
87
|
+
Behaviour worth knowing:
|
|
88
|
+
|
|
89
|
+
- **Failed starts.** Code Ocean sometimes fails to start a run (`end_status=Failed`, no
|
|
90
|
+
results). Up to `start_retries` relaunches follow, after `start_retry_backoff_s` (60 s)
|
|
91
|
+
doubling each time, and every pending launch waits too, since the cause is usually
|
|
92
|
+
machine capacity.
|
|
93
|
+
- **Rate limits and errors.** 429s are retried honouring `Retry-After`. Other 5xx and
|
|
94
|
+
connection errors are retried on reads only: a launch or capture POST may already have
|
|
95
|
+
taken effect, so resending it could duplicate the computation or asset.
|
|
96
|
+
- **Pacing.** `max_in_flight` caps concurrent runs and `launch_interval_s` spaces
|
|
97
|
+
launches; both are off by default.
|
|
98
|
+
- **Reuse.** A job's `key` is added to its asset as a tag. With `reuse=True`, a Ready
|
|
99
|
+
asset you created with that tag is returned instead of launching. `job_key` fingerprints
|
|
100
|
+
the launch request; pass `salt=` for what the request cannot show, such as the commit
|
|
101
|
+
of an unreleased capsule (`capsule_head_commit(client, capsule_id)` reads it inside a
|
|
102
|
+
run).
|
|
103
|
+
- **Acting on each job as it finishes.** `on_outcome=` is called with each job's
|
|
104
|
+
`Outcome` as soon as it is reached, whatever its status, so a caller can register or
|
|
105
|
+
report results without waiting for the whole batch. An exception it raises is logged
|
|
106
|
+
and does not affect the other jobs.
|
|
107
|
+
- **Resuming a batch.** A keyed job's computation is renamed to its label plus
|
|
108
|
+
`key_marker(key)`. With `resume_owner=` set to your Code Ocean user id or email, a
|
|
109
|
+
later batch follows your computation of the same job instead of launching it again:
|
|
110
|
+
one still running is waited on, one that succeeded but was never captured is
|
|
111
|
+
captured, and a capture still in progress is waited on. A succeeded computation that
|
|
112
|
+
already has a Ready capture is resumed only through `reuse`. Code Ocean lists every
|
|
113
|
+
user's computations of a capsule and has no call naming the caller, hence the
|
|
114
|
+
argument; it needs codeocean 0.15 or later.
|
|
115
|
+
- **Cancelling a doomed batch.** `cancel_on_failure=True`, or a predicate on the failed
|
|
116
|
+
`Outcome` such as `lambda o: not o.transient`, stops launching and stops and deletes
|
|
117
|
+
running computations; captures already under way finish.
|
|
118
|
+
- **Capture names.** `capture_as` uses a fixed name. `capture_named_by_data_description`
|
|
119
|
+
uses the name a derived `data_description.json` in the results declares, and otherwise
|
|
120
|
+
a fallback name plus the capture time.
|
|
121
|
+
|
|
122
|
+
### Capturing runs from a post-run automation
|
|
123
|
+
|
|
124
|
+
A Code Ocean pipeline can name an automation capsule that runs after every run. A capsule
|
|
125
|
+
whose `code/run` is
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
#!/usr/bin/env bash
|
|
129
|
+
set -e
|
|
130
|
+
python -m aind_codeocean_dispatch.automation
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
captures each successful run as an internal data asset, named by the results'
|
|
134
|
+
`data_description.json` when it declares a derived name and otherwise after the first
|
|
135
|
+
input asset. It needs only `CODEOCEAN_TOKEN`; `CAPTURE_TAGS`, `CAPTURE_FAILED` and
|
|
136
|
+
`CAPTURE_PERMISSIONS` adjust it. It registers nothing in DocDB and writes no external
|
|
137
|
+
bucket, so pipelines that publish use `asset-creation-capsule` instead.
|
|
138
|
+
|
|
139
|
+
## Contributing
|
|
140
|
+
|
|
141
|
+
Setup, checks, tests and conventions are in [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
146
|
+
|
|
147
|
+
## Worker diagnostics
|
|
148
|
+
|
|
149
|
+
Install `aind-codeocean-dispatch[diagnostics]` when workers emit structured
|
|
150
|
+
failure artifacts. The extra uses `aind-diagnostics` for the report contract and
|
|
151
|
+
`aind-code-ocean-pipeline-utils` for the shared artifact namespace.
|
|
152
|
+
|
|
153
|
+
Optional reporting dependencies load when the reporting helpers are used, so
|
|
154
|
+
importing the module also works with the base installation.
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from aind_codeocean_dispatch import raise_for_failures, run_jobs
|
|
158
|
+
from aind_codeocean_dispatch.diagnostics import diagnose_outcome
|
|
159
|
+
|
|
160
|
+
outcomes = run_jobs(client, jobs)
|
|
161
|
+
outcomes = [diagnose_outcome(client, item, stage="ephys", unit=item.job.label) for item in outcomes]
|
|
162
|
+
raise_for_failures(outcomes)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Supply the stage and unit that the worker used when saving its report; the
|
|
166
|
+
dispatcher does not infer capsule-specific parameter names. `diagnose_outcome`
|
|
167
|
+
adds `diagnostic_report` and a readable explanation to failed outcomes while
|
|
168
|
+
preserving their status, failure classification, and capture state. Successful
|
|
169
|
+
outcomes are unchanged. `DispatchFailed` combines attached reports and retains
|
|
170
|
+
the individual failures for per-worker facts and provenance.
|
|
171
|
+
|
|
172
|
+
`worker_failure` reads the small failure header and then its namespaced report.
|
|
173
|
+
It bounds downloads, validates the common schema and causal graph, accepts new
|
|
174
|
+
domain codes, and keeps the producer's summary when full evidence is missing,
|
|
175
|
+
malformed, or larger than 2 MiB. It returns `None` when no usable header exists,
|
|
176
|
+
so callers can retain their fallback for older workers. Result-file errors
|
|
177
|
+
cannot replace the original computation failure, and signed download URLs are
|
|
178
|
+
never included in the returned explanation.
|
|
179
|
+
|
|
180
|
+
For other required JSON artifacts, `result_json` provides the same bounded
|
|
181
|
+
reader but propagates retrieval errors. Domain facts such as manifest coverage
|
|
182
|
+
remain the caller's responsibility.
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# AIND Code Ocean dispatch
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://pypi.org/project/aind-codeocean-dispatch/)
|
|
5
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
[](https://github.com/astral-sh/ruff)
|
|
8
|
+
[](https://github.com/astral-sh/uv)
|
|
9
|
+
[](https://github.com/copier-org/copier)
|
|
10
|
+
|
|
11
|
+
Launch, monitor, and capture batches of Code Ocean capsule runs
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
If you choose to clone the repository, you can install the package by running the following command from the root directory of the repository:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install .
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Otherwise, you can use pip:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install aind-codeocean-dispatch
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### Running a batch of runs
|
|
30
|
+
|
|
31
|
+
`run_jobs` launches a batch of capsule or pipeline runs, waits for them from one polling
|
|
32
|
+
loop, and captures each successful run as a data asset:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from codeocean import CodeOcean
|
|
36
|
+
from codeocean.computation import DataAssetsRunParam, NamedRunParam, RunParams
|
|
37
|
+
|
|
38
|
+
from aind_codeocean_dispatch import Job, capture_as, job_key, raise_for_failures, run_jobs
|
|
39
|
+
|
|
40
|
+
client = CodeOcean(domain="https://codeocean.allenneuraldynamics.org", token=token)
|
|
41
|
+
|
|
42
|
+
jobs = []
|
|
43
|
+
for stream in streams:
|
|
44
|
+
params = RunParams(
|
|
45
|
+
capsule_id=WORKER_CAPSULE_ID,
|
|
46
|
+
data_assets=[DataAssetsRunParam(id=raw_asset_id, mount="raw")],
|
|
47
|
+
named_parameters=[NamedRunParam(param_name="stream", value=stream)],
|
|
48
|
+
)
|
|
49
|
+
jobs.append(
|
|
50
|
+
Job(
|
|
51
|
+
label=stream,
|
|
52
|
+
run_params=params,
|
|
53
|
+
capture=capture_as(f"{session}_{stream}_sorted", tags=["sorted"]),
|
|
54
|
+
key=job_key(params),
|
|
55
|
+
)
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
outcomes = run_jobs(client, jobs, launch_interval_s=1, start_retries=3, reuse=True)
|
|
59
|
+
raise_for_failures(outcomes)
|
|
60
|
+
asset_ids = {o.job.label: o.asset_id for o in outcomes}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
A failed job does not raise; each `Outcome` carries its `status`, `asset_id`, every
|
|
64
|
+
`computation_ids` attempt, and on failure an `error` and a `failure` kind.
|
|
65
|
+
`raise_for_failures` turns failures into one `DispatchFailed`.
|
|
66
|
+
|
|
67
|
+
Behaviour worth knowing:
|
|
68
|
+
|
|
69
|
+
- **Failed starts.** Code Ocean sometimes fails to start a run (`end_status=Failed`, no
|
|
70
|
+
results). Up to `start_retries` relaunches follow, after `start_retry_backoff_s` (60 s)
|
|
71
|
+
doubling each time, and every pending launch waits too, since the cause is usually
|
|
72
|
+
machine capacity.
|
|
73
|
+
- **Rate limits and errors.** 429s are retried honouring `Retry-After`. Other 5xx and
|
|
74
|
+
connection errors are retried on reads only: a launch or capture POST may already have
|
|
75
|
+
taken effect, so resending it could duplicate the computation or asset.
|
|
76
|
+
- **Pacing.** `max_in_flight` caps concurrent runs and `launch_interval_s` spaces
|
|
77
|
+
launches; both are off by default.
|
|
78
|
+
- **Reuse.** A job's `key` is added to its asset as a tag. With `reuse=True`, a Ready
|
|
79
|
+
asset you created with that tag is returned instead of launching. `job_key` fingerprints
|
|
80
|
+
the launch request; pass `salt=` for what the request cannot show, such as the commit
|
|
81
|
+
of an unreleased capsule (`capsule_head_commit(client, capsule_id)` reads it inside a
|
|
82
|
+
run).
|
|
83
|
+
- **Acting on each job as it finishes.** `on_outcome=` is called with each job's
|
|
84
|
+
`Outcome` as soon as it is reached, whatever its status, so a caller can register or
|
|
85
|
+
report results without waiting for the whole batch. An exception it raises is logged
|
|
86
|
+
and does not affect the other jobs.
|
|
87
|
+
- **Resuming a batch.** A keyed job's computation is renamed to its label plus
|
|
88
|
+
`key_marker(key)`. With `resume_owner=` set to your Code Ocean user id or email, a
|
|
89
|
+
later batch follows your computation of the same job instead of launching it again:
|
|
90
|
+
one still running is waited on, one that succeeded but was never captured is
|
|
91
|
+
captured, and a capture still in progress is waited on. A succeeded computation that
|
|
92
|
+
already has a Ready capture is resumed only through `reuse`. Code Ocean lists every
|
|
93
|
+
user's computations of a capsule and has no call naming the caller, hence the
|
|
94
|
+
argument; it needs codeocean 0.15 or later.
|
|
95
|
+
- **Cancelling a doomed batch.** `cancel_on_failure=True`, or a predicate on the failed
|
|
96
|
+
`Outcome` such as `lambda o: not o.transient`, stops launching and stops and deletes
|
|
97
|
+
running computations; captures already under way finish.
|
|
98
|
+
- **Capture names.** `capture_as` uses a fixed name. `capture_named_by_data_description`
|
|
99
|
+
uses the name a derived `data_description.json` in the results declares, and otherwise
|
|
100
|
+
a fallback name plus the capture time.
|
|
101
|
+
|
|
102
|
+
### Capturing runs from a post-run automation
|
|
103
|
+
|
|
104
|
+
A Code Ocean pipeline can name an automation capsule that runs after every run. A capsule
|
|
105
|
+
whose `code/run` is
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
#!/usr/bin/env bash
|
|
109
|
+
set -e
|
|
110
|
+
python -m aind_codeocean_dispatch.automation
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
captures each successful run as an internal data asset, named by the results'
|
|
114
|
+
`data_description.json` when it declares a derived name and otherwise after the first
|
|
115
|
+
input asset. It needs only `CODEOCEAN_TOKEN`; `CAPTURE_TAGS`, `CAPTURE_FAILED` and
|
|
116
|
+
`CAPTURE_PERMISSIONS` adjust it. It registers nothing in DocDB and writes no external
|
|
117
|
+
bucket, so pipelines that publish use `asset-creation-capsule` instead.
|
|
118
|
+
|
|
119
|
+
## Contributing
|
|
120
|
+
|
|
121
|
+
Setup, checks, tests and conventions are in [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
126
|
+
|
|
127
|
+
## Worker diagnostics
|
|
128
|
+
|
|
129
|
+
Install `aind-codeocean-dispatch[diagnostics]` when workers emit structured
|
|
130
|
+
failure artifacts. The extra uses `aind-diagnostics` for the report contract and
|
|
131
|
+
`aind-code-ocean-pipeline-utils` for the shared artifact namespace.
|
|
132
|
+
|
|
133
|
+
Optional reporting dependencies load when the reporting helpers are used, so
|
|
134
|
+
importing the module also works with the base installation.
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from aind_codeocean_dispatch import raise_for_failures, run_jobs
|
|
138
|
+
from aind_codeocean_dispatch.diagnostics import diagnose_outcome
|
|
139
|
+
|
|
140
|
+
outcomes = run_jobs(client, jobs)
|
|
141
|
+
outcomes = [diagnose_outcome(client, item, stage="ephys", unit=item.job.label) for item in outcomes]
|
|
142
|
+
raise_for_failures(outcomes)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Supply the stage and unit that the worker used when saving its report; the
|
|
146
|
+
dispatcher does not infer capsule-specific parameter names. `diagnose_outcome`
|
|
147
|
+
adds `diagnostic_report` and a readable explanation to failed outcomes while
|
|
148
|
+
preserving their status, failure classification, and capture state. Successful
|
|
149
|
+
outcomes are unchanged. `DispatchFailed` combines attached reports and retains
|
|
150
|
+
the individual failures for per-worker facts and provenance.
|
|
151
|
+
|
|
152
|
+
`worker_failure` reads the small failure header and then its namespaced report.
|
|
153
|
+
It bounds downloads, validates the common schema and causal graph, accepts new
|
|
154
|
+
domain codes, and keeps the producer's summary when full evidence is missing,
|
|
155
|
+
malformed, or larger than 2 MiB. It returns `None` when no usable header exists,
|
|
156
|
+
so callers can retain their fallback for older workers. Result-file errors
|
|
157
|
+
cannot replace the original computation failure, and signed download URLs are
|
|
158
|
+
never included in the returned explanation.
|
|
159
|
+
|
|
160
|
+
For other required JSON artifacts, `result_json` provides the same bounded
|
|
161
|
+
reader but propagates retrieval errors. Domain facts such as manifest coverage
|
|
162
|
+
remain the caller's responsibility.
|
|
@@ -13,7 +13,7 @@ authors = [
|
|
|
13
13
|
]
|
|
14
14
|
classifiers = ["Programming Language :: Python :: 3"]
|
|
15
15
|
readme = "README.md"
|
|
16
|
-
version = "0.
|
|
16
|
+
version = "0.7.0"
|
|
17
17
|
|
|
18
18
|
dependencies = [
|
|
19
19
|
"aind-code-ocean-pipeline-utils>=0.7.1",
|
|
@@ -25,9 +25,24 @@ Repository = "https://github.com/AllenNeuralDynamics/aind-codeocean-dispatch/"
|
|
|
25
25
|
Issues = "https://github.com/AllenNeuralDynamics/aind-codeocean-dispatch/issues"
|
|
26
26
|
Changelog = "https://github.com/AllenNeuralDynamics/aind-codeocean-dispatch/blob/main/CHANGELOG.md"
|
|
27
27
|
|
|
28
|
+
[project.optional-dependencies]
|
|
29
|
+
diagnostics = [
|
|
30
|
+
"aind-code-ocean-pipeline-utils[diagnostics]>=0.10.0,<1",
|
|
31
|
+
"aind-diagnostics>=0.2.0,<1",
|
|
32
|
+
]
|
|
33
|
+
|
|
28
34
|
[dependency-groups]
|
|
29
35
|
types = ["mypy"]
|
|
30
|
-
dev = [
|
|
36
|
+
dev = [
|
|
37
|
+
{ include-group = "types" },
|
|
38
|
+
"codespell",
|
|
39
|
+
"pytest",
|
|
40
|
+
"pytest-cov",
|
|
41
|
+
"ruff",
|
|
42
|
+
"interrogate",
|
|
43
|
+
"aind-diagnostics>=0.2.0,<1",
|
|
44
|
+
"aind-code-ocean-pipeline-utils[diagnostics]>=0.10.0,<1",
|
|
45
|
+
]
|
|
31
46
|
jupyter = [{ include-group = "dev" }, "ipykernel", "pip"]
|
|
32
47
|
|
|
33
48
|
[tool.hatch.build.targets.wheel]
|
|
@@ -14,6 +14,7 @@ from .runner import (
|
|
|
14
14
|
Outcome,
|
|
15
15
|
failed_without_results,
|
|
16
16
|
job_key,
|
|
17
|
+
key_marker,
|
|
17
18
|
key_tag,
|
|
18
19
|
raise_for_failures,
|
|
19
20
|
run_jobs,
|
|
@@ -35,6 +36,7 @@ __all__ = [
|
|
|
35
36
|
"failed_without_results",
|
|
36
37
|
"head_commit",
|
|
37
38
|
"job_key",
|
|
39
|
+
"key_marker",
|
|
38
40
|
"key_tag",
|
|
39
41
|
"raise_for_failures",
|
|
40
42
|
"read_only_retries",
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""Bounded retrieval of worker-produced failure artifacts.
|
|
2
|
+
|
|
3
|
+
Install the ``diagnostics`` extra to use this module. Callers supply the stage
|
|
4
|
+
and unit rather than teaching the dispatcher their capsule parameter names.
|
|
5
|
+
Fetching diagnostics never changes whether a computation succeeded.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
from dataclasses import dataclass, replace
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
import requests
|
|
15
|
+
from codeocean.error import Error as CodeOceanError
|
|
16
|
+
|
|
17
|
+
from .runner import Outcome
|
|
18
|
+
|
|
19
|
+
REPORT_LIMIT = 2 * 1024 * 1024
|
|
20
|
+
FAILURE_LIMIT = 64 * 1024
|
|
21
|
+
_UNAVAILABLE = (CodeOceanError, requests.RequestException, OSError, ValueError, KeyError, TypeError, RecursionError)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class WorkerFailure:
|
|
26
|
+
"""An explanation and optional evidence from one failed computation."""
|
|
27
|
+
|
|
28
|
+
computation_id: str
|
|
29
|
+
filename: str
|
|
30
|
+
error: str
|
|
31
|
+
report: dict[str, Any] | None = None
|
|
32
|
+
|
|
33
|
+
def render(self) -> str:
|
|
34
|
+
"""Include the worker and exact results artifact for follow-up."""
|
|
35
|
+
return f"{self.error}; worker {self.computation_id}; details: {self.filename} in worker results"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def result_json(client: Any, computation_id: str, filename: str, limit: int = REPORT_LIMIT) -> dict[str, Any]:
|
|
39
|
+
"""Read a JSON object with bounded download size and request timeout.
|
|
40
|
+
|
|
41
|
+
This lower-level function propagates errors for callers that require the
|
|
42
|
+
artifact. Signed download URLs are used for the request and never logged.
|
|
43
|
+
"""
|
|
44
|
+
if limit <= 0:
|
|
45
|
+
raise ValueError("An artifact byte limit must be positive")
|
|
46
|
+
urls = client.computations.get_result_file_urls(computation_id, filename)
|
|
47
|
+
with requests.get(urls.download_url, stream=True, timeout=30) as response:
|
|
48
|
+
response.raise_for_status()
|
|
49
|
+
content = bytearray()
|
|
50
|
+
for chunk in response.iter_content(chunk_size=4096):
|
|
51
|
+
content.extend(chunk)
|
|
52
|
+
if len(content) > limit:
|
|
53
|
+
raise ValueError("Worker artifact exceeds limit")
|
|
54
|
+
value = json.loads(content)
|
|
55
|
+
if not isinstance(value, dict):
|
|
56
|
+
raise TypeError("Worker artifact is not an object")
|
|
57
|
+
return value
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def worker_diagnostic_report(
|
|
61
|
+
client: Any, computation_id: str, stage: str, unit: str | None, failure: dict[str, Any]
|
|
62
|
+
) -> dict[str, Any] | None:
|
|
63
|
+
"""Prefer full evidence, retaining a valid summary when detail is unavailable."""
|
|
64
|
+
from aind_code_ocean_pipeline_utils.failure_reporting import diagnostic_path
|
|
65
|
+
from aind_diagnostics import validate_report
|
|
66
|
+
|
|
67
|
+
packet = failure.get("diagnostic_summary")
|
|
68
|
+
if not isinstance(packet, dict):
|
|
69
|
+
return None
|
|
70
|
+
try:
|
|
71
|
+
fallback = validate_report({**packet, "diagnostics": []})
|
|
72
|
+
except _UNAVAILABLE:
|
|
73
|
+
return None
|
|
74
|
+
path = (diagnostic_path(stage, unit) / "report.json").as_posix()
|
|
75
|
+
if failure.get("diagnostic_report_file") == path:
|
|
76
|
+
try:
|
|
77
|
+
return validate_report(result_json(client, computation_id, path))
|
|
78
|
+
except _UNAVAILABLE:
|
|
79
|
+
pass
|
|
80
|
+
fallback["facts"]["diagnostic_details_available"] = False
|
|
81
|
+
return fallback
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def worker_failure(client: Any, computation_id: str, stage: str, unit: str | None = None) -> WorkerFailure | None:
|
|
85
|
+
"""Read new or legacy failure artifacts, returning None when unavailable.
|
|
86
|
+
|
|
87
|
+
Only the expected stage/unit report path is fetched. A producer cannot
|
|
88
|
+
redirect the orchestrator to an unrelated result file. Callers retain their
|
|
89
|
+
existing fallback for older workers with no structured artifacts.
|
|
90
|
+
"""
|
|
91
|
+
from aind_code_ocean_pipeline_utils.failure_reporting import diagnostic_path, safe_text
|
|
92
|
+
|
|
93
|
+
try:
|
|
94
|
+
filename = (diagnostic_path(stage, unit) / "failure.json").as_posix()
|
|
95
|
+
failure = result_json(client, computation_id, filename, FAILURE_LIMIT)
|
|
96
|
+
if type(failure.get("schema_version")) is not int or failure["schema_version"] != 1:
|
|
97
|
+
raise ValueError("Unknown failure report version")
|
|
98
|
+
if not isinstance(failure.get("error"), str):
|
|
99
|
+
raise TypeError("Invalid failure explanation")
|
|
100
|
+
report = worker_diagnostic_report(client, computation_id, stage, unit, failure)
|
|
101
|
+
error = report["summary"] if report is not None else safe_text(failure["error"])
|
|
102
|
+
causes = failure.get("causes") or []
|
|
103
|
+
if report is None and len(causes) > 1:
|
|
104
|
+
cause = causes[-1]
|
|
105
|
+
error += safe_text(f"; caused by {cause['type']}: {cause['message']}")
|
|
106
|
+
return WorkerFailure(computation_id, filename, error, report)
|
|
107
|
+
except _UNAVAILABLE:
|
|
108
|
+
return None
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def diagnose_outcome(client: Any, outcome: Outcome, stage: str, unit: str | None = None) -> Outcome:
|
|
112
|
+
"""Attach worker evidence without changing status, retry class, or capture state."""
|
|
113
|
+
if outcome.status != "failed" or not outcome.computation_ids:
|
|
114
|
+
return outcome
|
|
115
|
+
failure = worker_failure(client, outcome.computation_ids[-1], stage, unit)
|
|
116
|
+
if failure is None:
|
|
117
|
+
return outcome
|
|
118
|
+
return replace(outcome, error=failure.render(), diagnostic_report=failure.report)
|