Skip to content

Commit

Permalink
introduce: OCRD_NETWORK_CLIENT_POLLING_PRINT
Browse files Browse the repository at this point in the history
  • Loading branch information
MehmedGIT committed Oct 1, 2024
1 parent ff81c6b commit f44e28b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/ocrd_network/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def __init__(
self,
server_addr_processing: Optional[str],
timeout: int = config.OCRD_NETWORK_CLIENT_POLLING_TIMEOUT,
wait: int = config.OCRD_NETWORK_CLIENT_POLLING_SLEEP
wait: int = config.OCRD_NETWORK_CLIENT_POLLING_SLEEP,
print_output: bool = config.OCRD_NETWORK_CLIENT_POLLING_PRINT
):
self.log = getLogger(f"ocrd_network.client")
if not server_addr_processing:
Expand All @@ -29,6 +30,7 @@ def __init__(
self.polling_timeout = timeout
self.polling_wait = wait
self.polling_tries = int(timeout / wait)
self.polling_print_output = print_output

def check_deployed_processors(self):
return get_ps_deployed_processors(ps_server_host=self.server_addr_processing)
Expand All @@ -48,11 +50,13 @@ def check_workflow_status(self, workflow_job_id: str):

def poll_job_status(self, job_id: str) -> str:
return poll_job_status_till_timeout_fail_or_success(
ps_server_host=self.server_addr_processing, job_id=job_id, tries=self.polling_tries, wait=self.polling_wait)
ps_server_host=self.server_addr_processing, job_id=job_id, tries=self.polling_tries, wait=self.polling_wait,
print_output=self.polling_print_output)

def poll_workflow_status(self, job_id: str) -> str:
return poll_wf_status_till_timeout_fail_or_success(
ps_server_host=self.server_addr_processing, job_id=job_id, tries=self.polling_tries, wait=self.polling_wait)
ps_server_host=self.server_addr_processing, job_id=job_id, tries=self.polling_tries, wait=self.polling_wait,
print_output=self.polling_print_output)

def send_processing_job_request(self, processor_name: str, req_params: dict) -> str:
return post_ps_processing_request(
Expand Down
14 changes: 9 additions & 5 deletions src/ocrd_network/client_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .constants import JobState, NETWORK_PROTOCOLS


def _poll_endpoint_status(ps_server_host: str, job_id: str, job_type: str, tries: int, wait: int):
def _poll_endpoint_status(ps_server_host: str, job_id: str, job_type: str, tries: int, wait: int, print_output: bool):
if job_type not in ["workflow", "processor"]:
raise ValueError(f"Unknown job type '{job_type}', expected 'workflow' or 'processor'")
job_state = JobState.unset
Expand All @@ -13,18 +13,22 @@ def _poll_endpoint_status(ps_server_host: str, job_id: str, job_type: str, tries
job_state = get_ps_processing_job_status(ps_server_host, job_id)
if job_type == "workflow":
job_state = get_ps_workflow_job_status(ps_server_host, job_id)
if print_output:
print(f"State of the {job_type} job {job_id}: {job_state}")
if job_state == JobState.success or job_state == JobState.failed:
break
tries -= 1
return job_state


def poll_job_status_till_timeout_fail_or_success(ps_server_host: str, job_id: str, tries: int, wait: int) -> JobState:
return _poll_endpoint_status(ps_server_host, job_id, "processor", tries, wait)
def poll_job_status_till_timeout_fail_or_success(
ps_server_host: str, job_id: str, tries: int, wait: int, print_output: bool) -> JobState:
return _poll_endpoint_status(ps_server_host, job_id, "processor", tries, wait, print_output)


def poll_wf_status_till_timeout_fail_or_success(ps_server_host: str, job_id: str, tries: int, wait: int) -> JobState:
return _poll_endpoint_status(ps_server_host, job_id, "workflow", tries, wait)
def poll_wf_status_till_timeout_fail_or_success(
ps_server_host: str, job_id: str, tries: int, wait: int, print_output: bool) -> JobState:
return _poll_endpoint_status(ps_server_host, job_id, "workflow", tries, wait, print_output)


def get_ps_deployed_processors(ps_server_host: str):
Expand Down
7 changes: 6 additions & 1 deletion src/ocrd_utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,18 @@ def _ocrd_download_timeout_parser(val):
config.add("OCRD_NETWORK_CLIENT_POLLING_SLEEP",
description="How many seconds to sleep before trying again.",
parser=int,
default=(True, 30))
default=(True, 10))

config.add("OCRD_NETWORK_CLIENT_POLLING_TIMEOUT",
description="Timeout for a blocking ocrd network client (in seconds).",
parser=int,
default=(True, 3600))

config.add("OCRD_NETWORK_CLIENT_POLLING_PRINT",
description="Timeout for a blocking ocrd network client (in seconds).",
parser=bool,
default=(True, False))

config.add("OCRD_NETWORK_SERVER_ADDR_WORKFLOW",
description="Default address of Workflow Server to connect to (for `ocrd network client workflow`).",
default=(True, ''))
Expand Down

0 comments on commit f44e28b

Please sign in to comment.