-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a399d2
commit 260e5f0
Showing
9 changed files
with
196 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#!/usr/bin/env python | ||
# (C) Copyright 2024 ECMWF. | ||
# | ||
# This software is licensed under the terms of the Apache Licence Version 2.0 | ||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
# In applying this licence, ECMWF does not waive the privileges and immunities | ||
# granted to it by virtue of its status as an intergovernmental organisation | ||
# nor does it submit to any jurisdiction. | ||
# | ||
|
||
"""Command place holder. Delete when we have real commands. | ||
""" | ||
|
||
import datetime | ||
import logging | ||
|
||
from anemoi.utils.humanize import when | ||
from anemoi.utils.text import table | ||
|
||
from ..queue_manager import add | ||
from ..queue_manager import disown | ||
from ..queue_manager import get_list | ||
from ..queue_manager import own | ||
from ..queue_manager import remove | ||
from ..queue_manager import set_progress | ||
from ..queue_manager import set_status | ||
from . import Command | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class Queues(Command): | ||
internal = True | ||
timestamp = True | ||
|
||
def add_arguments(self, command_parser): | ||
command_parser.add_argument("--new", help="add to queue (key=value list)", nargs="+", metavar="K=V") | ||
command_parser.add_argument("--remove", help="remove from queue", nargs=1) | ||
command_parser.add_argument("--set-status", help="--set-status uuid <status>", nargs=2) | ||
command_parser.add_argument("--set-progress", help="--set-progress uuid <progress>", nargs=2) | ||
|
||
command_parser.add_argument("--own", help="Take ownership of the oldest entry with status=queued", nargs="*") | ||
command_parser.add_argument("--disown", help="Release a task and requeue it", metavar="UUID") | ||
|
||
command_parser.add_argument("--sort", help="Sort by date", choices=["created", "updated"], default="updated") | ||
|
||
command_parser.add_argument("--list", help="List some queue entries", nargs="*") | ||
command_parser.add_argument("-l", "--long", help="Details", action="store_true") | ||
|
||
def run(self, args): | ||
if args.list is not None: | ||
request = {v.split("=")[0]: v.split("=")[1] for v in args.list} | ||
return self.list(request=args.list, long=args.long, sort=args.sort) | ||
|
||
if args.disown: | ||
disown(args.disown) | ||
|
||
if args.own is not None: | ||
request = {v.split("=")[0]: v.split("=")[1] for v in args.own} | ||
self.own(request, sort=args.sort) | ||
|
||
if args.remove: | ||
res = remove(args.remove[0]) | ||
print(res) | ||
return | ||
|
||
if args.new: | ||
record = {v.split("=")[0]: v.split("=")[1] for v in args.new} | ||
res = add(record) | ||
print(res) | ||
|
||
if args.set_status: | ||
uuid, status = args.set_status | ||
set_status(uuid, status) | ||
|
||
if args.set_progress: | ||
uuid, progress = args.set_progress | ||
set_progress(uuid, int(progress)) | ||
|
||
def list(self, request, long=False, sort="updated"): | ||
res = get_list(request) | ||
res = sorted(res, key=lambda x: x[sort]) | ||
|
||
rows = [] | ||
for v in res: | ||
if not isinstance(v, dict): | ||
raise ValueError(v) | ||
created = datetime.datetime.fromisoformat(v.pop("created")) | ||
updated = datetime.datetime.fromisoformat(v.pop("updated")) | ||
|
||
uuid = v.pop("uuid") | ||
content = " ".join(f"{k}={v}" for k, v in v.items()) | ||
if not long: | ||
content = content[:20] + "..." | ||
uuid = uuid[:5] + "..." | ||
rows.append( | ||
[ | ||
when(created), | ||
when(updated), | ||
v.pop("status"), | ||
v.pop("progress", ""), | ||
content, | ||
uuid, | ||
] | ||
) | ||
print(table(rows, ["Created", "Updated", "Status", "%", "Details", "UUID"], ["<", "<", "<", "<", "<", "<"])) | ||
return | ||
|
||
def own(request, sort): | ||
if not request: | ||
request = {"status": "queued"} | ||
res = get_list(request) | ||
res = sorted(res, key=lambda x: x[sort]) | ||
uuids = [v["uuid"] for v in res] | ||
latest = uuids.pop() | ||
|
||
own(latest) | ||
|
||
|
||
command = Queues |
Oops, something went wrong.