diff --git a/src/ocrd_network/cli/client.py b/src/ocrd_network/cli/client.py index 00a185ee1a..ee6c29fc3b 100644 --- a/src/ocrd_network/cli/client.py +++ b/src/ocrd_network/cli/client.py @@ -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 @@ -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(): """ @@ -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): """ @@ -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') diff --git a/src/ocrd_network/client.py b/src/ocrd_network/client.py index 2744cd5f0f..b3872f1aaf 100644 --- a/src/ocrd_network/client.py +++ b/src/ocrd_network/client.py @@ -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, @@ -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) diff --git a/src/ocrd_network/client_utils.py b/src/ocrd_network/client_utils.py index 30b4a2bd96..2dc2805aa0 100644 --- a/src/ocrd_network/client_utils.py +++ b/src/ocrd_network/client_utils.py @@ -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"}) diff --git a/src/ocrd_network/processing_server.py b/src/ocrd_network/processing_server.py index e142802268..34c22e5cf6 100644 --- a/src/ocrd_network/processing_server.py +++ b/src/ocrd_network/processing_server.py @@ -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