Skip to content

Commit 1765ded

Browse files
authored
chore: update to latest migas api (#3138)
Strips away error handling logic, which has been ported to `migas.track_exit`.
2 parents 556db60 + 27ec656 commit 1765ded

File tree

3 files changed

+19
-49
lines changed

3 files changed

+19
-49
lines changed

fmriprep/cli/run.py

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
"""fMRI preprocessing workflow."""
2525
from .. import config
2626

27-
EXITCODE: int = -1
28-
2927

3028
def main():
3129
"""Entry point."""
@@ -76,15 +74,12 @@ def main():
7674

7775
sentry_sdk = None
7876
if not config.execution.notrack and not config.execution.debug:
79-
import atexit
80-
8177
import sentry_sdk
8278

8379
from ..utils.telemetry import sentry_setup, setup_migas
8480

8581
sentry_setup()
8682
setup_migas(init_ping=True)
87-
atexit.register(migas_exit)
8883

8984
# CRITICAL Save the config to a file. This is necessary because the execution graph
9085
# is built as a separate process to keep the memory footprint low. The most
@@ -110,8 +105,7 @@ def main():
110105
else:
111106
retval = build_workflow(str(config_file), {})
112107

113-
global EXITCODE
114-
EXITCODE = retval.get("return_code", 0)
108+
exitcode = retval.get("return_code", 0)
115109
fmriprep_wf = retval.get("workflow", None)
116110

117111
# CRITICAL Load the config from the file. This is necessary because the ``build_workflow``
@@ -120,14 +114,14 @@ def main():
120114
config.load(config_file)
121115

122116
if config.execution.reports_only:
123-
sys.exit(int(EXITCODE > 0))
117+
sys.exit(int(exitcode > 0))
124118

125119
if fmriprep_wf and config.execution.write_graph:
126120
fmriprep_wf.write_graph(graph2use="colored", format="svg", simple_form=True)
127121

128-
EXITCODE = EXITCODE or (fmriprep_wf is None) * EX_SOFTWARE
129-
if EXITCODE != 0:
130-
sys.exit(EXITCODE)
122+
exitcode = exitcode or (fmriprep_wf is None) * EX_SOFTWARE
123+
if exitcode != 0:
124+
sys.exit(exitcode)
131125

132126
# Generate boilerplate
133127
with Manager() as mgr:
@@ -138,7 +132,7 @@ def main():
138132
p.join()
139133

140134
if config.execution.boilerplate_only:
141-
sys.exit(int(EXITCODE > 0))
135+
sys.exit(int(exitcode > 0))
142136

143137
# Clean up master process before running workflow, which may create forks
144138
gc.collect()
@@ -238,37 +232,6 @@ def main():
238232
sys.exit(int((errno + failed_reports) > 0))
239233

240234

241-
def migas_exit() -> None:
242-
"""
243-
Send a final crumb to the migas server signaling if the run successfully completed
244-
This function should be registered with `atexit` to run at termination.
245-
"""
246-
import sys
247-
248-
from ..utils.telemetry import send_breadcrumb
249-
250-
global EXITCODE
251-
migas_kwargs = {'status': 'C'}
252-
# `sys` will not have these attributes unless an error has been handled
253-
if hasattr(sys, 'last_type'):
254-
migas_kwargs = {
255-
'status': 'F',
256-
'status_desc': 'Finished with error(s)',
257-
'error_type': sys.last_type,
258-
'error_desc': sys.last_value,
259-
}
260-
elif EXITCODE != 0:
261-
migas_kwargs.update(
262-
{
263-
'status': 'F',
264-
'status_desc': f'Completed with exitcode {EXITCODE}',
265-
}
266-
)
267-
else:
268-
migas_kwargs['status_desc'] = 'Success'
269-
send_breadcrumb(**migas_kwargs)
270-
271-
272235
if __name__ == "__main__":
273236
raise RuntimeError(
274237
"fmriprep/cli/run.py should not be run directly;\n"

fmriprep/utils/telemetry.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def _chunks(string, length=CHUNK_SIZE):
185185
return [string[i : i + length] for i in range(0, len(string), length)]
186186

187187

188-
def setup_migas(init_ping: bool = True) -> None:
188+
def setup_migas(init_ping: bool = True, exit_ping: bool = True) -> None:
189189
"""
190190
Prepare the migas python client to communicate with a migas server.
191191
If ``init_ping`` is ``True``, send an initial breadcrumb.
@@ -197,12 +197,19 @@ def setup_migas(init_ping: bool = True) -> None:
197197

198198
migas.setup(session_id=session_id)
199199
if init_ping:
200-
send_breadcrumb(status='R', status_desc='workflow start')
200+
send_crumb(status='R', status_desc='workflow start')
201+
if exit_ping:
202+
from migas.error.nipype import node_execution_error
203+
204+
migas.track_exit(
205+
'nipreps/fmriprep',
206+
__version__,
207+
{'NodeExecutionEror', node_execution_error},
208+
)
201209

202210

203-
def send_breadcrumb(**kwargs) -> dict:
211+
def send_crumb(**kwargs) -> dict:
204212
"""
205213
Communicate with the migas telemetry server. This requires `migas.setup()` to be called.
206214
"""
207-
res = migas.add_project("nipreps/fmriprep", __version__, **kwargs)
208-
return res
215+
return migas.add_breadcrumb("nipreps/fmriprep", __version__, **kwargs)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ container = [
7070
"datalad-osf",
7171
]
7272
telemetry = [
73-
"migas >= 0.3.0",
73+
"migas >= 0.4.0",
7474
"sentry-sdk >= 1.3",
7575
]
7676
test = [

0 commit comments

Comments
 (0)