Passed
Push — master ( de0845...2128be )
by Konstantin
03:03
created

ocrd.cli.processing_worker   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 37
dl 0
loc 62
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A processing_worker_cli() 0 41 3
1
"""
2
OCR-D CLI: start the processing worker
3
4
.. click:: ocrd.cli.processing_worker:processing_worker_cli
5
    :prog: ocrd processing-worker
6
    :nested: full
7
"""
8
import click
9
import logging
10
from ocrd_utils import (
11
    initLogging,
12
    get_ocrd_tool_json
13
)
14
from ocrd_network import (
15
    DatabaseParamType,
16
    ProcessingWorker,
17
    QueueServerParamType,
18
)
19
20
21
@click.command('processing-worker')
22
@click.argument('processor_name', required=True, type=click.STRING)
23
@click.option('-q', '--queue',
24
              default="amqp://admin:admin@localhost:5672/",
25
              help='The URL of the Queue Server, format: amqp://username:password@host:port/vhost',
26
              type=QueueServerParamType())
27
@click.option('-d', '--database',
28
              default="mongodb://localhost:27018",
29
              help='The URL of the MongoDB, format: mongodb://host:port',
30
              type=DatabaseParamType())
31
def processing_worker_cli(processor_name: str, queue: str, database: str):
32
    """
33
    Start a processing worker (a specific ocr-d processor)
34
    """
35
    initLogging()
36
    # TODO: Remove before the release
37
    logging.getLogger('ocrd.network').setLevel(logging.DEBUG)
38
39
    # Get the ocrd_tool dictionary
40
    # ocrd_tool = parse_json_string_with_comments(
41
    #     run([processor_name, '--dump-json'], stdout=PIPE, check=True, universal_newlines=True).stdout
42
    # )
43
44
    ocrd_tool = get_ocrd_tool_json(processor_name)
45
    if not ocrd_tool:
46
        raise Exception(f"The ocrd_tool is empty or missing")
47
48
    try:
49
        processing_worker = ProcessingWorker(
50
            rabbitmq_addr=queue,
51
            mongodb_addr=database,
52
            processor_name=ocrd_tool['executable'],
53
            ocrd_tool=ocrd_tool,
54
            processor_class=None,  # For readability purposes assigned here
55
        )
56
        # The RMQConsumer is initialized and a connection to the RabbitMQ is performed
57
        processing_worker.connect_consumer()
58
        # Start consuming from the queue with name `processor_name`
59
        processing_worker.start_consuming()
60
    except Exception as e:
61
        raise Exception("Processing worker has failed with error") from e
62