|
| 1 | +############################################################################## |
| 2 | +# Copyright (c) Lawrence Livermore National Security, LLC and other Merlin |
| 3 | +# Project developers. See top-level LICENSE and COPYRIGHT files for dates and |
| 4 | +# other details. No copyright assignment is required to contribute to Merlin. |
| 5 | +############################################################################## |
| 6 | + |
| 7 | +""" |
| 8 | +CLI module for cancelling running studies. |
| 9 | +
|
| 10 | +This module defines the `CancelCommand` class, which handles the `cancel` subcommand |
| 11 | +of the Merlin CLI. The `cancel` command is intended to terminate running studies |
| 12 | +gracefully. It acts as a wrapper around the `merlin purge` and `merlin stop-workers` |
| 13 | +commands. Additionally, any running studies associated with the study being cancelled |
| 14 | +will be marked as cancelled in the database. This ensures that the `merlin monitor` |
| 15 | +command will no longer track these studies. |
| 16 | +
|
| 17 | +In short, this command will: |
| 18 | +
|
| 19 | +1. Purge the queues from the study |
| 20 | +2. Stop the workers for that study |
| 21 | +3. Mark all runs associated with that study as cancelled |
| 22 | +""" |
| 23 | + |
| 24 | +# pylint: disable=duplicate-code |
| 25 | + |
| 26 | +import logging |
| 27 | +from argparse import ArgumentParser, Namespace |
| 28 | + |
| 29 | +from merlin.cli.commands.command_entry_point import CommandEntryPoint |
| 30 | +from merlin.cli.utils import get_merlin_spec_with_override |
| 31 | +from merlin.study.manager import StudyManager |
| 32 | + |
| 33 | + |
| 34 | +LOG = logging.getLogger("merlin") |
| 35 | + |
| 36 | + |
| 37 | +class CancelCommand(CommandEntryPoint): |
| 38 | + """ |
| 39 | + Handles `cancel` CLI command for terminating running studies gracefully. |
| 40 | +
|
| 41 | + Methods: |
| 42 | + add_parser: Adds the `cancel` command to the CLI parser. |
| 43 | + process_command: Processes the CLI input and dispatches the appropriate action. |
| 44 | + """ |
| 45 | + |
| 46 | + def add_parser(self, subparsers: ArgumentParser): |
| 47 | + """ |
| 48 | + Add the `cancel` command parser to the CLI argument parser. |
| 49 | +
|
| 50 | + Parameters: |
| 51 | + subparsers (ArgumentParser): The subparsers object to which the `cancel` command parser will be added. |
| 52 | + """ |
| 53 | + cancel: ArgumentParser = subparsers.add_parser( |
| 54 | + "cancel", |
| 55 | + help="Terminate running studies gracefully by purging queues, stopping workers, and marking runs as cancelled.", |
| 56 | + ) |
| 57 | + cancel.set_defaults(func=self.process_command) |
| 58 | + |
| 59 | + cancel.add_argument( |
| 60 | + "specification", |
| 61 | + type=str, |
| 62 | + help="Path to a Merlin YAML spec file for the study you want to cancel.", |
| 63 | + ) |
| 64 | + |
| 65 | + # Options to skip certain steps in the cancellation process |
| 66 | + cancel.add_argument( |
| 67 | + "--no-purge", |
| 68 | + action="store_true", |
| 69 | + help="Skip purging the queues for the study.", |
| 70 | + ) |
| 71 | + cancel.add_argument( |
| 72 | + "--no-stop-workers", |
| 73 | + action="store_true", |
| 74 | + help="Skip stopping the workers for the study.", |
| 75 | + ) |
| 76 | + cancel.add_argument( |
| 77 | + "--no-mark-cancelled", |
| 78 | + action="store_true", |
| 79 | + help="Skip marking runs as cancelled in the database.", |
| 80 | + ) |
| 81 | + |
| 82 | + # Option to substitute variables in the specification file |
| 83 | + cancel.add_argument( |
| 84 | + "--vars", |
| 85 | + action="store", |
| 86 | + dest="variables", |
| 87 | + type=str, |
| 88 | + nargs="+", |
| 89 | + default=None, |
| 90 | + help="Specify desired Merlin variable values to override those found in the specification. Space-delimited. " |
| 91 | + "Example: '--vars LEARN=path/to/new_learn.py EPOCHS=3'", |
| 92 | + ) |
| 93 | + |
| 94 | + def process_command(self, args: Namespace): |
| 95 | + """ |
| 96 | + CLI command to cancel a running study. |
| 97 | +
|
| 98 | + Args: |
| 99 | + args: Parsed CLI arguments. |
| 100 | + """ |
| 101 | + spec, _ = get_merlin_spec_with_override(args) |
| 102 | + |
| 103 | + study_manager = StudyManager() |
| 104 | + result = study_manager.cancel( |
| 105 | + spec=spec, |
| 106 | + purge_queues=not args.no_purge, |
| 107 | + stop_workers=not args.no_stop_workers, |
| 108 | + mark_runs_cancelled=not args.no_mark_cancelled, |
| 109 | + ) |
| 110 | + |
| 111 | + # Print summary |
| 112 | + result_summary = ( |
| 113 | + "\nCancellation Summary:\n" |
| 114 | + f" Study: {result['study_name']}\n" |
| 115 | + f" Runs cancelled: {result['runs_cancelled']}\n" |
| 116 | + f" Queues purged: {len(result['queues_purged'])}\n" |
| 117 | + f" Workers stopped: {len(result['workers_stopped'])}" |
| 118 | + ) |
| 119 | + LOG.info(result_summary) |
0 commit comments