-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from slub/fix-jobinfo
bug fixes and more
- Loading branch information
Showing
17 changed files
with
214 additions
and
127 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
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 |
---|---|---|
@@ -1,52 +1,67 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
import logging | ||
from pathlib import Path | ||
from typing import Protocol | ||
from ocrdmonitor import sshremote | ||
|
||
from ocrdmonitor.ocrdjob import OcrdJob | ||
from ocrdmonitor.processstatus import ProcessStatus | ||
from ocrdmonitor.processstatus import ProcessStatus, ProcessState | ||
|
||
if sys.version_info >= (3, 10): | ||
from typing import TypeGuard | ||
else: | ||
from typing_extensions import TypeGuard | ||
|
||
|
||
class ProcessQuery(Protocol): | ||
def __call__(self, process_group: int) -> list[ProcessStatus]: | ||
class RemoteServer(Protocol): | ||
async def read_file(self, path: str) -> str: | ||
... | ||
|
||
async def process_status(self, process_group: int) -> list[ProcessStatus]: | ||
... | ||
|
||
|
||
class OcrdController: | ||
def __init__(self, process_query: ProcessQuery, job_dir: Path) -> None: | ||
self._process_query = process_query | ||
def __init__(self, remote: RemoteServer, job_dir: Path) -> None: | ||
self._remote = remote | ||
self._job_dir = job_dir | ||
logging.info(f"process_query: {remote}") | ||
logging.info(f"job_dir: {job_dir}") | ||
|
||
def get_jobs(self) -> list[OcrdJob]: | ||
def is_ocrd_job(j: OcrdJob | None) -> TypeGuard[OcrdJob]: | ||
return j is not None | ||
|
||
job_candidates = [ | ||
self._try_parse(job_file.read_text()) | ||
self._try_parse(job_file) | ||
for job_file in self._job_dir.iterdir() | ||
if job_file.is_file() | ||
] | ||
|
||
return list(filter(is_ocrd_job, job_candidates)) | ||
|
||
def _try_parse(self, job_str: str) -> OcrdJob | None: | ||
def _try_parse(self, job_file: Path) -> OcrdJob | None: | ||
job_str = job_file.read_text() | ||
try: | ||
return OcrdJob.from_str(job_str) | ||
except (ValueError, KeyError): | ||
except (ValueError, KeyError) as e: | ||
logging.warning(f"found invalid job file: {job_file.name} - {e}") | ||
return None | ||
|
||
def status_for(self, ocrd_job: OcrdJob) -> ProcessStatus | None: | ||
if ocrd_job.pid is None: | ||
async def status_for(self, ocrd_job: OcrdJob) -> ProcessStatus | None: | ||
if ocrd_job.remotedir is None: | ||
return None | ||
|
||
process_statuses = self._process_query(ocrd_job.pid) | ||
matching_statuses = ( | ||
status for status in process_statuses if status.pid == ocrd_job.pid | ||
) | ||
return next(matching_statuses, None) | ||
pid = await self._remote.read_file(f"/data/{ocrd_job.remotedir}/ocrd.pid") | ||
process_statuses = await self._remote.process_status(int(pid)) | ||
|
||
for status in process_statuses: | ||
if status.state == ProcessState.RUNNING: | ||
return status | ||
|
||
if process_statuses: | ||
return process_statuses[0] | ||
|
||
return None |
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
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
Oops, something went wrong.