Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the network client #1269

Merged
merged 35 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
2927a28
add integration test for client
MehmedGIT Aug 6, 2024
8e7cd3e
fix the test dir path in docker
MehmedGIT Aug 6, 2024
bd16dd7
update network client
MehmedGIT Aug 6, 2024
b2c0675
integration test for client
MehmedGIT Aug 6, 2024
db6e566
Fix flag typo
MehmedGIT Aug 6, 2024
bec81ba
try docker host ip
MehmedGIT Aug 6, 2024
4815896
remove the client server
MehmedGIT Aug 9, 2024
cb3460f
refactor status checks
MehmedGIT Aug 9, 2024
920c1a9
fix test
MehmedGIT Aug 9, 2024
2a843a8
fix: client processing request
MehmedGIT Aug 9, 2024
3a238a7
add: client workflow run
MehmedGIT Aug 9, 2024
50794f9
add timeout and wait to configs
MehmedGIT Aug 9, 2024
cc06fc3
Update src/ocrd_network/client_utils.py
MehmedGIT Aug 12, 2024
4115937
refine status check methods
MehmedGIT Aug 12, 2024
0136db0
add help for new env
MehmedGIT Aug 12, 2024
734bbf0
add cli job status check
MehmedGIT Aug 13, 2024
f86bc23
add: help section to the cli
MehmedGIT Aug 13, 2024
4194f9f
fix: required job id
MehmedGIT Aug 13, 2024
97b3eea
add docstring to cli commands
MehmedGIT Aug 13, 2024
8e7ba26
Fix: rename to block
MehmedGIT Aug 13, 2024
69808b6
Fix: server_utils.py > 404 to 400
MehmedGIT Aug 13, 2024
4de1e83
fix: set ps address if None in constructor
MehmedGIT Aug 13, 2024
d1af85b
fix: check report validation outside try block
MehmedGIT Aug 13, 2024
50f73c5
fix: the annoying string dict
MehmedGIT Aug 13, 2024
8f2861c
add: parameter_override
MehmedGIT Aug 13, 2024
06a371c
add sort to network agents
MehmedGIT Aug 13, 2024
4d85970
add: discovery cli, processors and processor
MehmedGIT Aug 13, 2024
bb3007d
add: check processing job log file
MehmedGIT Aug 13, 2024
ff4243f
fix: exception handling
MehmedGIT Aug 14, 2024
5f746c1
ocrd network client: parse parameters and overrides
kba Aug 14, 2024
8fc8bff
fix parameter parsing again
kba Aug 14, 2024
d73cfaa
Merge pull request #1270 from OCR-D/fix-parsing
MehmedGIT Aug 20, 2024
15cea57
:memo: changelog
kba Aug 22, 2024
18d743a
Merge branch 'master' into extend-network-client
kba Aug 22, 2024
6608539
refactor client cli: process -> run
MehmedGIT Aug 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ network-module-test: assets
INTEGRATION_TEST_IN_DOCKER = docker exec core_test
network-integration-test:
$(DOCKER_COMPOSE) --file tests/network/docker-compose.yml up -d
-$(INTEGRATION_TEST_IN_DOCKER) pytest -k 'test_integration_' -v --ignore-glob="$(TESTDIR)/network/*ocrd_all*.py"
-$(INTEGRATION_TEST_IN_DOCKER) pytest -k 'test_integration_' -v --ignore-glob="tests/network/*ocrd_all*.py"
kba marked this conversation as resolved.
Show resolved Hide resolved
$(DOCKER_COMPOSE) --file tests/network/docker-compose.yml down --remove-orphans

network-integration-test-cicd:
Expand Down
69 changes: 50 additions & 19 deletions src/ocrd_network/cli/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import click
from http.server import BaseHTTPRequestHandler, HTTPServer
from typing import Optional

from ocrd.decorators import parameter_option
from ocrd_network import Client
from ocrd_utils import DEFAULT_METS_BASENAME


STOP_WAITING_CALLBACK = False


class ClientCallbackHandler(BaseHTTPRequestHandler):
"""
A simple callback handler for the network client to be invoked when the Processing Worker
sends requests to the `callback_url` set in the processing request submitted to the Processing Server.
"""

def do_POST(self):
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write("finished".encode("utf-8"))
len = int(self.headers.get("Content-Length", 0))
data = self.rfile.read(len).decode("utf-8")
# TODO: how should the callback-content be handled/printed
print(f"Processor finished: {data}")
global STOP_WAITING_CALLBACK
STOP_WAITING_CALLBACK = True
MehmedGIT marked this conversation as resolved.
Show resolved Hide resolved


