Skip to content

start_manager

start_manager

Start acquisition control manager service/process.

main

main() -> None

Start the acquisition service (command line entry point nexus).

Connects the measurement cards via AcquisitionControl and serves this instance through an AcquisitionControlManager (multiprocessing.managers.BaseManager), so that acquisitions can be run from other processes or hosts.

Command line arguments:

  • -d, --device_config: path to the device configuration YAML file (required).
  • -f, --sessions_folder: directory for logs and acquisition data (default <home>/nexus-console).
  • -k, --authkey: authentication key of the manager process.
  • -a, --address: address the manager binds to (default localhost).
  • -p, --port: port of the manager (default 50000).
  • -n, --no-verify: skip the confirmation prompt that all amplifiers are switched off. The XIO outputs of the cards are pulled high while the cards are opened.
Source code in src/console/service/start_manager.py
def main() -> None:
    """Start the acquisition service (command line entry point ``nexus``).

    Connects the measurement cards via `AcquisitionControl` and serves this instance through an
    `AcquisitionControlManager` (``multiprocessing.managers.BaseManager``), so that acquisitions
    can be run from other processes or hosts.

    Command line arguments:

    - ``-d``, ``--device_config``: path to the device configuration YAML file (required).
    - ``-f``, ``--sessions_folder``: directory for logs and acquisition data (default ``<home>/nexus-console``).
    - ``-k``, ``--authkey``: authentication key of the manager process.
    - ``-a``, ``--address``: address the manager binds to (default ``localhost``).
    - ``-p``, ``--port``: port of the manager (default 50000).
    - ``-n``, ``--no-verify``: skip the confirmation prompt that all amplifiers are switched off.
      The XIO outputs of the cards are pulled high while the cards are opened.
    """
    parser = argparse.ArgumentParser(description="Start Nexus acquisition service.")
    parser.add_argument(
        "-d",
        "--device_config",
        type=str,
        required=True,
        help="Path to device configuration yaml file.",
    )
    parser.add_argument(
        "-f",
        "--sessions_folder",
        type=str,
        default=str(Path.home() / "nexus-console"),
        help="Directory to store all the acquisition data acquired during the session.",
    )
    parser.add_argument(
        "-k",
        "--authkey",
        type=str,
        default=b"secretkey",
        help="Manager process authentication key, must be a bytestring",
    )
    parser.add_argument(
        "-a",
        "--address",
        type=str,
        default="localhost",
        help="Manager process connection address",
    )
    parser.add_argument(
        "-p",
        "--port",
        type=int,
        default=50000,
        help="Manager process connection port",
    )
    # Note, XIO lines have a pull-up -> XIO output is temporarily high when opening the cards:
    # https://github.com/schote/nexus-console/issues/54#issuecomment-2823593549
    parser.add_argument(
        "-n",
        "--no-verify",
        action="store_true",
        help="If this flag is set, the service starts immediately without asking the user for confirmation. \
                Be aware that the outputs of the XIO lines have a pull-up resistor to pull the signal high, \
                    when the card isn't actively setting the output (e.g. when opening the card). \
                        This is the case when starting the service because the cards are opened.",
    )
    args = parser.parse_args()
    if not args.no_verify:
        input("\n[neXus] Before starting the setup, confirm that all the amplifiers are turned off.\
            \nPress Enter to continue...")
    print("\n[neXus] Setting up the acquisition control...\n")

    # Setup global acquisition control with argparse arguments
    # global acquisition_control
    acquisition_control = AcquisitionControl(
        configuration_file=args.device_config,
        nexus_data_dir=args.sessions_folder,
        console_log_level=logging.INFO,
        file_log_level=logging.INFO
    )

    manager = AcquisitionControlManager(
        callable_acq_control=lambda: acquisition_control,
        address=(args.address, args.port),
        authkey=args.authkey,
    )

    server = manager.get_server()

    def shutdown_handler() -> None:
        try:
            print("\n[neXus] Shutting down nexus server...\n")
            if acquisition_control:
                acquisition_control.__del__()
                print("[neXus] Acquisition control shutdown successfully.")
        except Exception as e:  # noqa: BLE001 - atexit handler must not raise, report any error instead
            print(f"[neXus] Error during shutdown: {e}")
        finally:
            print("[neXus] Shutdown complete.")

    atexit.register(shutdown_handler)

    print(f"\n[neXus] AcquisitionControlManager >> Server started on port {args.port}...\n")
    server.serve_forever()