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

Improved implementation to kill a job #37

Merged
merged 10 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ CONTROLLER_PORT_SSH=22

MANAGER_DATA=~/.ssh/id_rsa
MANAGER_KEY=~/
MANAGER_HOST=ocrd-manager
MANAGER_PORT_WEB=4004

MONITOR_IMAGE=ghcr.io/slub/ocrd_monitor:latest
MONITOR_HOST=ocrd-monitor
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ services:
hostname: ${MONITOR_HOST}

environment:
MONITOR_PORT_LOG: ${MONITOR_PORT_LOG}
CONTROLLER: "${CONTROLLER_HOST}:${CONTROLLER_PORT_SSH}"
MANAGER_URL: "http://${MANAGER_HOST}:${MANAGER_PORT_WEB}"
MONITOR_PORT_LOG: ${MONITOR_PORT_LOG}
MONITOR_DB_CONNECTION: "mongodb://${MONITOR_DB_ROOT_USER:-root}:${MONITOR_DB_ROOT_PASSWORD:-root_password}@ocrd-database:27017"

ports:
Expand Down
1 change: 1 addition & 0 deletions init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export OCRD_CONTROLLER__HOST=$CONTROLLER_HOST
export OCRD_CONTROLLER__PORT=$CONTROLLER_PORT
export OCRD_CONTROLLER__USER=admin
export OCRD_CONTROLLER__KEYFILE=~/.ssh/id_rsa
export OCRD_MANAGER__URL=$MANAGER_URL

cd /usr/local/ocrd-monitor
pdm run monitor
24 changes: 22 additions & 2 deletions ocrdmonitor/server/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
from datetime import datetime, timezone
from typing import Iterable

from fastapi import APIRouter, Depends, Request, Response
from fastapi import APIRouter, Depends, Request, Response, status
from fastapi.encoders import jsonable_encoder
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
from fastapi.responses import JSONResponse
from fastapi.templating import Jinja2Templates

from ocrdmonitor.ocrdcontroller import OcrdController
from ocrdmonitor.processstatus import ProcessStatus
from ocrdmonitor.protocols import Environment, OcrdJob, Repositories

import httpx

@dataclass
class RunningJob:
Expand Down Expand Up @@ -69,8 +72,25 @@ async def jobs(
"completed_jobs": sorted(
completed,
key=lambda x: x.time_terminated or now,
),
)
},
)

@router.get("/kill/{job_pid}", name="jobs.kill")
async def kill(
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
job_pid: int
) -> Response:
status_code=status.HTTP_200_OK
message="Job successfully canceled"
try:
async with httpx.AsyncClient() as client:
response = await client.get(environment.settings.ocrd_manager.url + f"/cancel_job/{job_pid}")
response.raise_for_status()
except httpx.HTTPStatusError as exc:
status_code=status.HTTP_409_CONFLICT
message="Job could not be canceled."
print(f"Error response {exc.response.status_code} while requesting {exc.request.url!r}.")
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved

return JSONResponse(status_code=status_code,content=dict(message = message))

return router
4 changes: 3 additions & 1 deletion ocrdmonitor/server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ class OcrdControllerSettings(BaseSettings):
port: int = 22
keyfile: Path = Path.home() / ".ssh" / "id_rsa"

class OcrdManagerSettings(BaseSettings):
url: str

class OcrdLogViewSettings(BaseSettings):
port: int


class OcrdBrowserSettings(BaseSettings):
workspace_dir: Path
mode: Literal["native", "docker"] = "native"
Expand Down Expand Up @@ -79,6 +80,7 @@ class Settings(BaseSettings):
ocrd_browser: OcrdBrowserSettings
ocrd_controller: OcrdControllerSettings
ocrd_logview: OcrdLogViewSettings
ocrd_manager: OcrdManagerSettings

@classmethod
def settings_customise_sources(
Expand Down
2 changes: 1 addition & 1 deletion ocrdmonitor/server/templates/base.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="/">
<b>OCRD-Monitor</b>
<b>OCR-D Monitor</b>
</a>

<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
Expand Down
13 changes: 9 additions & 4 deletions ocrdmonitor/server/templates/jobs.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

{% block content %}
<script>
function killjob(pid) {
console.log(fetch(`http://ocrd-manager:4004/cancel_job/${pid}`));
async function handleAsyncRequest(url) {
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
fetch(url)
.then(response => response.json())
.then(response => console.log(response.message))
}
</script>
<h2 class="title">Active Jobs</h2>
Expand All @@ -36,13 +38,16 @@
<td>{{ job.ocrd_job.time_created }}</td>
<td>{{ job.ocrd_job.task_id }}</td>
<td>{{ job.ocrd_job.process_id }}</td>
<td><a href="{{ url_for('workflows.detail', path=job.ocrd_job.workflow_file) }}">{{ job.ocrd_job.workflow }}</a></td>
<td><a href="{{ url_for('workflows.detail', path=job.ocrd_job.workflow_file) }}">{{ job.ocrd_job.workflow
}}</a></td>
<td>{{ job.process_status.pid }}</td>
<td>{{ job.process_status.state }}</td>
<td>{{ job.process_status.percent_cpu }}</td>
<td>{{ job.process_status.memory }}</td>
<td>{{ job.process_status.cpu_time }}</td>
<td><button onclick="killjob({{ job.process_status.pid }})">Kill!</button></td>
<td><button
onclick="handleAsyncRequest('{{ url_for('jobs.kill', job_pid=job.process_status.pid ) }}')">Kill!</button>
markusweigelt marked this conversation as resolved.
Show resolved Hide resolved
</td>
</tr>
{% endfor %}
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion pdm.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies = [
"Jinja2>=3.1.2",
"websockets>=10.4",
"uvicorn>=0.19.0",
"httpx>=0.23.3",
"httpx>=0.24.1",
"beanie>=1.18.0",
"pydantic-settings>=2.0.2",
"pydantic>=2.1.1",
Expand Down