class ClientCallbackServer(HTTPServer):
"""
A simple http-server that listens for callbacks from the Processing Server/Worker.
"""
def __init__(self):
super().__init__(server_address=("0.0.0.0", 0), RequestHandlerClass=ClientCallbackHandler)
self.callback_url = f"http://172.17.0.1:{self.server_address[1]}"
MehmedGIT marked this conversation as resolved.
Show resolved Hide resolved
kba marked this conversation as resolved.
Show resolved Hide resolved


@click.group('client')
def client_cli():
"""
Expand Down Expand Up @@ -43,25 +75,27 @@ def processing_cli():
@click.option('--callback-url')
@click.option('--agent-type', default='worker')
def send_processing_request(
address: Optional[str],
processor_name: str,
mets: str,
input_file_grp: str,
output_file_grp: Optional[str],
page_id: Optional[str],
parameter: Optional[dict],
result_queue_name: Optional[str],
callback_url: Optional[str],
# TODO: This is temporally available to toggle
# between the ProcessingWorker/ProcessorServer
agent_type: Optional[str]
address: Optional[str],
processor_name: str,
mets: str,
input_file_grp: str,
output_file_grp: Optional[str],
page_id: Optional[str],
parameter: Optional[dict],
result_queue_name: Optional[str],
callback_url: Optional[str],
# TODO: This is temporally available to toggle
# between the ProcessingWorker/ProcessorServer
agent_type: Optional[str]
):
callback_server = ClientCallbackServer()
req_params = {
"path_to_mets": mets,
"description": "OCR-D Network client request",
"input_file_grps": input_file_grp.split(','),
"parameters": parameter if parameter else {},
"agent_type": agent_type,
"callback_url": callback_server.callback_url
}
if output_file_grp:
req_params["output_file_grps"] = output_file_grp.split(',')
Expand All @@ -72,15 +106,12 @@ def send_processing_request(
if callback_url:
req_params["callback_url"] = callback_url

client = Client(
server_addr_processing=address
)
response = client.send_processing_request(
processor_name=processor_name,
req_params=req_params
)
client = Client(server_addr_processing=address)
response = client.send_processing_request(processor_name=processor_name, req_params=req_params)
processing_job_id = response.get('job_id', None)
print(f"Processing job id: {processing_job_id}")
while not STOP_WAITING_CALLBACK:
MehmedGIT marked this conversation as resolved.
Show resolved Hide resolved
callback_server.handle_request()


@client_cli.group('workflow')
Expand Down
13 changes: 2 additions & 11 deletions src/ocrd_network/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@
from .constants import NETWORK_PROTOCOLS


# TODO: This is just a conceptual implementation and first try to
# trigger further discussions on how this should look like.
class Client:
def __init__(
self,
server_addr_processing: str = config.OCRD_NETWORK_SERVER_ADDR_PROCESSING,
server_addr_workflow: str = config.OCRD_NETWORK_SERVER_ADDR_WORKFLOW,
server_addr_workspace: str = config.OCRD_NETWORK_SERVER_ADDR_WORKSPACE
):
def __init__(self, server_addr_processing: str = config.OCRD_NETWORK_SERVER_ADDR_PROCESSING):
self.log = getLogger(f"ocrd_network.client")
self.server_addr_processing = server_addr_processing
self.server_addr_workflow = server_addr_workflow
self.server_addr_workspace = server_addr_workspace
verify_server_protocol(self.server_addr_processing)

def send_processing_request(self, processor_name: str, req_params: dict):
verify_server_protocol(self.server_addr_processing)
req_url = f"{self.server_addr_processing}/processor/{processor_name}"
req_headers = {"Content-Type": "application/json; charset=utf-8"}
req_json = loads(dumps(req_params))
Expand Down
32 changes: 32 additions & 0 deletions tests/network/test_integration_6_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from click.testing import CliRunner

from src.ocrd_network.constants import AgentType, JobState
from tests.base import assets
from tests.network.config import test_config
from ocrd_network.cli.client import client_cli

PROCESSING_SERVER_URL = test_config.PROCESSING_SERVER_URL


def test_client_processing_processor():
workspace_root = "kant_aufklaerung_1784/data"
path_to_mets = assets.path_to(f"{workspace_root}/mets.xml")
runner = CliRunner()
result = runner.invoke(
client_cli,
args=[
"processing", "processor", "ocrd-dummy",
"--address", PROCESSING_SERVER_URL,
"--mets", path_to_mets,
"--input-file-grp", "OCR-D-IMG",
"--output-file-grp", "OCR-D-DUMMY-TEST-CLIENT",
"--agent-type", AgentType.PROCESSING_WORKER
]
)
# TODO: Do a better result check
assert result.output.count("finished") == 1


def test_client_processing_workflow():
pass

Loading