Skip to content

Commit

Permalink
Merge pull request #37 from slub/kill-job
Browse files Browse the repository at this point in the history
Improved implementation to kill a job
  • Loading branch information
markusweigelt authored Aug 29, 2023
2 parents f7d1293 + 08b4b60 commit 35689c0
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 289 deletions.
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ __pypackages__/
.python-version
.pdm-python
.pdm.lock
.idea
tests/ocrdmonitor/server/keys/
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
1 change: 0 additions & 1 deletion ocrdmonitor/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ async def read_file(self, path: str) -> str:
async def process_status(self, process_group: int) -> list[ProcessStatus]:
...


class Repositories(NamedTuple):
browser_processes: BrowserProcessRepository
ocrd_jobs: JobRepository
Expand Down
25 changes: 24 additions & 1 deletion ocrdmonitor/server/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
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.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
import logging


@dataclass
class RunningJob:
Expand Down Expand Up @@ -73,4 +77,23 @@ async def jobs(
},
)

@router.get("/kill/{job_pid}", name="jobs.kill")
async def kill(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."
logging.error(
f"Error response {exc.response.status_code} while requesting {exc.request.url!r}."
)

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
23 changes: 17 additions & 6 deletions ocrdmonitor/server/templates/jobs.html.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'base.html.j2' %}

{% block meta %}
<meta http-equiv="refresh" content="5" />
<!--<meta http-equiv="refresh" content="5" />-->
{% endblock %}

{% block headline %}
Expand All @@ -10,8 +10,17 @@

{% block content %}
<script>
function killjob(pid) {
console.log(fetch(`http://ocrd-manager:4004/cancel_job/${pid}`));
async function killjob(jobId) {
const killUrl = "{{ url_for('jobs.kill', job_pid='%JOB_PID%') }}".replace("%JOB_PID%", jobId)
fetch(killUrl)
.then(response => response.json())
.then(response => console.log(response.message))
}
async function handleAsyncRequest(url) {
fetch(url)
.then(response => response.json())
.then(response => console.log(response.message))
}
</script>
<h2 class="title">Active Jobs</h2>
Expand All @@ -36,13 +45,15 @@
<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="killjob({{ job.process_status.pid }})">Kill!</button>
</td>
</tr>
{% endfor %}
</tbody>
Expand Down Expand Up @@ -76,4 +87,4 @@
{% endfor %}
</tbody>
</table>
{% endblock %}
{% endblock %}
Loading

0 comments on commit 35689c0

Please sign in to comment.