Repository: ray-project/ray
File: python/ray/scripts/scripts.py (Python)

Code before:

    address = services.canonicalize_bootstrap_address_or_die(address)

    # Respect token-based authentication when enabled.
    ensure_token_if_auth_enabled(None, create_token_if_missing=False)

    # Connect to the cluster (no driver logging)
    if not ray.is_initialized():
        ray.init(address=address, namespace=namespace, log_to_driver=False)

    if not force and no_restart:
        click.echo(
            "WARNING: --no-restart flag is only effective with --force (graceful termination does not support controlling actor restart).",
            err=True,
        )

    try:
        try:
            actor_handle = ray.get_actor(name, namespace=namespace)
        except Exception as e:
            raise click.ClickException(
                f"No named actor found: {name} (namespace={namespace}): {e}"
            )

        if force:
            try:
                ray.kill(actor_handle, no_restart=no_restart)
                click.echo(f"Actor killed (force): {name}")
                return
            except Exception as e:
                raise click.ClickException(f"Failed to force kill actor: {e}")

        else:
            try:
                actor_handle.__ray_terminate__.remote()
                click.echo(f"Requested graceful termination for actor: {name}")
            except Exception as e:
                raise click.ClickException(
                    f"Failed to request graceful termination: {e}"
                )
    except click.ClickException:
        raise
    except Exception as e:
        raise click.ClickException(f"Failed to kill actor: {e}")


@cli.command()
@click.argument("cluster_config_file", required=True, type=str)
@click.option(
    "--min-workers",
    required=False,

Review comment (suggestion):
the exception handling here is unnecessarily convoluted. the only place that I think it makes sense to catch and wrap an error is in the "actor not found" case. In that case, we should also catch the specific exception type that's raised when the actor isn't found, which I believe is `ValueError`

Code after:
    Examples:
        ray kill-actor MyActor
        ray kill-actor MyActor --namespace my_namespace
        ray kill-actor MyActor --force
        ray kill-actor MyActor --force --no-restart
    """
    # Validate input: require a name
    if not name:
        raise click.ClickException("Must specify --name to identify the actor.")

    address = services.canonicalize_bootstrap_address_or_die(address)

    # Respect token-based authentication when enabled.
    ensure_token_if_auth_enabled(None, create_token_if_missing=False)

    # Connect to the cluster (no driver logging)
    if not ray.is_initialized():
        ray.init(address=address, namespace=namespace, log_to_driver=False)

    if not force and no_restart:
        click.echo(
            "WARNING: --no-restart flag is only effective with --force (graceful termination does not support controlling actor restart).",
            err=True,
        )

    try:
        actor_handle = ray.get_actor(name, namespace=namespace)
    except ValueError:
        raise click.ClickException(
            f"No named actor found: {name} (namespace={namespace})"
        )
    if force:
        ray.kill(actor_handle, no_restart=no_restart)
        click.echo(f"Actor killed (force): {name}")
    else:
        actor_handle.__ray_terminate__.remote()
        click.echo(f"Requested graceful termination for actor: {name}")


@cli.command()
@click.argument("cluster_config_file", required=True, type=str)
@click.option(
    "--min-workers",
    required=False,
    type=int,
    help="Override the configured min worker node count for the cluster.",
)
@click.option(
    "--max-workers",
    required=False,
    type=int,