Skip to content

Commit

Permalink
Merge pull request #1160 from mgxd/chore/migas
Browse files Browse the repository at this point in the history
MAINT: update to latest migas API
  • Loading branch information
oesteban committed Nov 27, 2023
2 parents 89fedbe + 7d3909d commit 8ceadba
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 44 deletions.
42 changes: 7 additions & 35 deletions mriqc/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# https://www.nipreps.org/community/licensing/
#
"""Definition of the command line interface's (CLI) entry point."""
EXITCODE: int = -1


def main():
Expand Down Expand Up @@ -55,7 +54,7 @@ def main():
)
config.to_filename(config_file)
config.file_path = config_file

exitcode = 0
# Set up participant level
if "participant" in config.workflow.analysis_level:
_pool = None
Expand Down Expand Up @@ -92,15 +91,13 @@ def main():
if not config.execution.notrack:
from ..utils.telemetry import setup_migas

setup_migas(init=True)
atexit.register(migas_exit)
setup_migas()

# CRITICAL Call build_workflow(config_file, retval) in a subprocess.
# Because Python on Linux does not ever free virtual memory (VM), running the
# workflow construction jailed within a process preempts excessive VM buildup.
from multiprocessing import Manager, Process

global EXITCODE
with Manager() as mgr:
from .workflow import build_workflow

Expand All @@ -110,16 +107,16 @@ def main():
p.join()

mriqc_wf = retval.get("workflow", None)
EXITCODE = p.exitcode or retval.get("return_code", 0)
exitcode = p.exitcode or retval.get("return_code", 0)

# CRITICAL Load the config from the file. This is necessary because the ``build_workflow``
# function executed constrained in a process may change the config (and thus the global
# state of MRIQC).
config.load(config_file)

EXITCODE = EXITCODE or (mriqc_wf is None) * os.EX_SOFTWARE
if EXITCODE != 0:
sys.exit(EXITCODE)
exitcode = exitcode or (mriqc_wf is None) * os.EX_SOFTWARE
if exitcode != 0:
sys.exit(exitcode)

# Initialize nipype config
config.nipype.init()
Expand Down Expand Up @@ -232,7 +229,6 @@ def main():
if not mod_group_reports:
raise Exception(messages.GROUP_NO_DATA)

EXITCODE = 0 if EXITCODE == -1 else EXITCODE
config.loggers.cli.info(messages.GROUP_FINISHED)

from mriqc.utils.bids import write_bidsignore, write_derivative_description
Expand All @@ -241,31 +237,7 @@ def main():
write_derivative_description(config.execution.bids_dir, config.execution.output_dir)
write_bidsignore(config.execution.output_dir)
config.loggers.cli.info(messages.RUN_FINISHED)
sys.exit(EXITCODE)


def migas_exit() -> None:
"""
Send a final crumb to the migas server signaling if the run successfully completed
This function should be registered with `atexit` to run at termination.
"""
import sys
from ..utils.telemetry import send_breadcrumb

global EXITCODE
migas_kwargs = {'status': 'C', 'status_desc': 'Success'}
# `sys` will not have these attributes unless an error has been handled
if hasattr(sys, 'last_type'):
migas_kwargs = {
'status': 'F',
'status_desc': 'Finished with error(s)',
'error_type': sys.last_type,
'error_desc': sys.last_value,
}
elif EXITCODE != 0:
migas_kwargs.update({'status': 'F', 'status_desc': f'Completed with exitcode {EXITCODE}'})

send_breadcrumb(**migas_kwargs)
sys.exit(exitcode)


if __name__ == "__main__":
Expand Down
19 changes: 13 additions & 6 deletions mriqc/utils/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .. import __version__, config


def setup_migas(init: bool = True) -> None:
def setup_migas(init_ping: bool = True, exit_ping: bool = True) -> None:
"""
Prepare the migas python client to communicate with a migas server.
If ``init`` is ``True``, send an initial breadcrumb.
Expand All @@ -14,14 +14,21 @@ def setup_migas(init: bool = True) -> None:
session_id = config.execution.run_uuid.split('_', 1)[-1]

migas.setup(session_id=session_id)
if init:
if init_ping:
# send initial status ping
send_breadcrumb(status='R', status_desc='workflow start')
send_crumb(status='R', status_desc='workflow start')
if exit_ping:
from migas.error.nipype import node_execution_error

migas.track_exit(
'nipreps/mriqc',
__version__,
{'NodeExecutionError': node_execution_error},
)

def send_breadcrumb(**kwargs) -> dict:

def send_crumb(**kwargs) -> dict:
"""
Communicate with the migas telemetry server. This requires `migas.setup()` to be called.
"""
res = migas.add_project("nipreps/mriqc", __version__, **kwargs)
return res
return migas.add_breadcrumb("nipreps/mriqc", __version__, **kwargs)
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ dependencies = [
# jinja2 imports deprecated function removed in 2.1
"markupsafe ~= 2.0.1",
"matplotlib",
"migas",
"migas >= 0.4.0",
"mriqc-learn",
"nibabel >= 3.0.1",
"nilearn >= 0.5.1",
"nipype ~= 1.4",
"nireports ~= 23.1",
"nitransforms ~= 23.0",
"nireports ~= 23.1",
"nitransforms ~= 23.0",
"niworkflows @ git+https://github.com/nipreps/niworkflows.git@master",
"numpy ~=1.20",
"pandas ~=1.0",
Expand Down

0 comments on commit 8ceadba

Please sign in to comment.