Skip to content

Commit

Permalink
feat(cmd): Implemented a CLI for task management
Browse files Browse the repository at this point in the history
This patch extends `CodeChecker cmd` with a new sub-command,
`serverside-tasks`, which lets users and administrators deal with
querying the status of running server-side tasks.

By default, the CLI queries the information of the task(s) specified by
their token(s) in the `--token` argument from the server using
`getTaskInfo(token)`, and shows this information in either verbose
"plain text" (available if precisely **one** task was specified), "table"
or JSON formats.

In addition to `--token`, it also supports 19 more parameters, each of
which correspond to a filter option in the `TaskFilter` API type.
If any filters in addition to `--token` is specified, it will exercise
`getTasks(filter)` instead.
This mode is only available to administrators.
The resulting, more detailed information structs are printed in "table"
or JSON formats.

Apart from querying the current status, two additional flags are
available, irrespective of which query method is used to obtain a list
of "matching tasks":

  * `--kill` will call `cancelTask(token)` for each task.
  * `--await` will block execution until the specified task(s) terminate
    (in one way or another).

`--await` is implemented by calling the new **`await_task_termination`**
library function, which is implemented with the goal of being reusable
by other clients later.
  • Loading branch information
whisperity committed Sep 18, 2024
1 parent a75244f commit d73c1da
Show file tree
Hide file tree
Showing 9 changed files with 1,282 additions and 14 deletions.
35 changes: 35 additions & 0 deletions codechecker_common/typehints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -------------------------------------------------------------------------
#
# Part of the CodeChecker project, under the Apache License v2.0 with
# LLVM Exceptions. See LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# -------------------------------------------------------------------------
"""
Type hint (`typing`) extensions.
"""
from typing import Any, Protocol, TypeVar


_T_contra = TypeVar("_T_contra", contravariant=True)


class LTComparable(Protocol[_T_contra]):
def __lt__(self, other: _T_contra, /) -> bool: ...


class LEComparable(Protocol[_T_contra]):
def __le__(self, other: _T_contra, /) -> bool: ...


class GTComparable(Protocol[_T_contra]):
def __gt__(self, other: _T_contra, /) -> bool: ...


class GEComparable(Protocol[_T_contra]):
def __ge__(self, other: _T_contra, /) -> bool: ...


class Orderable(LTComparable[Any], LEComparable[Any],
GTComparable[Any], GEComparable[Any], Protocol):
"""Type hint for something that supports rich comparison operators."""
4 changes: 3 additions & 1 deletion codechecker_common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

from codechecker_common.logger import get_logger

from .typehints import Orderable

LOG = get_logger('system')


Expand All @@ -35,7 +37,7 @@ def arg_match(options, args):
return matched_args


def clamp(min_: int, value: int, max_: int) -> int:
def clamp(min_: Orderable, value: Orderable, max_: Orderable) -> Orderable:
"""Clamps ``value`` such that ``min_ <= value <= max_``."""
if min_ > max_:
raise ValueError("min <= max required")
Expand Down
290 changes: 290 additions & 0 deletions docs/web/user_guide.md

Large diffs are not rendered by default.

36 changes: 30 additions & 6 deletions web/client/codechecker_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
from codechecker_web.shared import env
from codechecker_web.shared.version import CLIENT_API

from codechecker_client.helpers.authentication import ThriftAuthHelper
from codechecker_client.helpers.product import ThriftProductHelper
from codechecker_client.helpers.results import ThriftResultsHelper
from .credential_manager import UserCredentials
from .helpers.authentication import ThriftAuthHelper
from .helpers.product import ThriftProductHelper
from .helpers.results import ThriftResultsHelper
from .helpers.tasks import ThriftServersideTaskHelper
from .product import split_product_url

LOG = get_logger('system')
Expand Down Expand Up @@ -65,7 +66,7 @@ def setup_auth_client(protocol, host, port, session_token=None):
session token for the session.
"""
client = ThriftAuthHelper(protocol, host, port,
'/v' + CLIENT_API + '/Authentication',
f"/v{CLIENT_API}/Authentication",
session_token)

return client
Expand All @@ -78,7 +79,7 @@ def login_user(protocol, host, port, username, login=False):
"""
session = UserCredentials()
auth_client = ThriftAuthHelper(protocol, host, port,
'/v' + CLIENT_API + '/Authentication')
f"/v{CLIENT_API}/Authentication")

if not login:
logout_done = auth_client.destroySession()
Expand Down Expand Up @@ -213,7 +214,7 @@ def setup_product_client(protocol, host, port, auth_client=None,
# as "viewpoint" from which the product service is called.
product_client = ThriftProductHelper(
protocol, host, port,
'/' + product_name + '/v' + CLIENT_API + '/Products',
f"/{product_name}/v{CLIENT_API}/Products",
session_token,
lambda: get_new_token(protocol, host, port, cred_manager))

Expand Down Expand Up @@ -263,3 +264,26 @@ def setup_client(product_url) -> ThriftResultsHelper:
f"/{product_name}/v{CLIENT_API}/CodeCheckerService",
session_token,
lambda: get_new_token(protocol, host, port, cred_manager))


def setup_task_client(protocol, host, port, auth_client=None,
session_token=None):
"""
Setup the Thrift client for the server-side task management endpoint.
"""
cred_manager = UserCredentials()
session_token = cred_manager.get_token(host, port)

if not session_token:
auth_client = setup_auth_client(protocol, host, port)
session_token = perform_auth_for_handler(auth_client, host, port,
cred_manager)

# Attach to the server-wide task management service.
task_client = ThriftServersideTaskHelper(
protocol, host, port,
f"/v{CLIENT_API}/Tasks",
session_token,
lambda: get_new_token(protocol, host, port, cred_manager))

return task_client
Loading

0 comments on commit d73c1da

Please sign in to comment.