Skip to content

Commit

Permalink
add: discovery cli, processors and processor
Browse files Browse the repository at this point in the history
  • Loading branch information
MehmedGIT committed Aug 13, 2024
1 parent 06a371c commit 4d85970
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
40 changes: 35 additions & 5 deletions src/ocrd_network/cli/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import click
from json import dumps, loads
from json import dumps
from typing import Optional
from ocrd.decorators.parameter_option import parameter_option, parameter_override_option
from ocrd_utils import DEFAULT_METS_BASENAME
Expand All @@ -23,6 +23,34 @@ def discovery_cli():
pass


@discovery_cli.command('processors')
@click.option('--address',
help='The address of the Processing Server. If not provided, '
'the "OCRD_NETWORK_SERVER_ADDR_PROCESSING" env variable is used by default')
def check_deployed_processors(address: Optional[str]):
"""
Get a list of deployed processing workers/processor servers.
Each processor is shown only once regardless of the amount of deployed instances.
"""
client = Client(server_addr_processing=address)
processors_list = client.check_deployed_processors()
print(dumps(processors_list, indent=4))


@discovery_cli.command('processor')
@click.option('--address',
help='The address of the Processing Server. If not provided, '
'the "OCRD_NETWORK_SERVER_ADDR_PROCESSING" env variable is used by default')
@click.argument('processor_name', required=True, type=click.STRING)
def check_processor_ocrd_tool(address: Optional[str], processor_name: str):
"""
Get the json tool of a deployed processor specified with `processor_name`
"""
client = Client(server_addr_processing=address)
ocrd_tool = client.check_deployed_processor_ocrd_tool(processor_name=processor_name)
print(dumps(ocrd_tool, indent=4))


@client_cli.group('processing')
def processing_cli():
"""
Expand All @@ -32,8 +60,9 @@ def processing_cli():


@processing_cli.command('check-status')
@click.option('--address', help='The address of the Processing Server. If not provided, '
'the "OCRD_NETWORK_SERVER_ADDR_PROCESSING" env variable is used by default')
@click.option('--address',
help='The address of the Processing Server. If not provided, '
'the "OCRD_NETWORK_SERVER_ADDR_PROCESSING" env variable is used by default')
@click.option('-j', '--processing-job-id', required=True)
def check_processing_job_status(address: Optional[str], processing_job_id: str):
"""
Expand All @@ -47,8 +76,9 @@ def check_processing_job_status(address: Optional[str], processing_job_id: str):

@processing_cli.command('processor')
@click.argument('processor_name', required=True, type=click.STRING)
@click.option('--address', help='The address of the Processing Server. If not provided, '
'the "OCRD_NETWORK_SERVER_ADDR_PROCESSING" env variable is used by default')
@click.option('--address',
help='The address of the Processing Server. If not provided, '
'the "OCRD_NETWORK_SERVER_ADDR_PROCESSING" env variable is used by default')
@click.option('-m', '--mets', required=True, default=DEFAULT_METS_BASENAME)
@click.option('-I', '--input-file-grp', default='OCR-D-INPUT')
@click.option('-O', '--output-file-grp', default='OCR-D-OUTPUT')
Expand Down
9 changes: 9 additions & 0 deletions src/ocrd_network/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from ocrd_utils import config, getLogger, LOG_FORMAT
from .client_utils import (
get_ps_deployed_processors,
get_ps_deployed_processor_ocrd_tool,
get_ps_processing_job_status,
get_ps_workflow_job_status,
poll_job_status_till_timeout_fail_or_success,
Expand All @@ -26,6 +28,13 @@ def __init__(
self.polling_wait = wait
self.polling_tries = int(timeout/wait)

def check_deployed_processors(self):
return get_ps_deployed_processors(ps_server_host=self.server_addr_processing)

def check_deployed_processor_ocrd_tool(self, processor_name: str):
return get_ps_deployed_processor_ocrd_tool(
ps_server_host=self.server_addr_processing, processor_name=processor_name)

def check_job_status(self, job_id: str):
return get_ps_processing_job_status(self.server_addr_processing, processing_job_id=job_id)

Expand Down
14 changes: 14 additions & 0 deletions src/ocrd_network/client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ def poll_wf_status_till_timeout_fail_or_success(ps_server_host: str, job_id: str
return _poll_endpoint_status(ps_server_host, job_id, "workflow", tries, wait)


def get_ps_deployed_processors(ps_server_host: str):
request_url = f"{ps_server_host}/processor"
response = request_get(url=request_url, headers={"accept": "application/json; charset=utf-8"})
assert response.status_code == 200, f"Processing server: {request_url}, {response.status_code}"
return response.json()


def get_ps_deployed_processor_ocrd_tool(ps_server_host: str, processor_name: str):
request_url = f"{ps_server_host}/processor/info/{processor_name}"
response = request_get(url=request_url, headers={"accept": "application/json; charset=utf-8"})
assert response.status_code == 200, f"Processing server: {request_url}, {response.status_code}"
return response.json()


def get_ps_processing_job_status(ps_server_host: str, processing_job_id: str) -> str:
request_url = f"{ps_server_host}/processor/job/{processing_job_id}"
response = request_get(url=request_url, headers={"accept": "application/json; charset=utf-8"})
Expand Down
2 changes: 1 addition & 1 deletion src/ocrd_network/processing_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ async def list_processors(self) -> List[str]:
# There is no caching on the Processing Server side
processor_names_list = self.deployer.find_matching_network_agents(
docker_only=False, native_only=False, worker_only=False, server_only=False,
str_names_only=True, unique_only=True
str_names_only=True, unique_only=True, sort=True
)
return processor_names_list

Expand Down

0 comments on commit 4d85970

Please sign in to comment.