3
3
"""NiBabies runner."""
4
4
from .. import config
5
5
6
+ EXITCODE : int = - 1
7
+
6
8
7
9
def main ():
8
10
"""Entry point."""
@@ -13,7 +15,7 @@ def main():
13
15
from pathlib import Path
14
16
15
17
from ..utils .bids import write_bidsignore , write_derivative_description
16
- from ..utils .misc import ping_migas
18
+ from ..utils .telemetry import setup_migas
17
19
from .parser import parse_args
18
20
from .workflow import build_boilerplate , build_workflow
19
21
@@ -23,9 +25,12 @@ def main():
23
25
24
26
parse_args ()
25
27
26
- # collect telemetry information - if `--notrack` is specified,
27
- # nothing is sent.
28
- ping_migas ()
28
+ # collect and submit telemetry information
29
+ # if `--notrack` is specified, nothing is done.
30
+ global EXITCODE
31
+ if not config .execution .notrack :
32
+ setup_migas (init_ping = True )
33
+ atexit .register (_migas_exit )
29
34
30
35
if "participant" in config .workflow .analysis_level :
31
36
_pool = None
@@ -55,27 +60,27 @@ def main():
55
60
# build the workflow within the same process
56
61
# it still needs to be saved / loaded to be properly initialized
57
62
retval = build_workflow (config_file )
58
- retcode = retval ['return_code' ]
63
+ EXITCODE = retval ['return_code' ]
59
64
nibabies_wf = retval ['workflow' ]
60
65
61
- if nibabies_wf is None :
62
- ping_migas (status = 'error' )
63
- if config .execution .reports_only :
64
- sys .exit (int (retcode > 0 ))
65
- sys .exit (os .EX_SOFTWARE )
66
+ # exit conditions:
67
+ # - no workflow (--reports-only)
68
+ # - retcode is not 0
69
+ # - boilerplate only
70
+
71
+ if nibabies_wf is None and not config .execution .reports_only :
72
+ sys .exit (EXITCODE )
66
73
67
74
if config .execution .write_graph :
68
75
nibabies_wf .write_graph (graph2use = "colored" , format = "svg" , simple_form = True )
69
76
70
- if retcode != 0 :
71
- ping_migas (status = 'error' )
72
- sys .exit (retcode )
77
+ if EXITCODE != 0 :
78
+ sys .exit (EXITCODE )
73
79
74
80
# generate boilerplate
75
81
build_boilerplate (nibabies_wf )
76
82
if config .execution .boilerplate_only :
77
- ping_migas (status = 'success' )
78
- sys .exit (0 )
83
+ sys .exit (EXITCODE )
79
84
80
85
gc .collect ()
81
86
@@ -99,13 +104,12 @@ def main():
99
104
nibabies_wf .run (** _plugin )
100
105
except Exception as e :
101
106
config .loggers .workflow .critical ("nibabies failed: %s" , e )
102
- ping_migas ( status = 'error' )
107
+ EXITCODE = 1
103
108
raise
104
109
else :
105
110
config .loggers .workflow .log (25 , "nibabies finished successfully!" )
106
111
# Bother users with the boilerplate only iff the workflow went okay.
107
112
boiler_file = config .execution .nibabies_dir / "logs" / "CITATION.md"
108
- ping_migas (status = 'success' )
109
113
if boiler_file .exists ():
110
114
if config .environment .exec_env in (
111
115
"singularity" ,
@@ -147,6 +151,20 @@ def main():
147
151
write_bidsignore (config .execution .nibabies_dir )
148
152
149
153
154
+ def _migas_exit () -> None :
155
+ """
156
+ Send a final crumb to the migas server signaling if the run successfully completed
157
+
158
+ This function is registered with `atexit` to run at termination.
159
+ """
160
+ global EXITCODE
161
+ status = 'error' if EXITCODE > 0 else 'success'
162
+
163
+ from ..utils .telemetry import ping_migas
164
+
165
+ ping_migas (status = status )
166
+
167
+
150
168
if __name__ == "__main__" :
151
169
raise RuntimeError (
152
170
"Please `pip install` this and run via the commandline interfaces, `nibabies <command>`"
0 commit comments