Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exp queue: Add two command queue remove and queue kill #7721

Merged
merged 1 commit into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dvc/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
move,
params,
plots,
queue,
remote,
remove,
repro,
Expand All @@ -50,6 +51,7 @@

COMMANDS = [
init,
queue,
get,
get_url,
destroy,
Expand Down
30 changes: 30 additions & 0 deletions dvc/commands/queue/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import argparse

from dvc.cli.utils import append_doc_link, fix_subparsers
from dvc.commands.queue import kill, remove

SUB_COMMANDS = [
remove,
kill,
]


def add_parser(subparsers, parent_parser):
QUEUE_HELP = "Commands to manage dvc task queue."

queue_parser = subparsers.add_parser(
"queue",
parents=[parent_parser],
description=append_doc_link(QUEUE_HELP, "queue"),
formatter_class=argparse.RawDescriptionHelpFormatter,
help=QUEUE_HELP,
)

queue_subparsers = queue_parser.add_subparsers(
dest="cmd",
help="Use `dvc queue CMD --help` to display " "command-specific help.",
)

fix_subparsers(queue_subparsers)
for cmd in SUB_COMMANDS:
cmd.add_parser(queue_subparsers, parent_parser)
34 changes: 34 additions & 0 deletions dvc/commands/queue/kill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import argparse
import logging

from dvc.cli.command import CmdBase
from dvc.cli.utils import append_doc_link

logger = logging.getLogger(__name__)


class CmdQueueKill(CmdBase):
"""Kill exp task in queue."""

def run(self):
self.repo.experiments.celery_queue.kill(revs=self.args.experiment)

return 0


def add_parser(queue_subparsers, parent_parser):
QUEUE_KILL_HELP = "Kill experiments in queue"
queue_kill_parser = queue_subparsers.add_parser(
"kill",
parents=[parent_parser],
description=append_doc_link(QUEUE_KILL_HELP, "queue/kill"),
help=QUEUE_KILL_HELP,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
queue_kill_parser.add_argument(
"experiment",
nargs="*",
help="Experiments in queue to kill.",
metavar="<experiment>",
)
queue_kill_parser.set_defaults(func=CmdQueueKill)
47 changes: 47 additions & 0 deletions dvc/commands/queue/remove.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import argparse
import logging

from dvc.cli.command import CmdBase
from dvc.cli.utils import append_doc_link
from dvc.ui import ui

logger = logging.getLogger(__name__)


class CmdQueueRemove(CmdBase):
"""Remove exp in queue."""

def run(self):
if self.args.all:
removed_list = self.repo.experiments.celery_queue.clear()
else:
removed_list = self.repo.experiments.celery_queue.remove(
revs=self.args.experiment
)

removed = ", ".join(removed_list)
ui.write(f"Removed experiments in queue: {removed}")

return 0


def add_parser(queue_subparsers, parent_parser):

QUEUE_REMOVE_HELP = "Remove experiments in queue"
queue_remove_parser = queue_subparsers.add_parser(
"remove",
parents=[parent_parser],
description=append_doc_link(QUEUE_REMOVE_HELP, "queue/remove"),
help=QUEUE_REMOVE_HELP,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
queue_remove_parser.add_argument(
"--all", action="store_true", help="Remove all experiments in queue."
)
queue_remove_parser.add_argument(
"experiment",
nargs="*",
help="Experiments in queue to remove.",
metavar="<experiment>",
)
queue_remove_parser.set_defaults(func=CmdQueueRemove)
63 changes: 63 additions & 0 deletions tests/unit/command/test_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from dvc.cli import parse_args
from dvc.commands.queue.kill import CmdQueueKill
from dvc.commands.queue.remove import CmdQueueRemove


def test_experiments_remove(dvc, scm, mocker):
cli_args = parse_args(
[
"queue",
"remove",
"--all",
]
)
assert cli_args.func == CmdQueueRemove

cmd = cli_args.func(cli_args)
m = mocker.patch(
"dvc.repo.experiments.queue.local.LocalCeleryQueue.clear",
return_value={},
)

assert cmd.run() == 0
m.assert_called_once_with()

cli_args = parse_args(
[
"queue",
"remove",
"exp1",
"exp2",
]
)
assert cli_args.func == CmdQueueRemove

cmd = cli_args.func(cli_args)
m = mocker.patch(
"dvc.repo.experiments.queue.local.LocalCeleryQueue.remove",
return_value={},
)

assert cmd.run() == 0
m.assert_called_once_with(revs=["exp1", "exp2"])


def test_experiments_kill(dvc, scm, mocker):
cli_args = parse_args(
[
"queue",
"kill",
"exp1",
"exp2",
]
)
assert cli_args.func == CmdQueueKill

cmd = cli_args.func(cli_args)
m = mocker.patch(
"dvc.repo.experiments.queue.local.LocalCeleryQueue.kill",
return_value={},
)

assert cmd.run() == 0
m.assert_called_once_with(revs=["exp1", "exp2"])