Skip to content

Commit

Permalink
Merge pull request #138 from ImperialCollegeLondon/general-refactor
Browse files Browse the repository at this point in the history
General refactor
  • Loading branch information
cc-a authored Oct 9, 2024
2 parents fc3a489 + 864ca64 commit 560b576
Show file tree
Hide file tree
Showing 18 changed files with 501 additions and 450 deletions.
4 changes: 4 additions & 0 deletions drunc_ui/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import os
from pathlib import Path

import django_stubs_ext

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent

Expand Down Expand Up @@ -151,3 +153,5 @@
CRISPY_TEMPLATE_PACK = "bootstrap5"

KAFKA_ADDRESS = os.getenv("KAFKA_ADDRESS", "kafka:9092")

django_stubs_ext.monkeypatch()
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions process_manager/process_manager_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""Module providing functions to interact with the drunc process manager."""

import asyncio
from collections.abc import Iterable
from enum import Enum

from django.conf import settings
from drunc.process_manager.process_manager_driver import ProcessManagerDriver
from drunc.utils.shell_utils import DecodedResponse, create_dummy_token_from_uname
from druncschema.process_manager_pb2 import (
LogRequest,
ProcessInstanceList,
ProcessQuery,
ProcessUUID,
)


def get_process_manager_driver() -> ProcessManagerDriver:
"""Get a ProcessManagerDriver instance."""
token = create_dummy_token_from_uname()
return ProcessManagerDriver(
settings.PROCESS_MANAGER_URL, token=token, aio_channel=True
)


async def _get_session_info() -> ProcessInstanceList:
pmd = get_process_manager_driver()
query = ProcessQuery(names=[".*"])
return await pmd.ps(query)


def get_session_info() -> ProcessInstanceList:
"""Get info about all sessions from process manager."""
return asyncio.run(_get_session_info())


class ProcessAction(Enum):
"""Enum for process actions."""

RESTART = "restart"
KILL = "kill"
FLUSH = "flush"


async def _process_call(uuids: Iterable[str], action: ProcessAction) -> None:
pmd = get_process_manager_driver()
uuids_ = [ProcessUUID(uuid=u) for u in uuids]

match action:
case ProcessAction.RESTART:
for uuid_ in uuids_:
query = ProcessQuery(uuids=[uuid_])
await pmd.restart(query)
case ProcessAction.KILL:
query = ProcessQuery(uuids=uuids_)
await pmd.kill(query)
case ProcessAction.FLUSH:
query = ProcessQuery(uuids=uuids_)
await pmd.flush(query)


def process_call(uuids: Iterable[str], action: ProcessAction) -> None:
"""Perform an action on a process with a given UUID.
Args:
uuids: List of UUIDs of the process to be actioned.
action: Action to be performed {restart,flush,kill}.
"""
return asyncio.run(_process_call(uuids, action))


async def _get_process_logs(uuid: str) -> list[DecodedResponse]:
pmd = get_process_manager_driver()
query = ProcessQuery(uuids=[ProcessUUID(uuid=uuid)])
request = LogRequest(query=query, how_far=100)
return [item async for item in pmd.logs(request)]


def get_process_logs(uuid: str) -> list[DecodedResponse]:
"""Retrieve logs for a process from the process manager.
Args:
uuid: UUID of the process.
Returns:
The process logs.
"""
return asyncio.run(_get_process_logs(uuid))


async def _boot_process(user: str, data: dict[str, str | int]) -> None:
pmd = get_process_manager_driver()
async for item in pmd.dummy_boot(user=user, **data):
pass


def boot_process(user: str, data: dict[str, str | int]) -> None:
"""Boot a process with the given data.
Args:
user: the user to boot the process as.
data: the data for the process.
"""
return asyncio.run(_boot_process(user, data))
12 changes: 6 additions & 6 deletions process_manager/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

from django.urls import include, path

from . import views
from .views import actions, pages, partials

app_name = "process_manager"

partial_urlpatterns = [
path("process_table/", views.process_table, name="process_table"),
path("process_table/", partials.process_table, name="process_table"),
]

urlpatterns = [
path("", views.index, name="index"),
path("process_action/", views.process_action, name="process_action"),
path("logs/<uuid:uuid>", views.logs, name="logs"),
path("boot_process/", views.BootProcessView.as_view(), name="boot_process"),
path("", pages.index, name="index"),
path("process_action/", actions.process_action, name="process_action"),
path("logs/<uuid:uuid>", pages.logs, name="logs"),
path("boot_process/", pages.BootProcessView.as_view(), name="boot_process"),
path("partials/", include(partial_urlpatterns)),
]
221 changes: 0 additions & 221 deletions process_manager/views.py

This file was deleted.

1 change: 1 addition & 0 deletions process_manager/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Module for app view functions."""
Loading

0 comments on commit 560b576

Please sign in to comment.