Skip to content

Commit

Permalink
Merge pull request #60 from slub/add-task-action-messages
Browse files Browse the repository at this point in the history
Send ocr processing messages to active mq
  • Loading branch information
markusweigelt authored Jun 8, 2023
2 parents 6143673 + b4e2deb commit d4a1229
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ MANAGER_DATA=~/

MQ_HOST=kitodo-mq
MQ_PORT=61616
MQ_CLIENT_QUEUE=FinalizeTaskQueue
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LABEL \
org.opencontainers.image.revision=$VCS_REF \
org.opencontainers.image.created=$BUILD_DATE

ARG KITODO_MQ_CLIENT_VERSION=0.2
ARG KITODO_MQ_CLIENT_VERSION=0.3

ENV HOME=/

Expand Down Expand Up @@ -48,10 +48,13 @@ COPY kitodo-activemq-client-log4j2.properties /opt/kitodo-activemq-client/log4j2
ENV ACTIVEMQ_CLIENT_LOG4J2 /opt/kitodo-activemq-client/log4j2.properties

# add ActiveMQ client library
ADD https://github.com/markusweigelt/kitodo-activemq-client/releases/download/${KITODO_MQ_CLIENT_VERSION}/kitodo-activemq-client-${KITODO_MQ_CLIENT_VERSION}.jar /opt/kitodo-activemq-client
ADD https://github.com/slub/kitodo-production-activemq/releases/download/${KITODO_MQ_CLIENT_VERSION}/kitodo-activemq-client-${KITODO_MQ_CLIENT_VERSION}.jar /opt/kitodo-activemq-client
ENV ACTIVEMQ_CLIENT /opt/kitodo-activemq-client/kitodo-activemq-client-${KITODO_MQ_CLIENT_VERSION}.jar
RUN chmod go+r $ACTIVEMQ_CLIENT

# configure ActiveMQ client queue
ENV ACTIVEMQ_CLIENT_QUEUE FinalizeTaskQueue

# workaround for OCR-D/core#983
RUN pip install ocrd
# install mets-mods2tei and page-to-alto
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ services:
- UMASK=${MANAGER_ENV_UMASK}
- "CONTROLLER=${CONTROLLER_HOST}:${CONTROLLER_PORT_SSH}"
- "ACTIVEMQ=${MQ_HOST}:${MQ_PORT}"
- ACTIVEMQ_CLIENT_QUEUE=${MQ_CLIENT_QUEUE}

ports:
- ${MANAGER_PORT_SSH}:22
Expand Down
65 changes: 61 additions & 4 deletions ocrd_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ TASK=$(basename $0)

logerr() {
logger -p user.info -t $TASK "terminating with error \$?=$? from ${BASH_COMMAND} on line $(caller)"

kitodo_production_task_action_error_open
}

stopbg() {
Expand Down Expand Up @@ -233,16 +235,71 @@ post_process_to_mets() {
# perhaps if URL_PREFIX: mm-update -m "$METS_PATH" validate -u $URL_PREFIX
}

close_task() {
if test -n "$ACTIVEMQ" -a -n "$ACTIVEMQ_CLIENT" -a -n "$TASK_ID" -a -n "$PROCESS_ID"; then
java -Dlog4j2.configurationFile=$ACTIVEMQ_CLIENT_LOG4J2 -jar "$ACTIVEMQ_CLIENT" "tcp://$ACTIVEMQ?closeAsync=false" "KitodoProduction.FinalizeStep.Queue" $TASK_ID $PROCESS_ID
kitodo_production_task_action() {
ACTION=""
MESSAGE="${2}"
case ${1} in
1)
ACTION="COMMENT"
;;
2)
ACTION="ERROR_OPEN"
;;
3)
ACTION="ERROR_CLOSE"
;;
4)
ACTION="PROCESS"
;;
5)
ACTION="CLOSE"
;;
*)
logger -p user.error -t $TASK "Unknown task action type"
;;
esac

if test -n "$ACTIVEMQ" -a "$ACTIVEMQ" != ":" -a -n "$TASK_ID" -a -n "$ACTION"; then
if test "$ACTIVEMQ_CLIENT_QUEUE" == "TaskActionQueue"; then
java -Dlog4j2.configurationFile=$ACTIVEMQ_CLIENT_LOG4J2 -jar "$ACTIVEMQ_CLIENT" "tcp://$ACTIVEMQ?closeAsync=false" "$ACTIVEMQ_CLIENT_QUEUE" $TASK_ID "$MESSAGE" "$ACTION"
elif test "$ACTION" == "CLOSE"; then
java -Dlog4j2.configurationFile=$ACTIVEMQ_CLIENT_LOG4J2 -jar "$ACTIVEMQ_CLIENT" "tcp://$ACTIVEMQ?closeAsync=false" "$ACTIVEMQ_CLIENT_QUEUE" $TASK_ID "$MESSAGE"
fi
fi
logret # communicate retval 0
}

kitodo_production_task_action_comment() {
if test -n "${1}"; then
kitodo_production_task_action 1 "${1}"
else
logger -p user.info -t $TASK "Could not send task info cause no message was specified"
fi
}

kitodo_production_task_action_error_open() {
MESSAGE="${1:-Error occured during the OCR processing}"
kitodo_production_task_action 2 "$MESSAGE"
}

kitodo_production_task_action_error_close() {
MESSAGE="${1:-OCR processing error has been fixed}"
kitodo_production_task_action 3 "$MESSAGE"
}

kitodo_production_task_action_process() {
MESSAGE="${1:-OCR processing started}"
kitodo_production_task_action 4 "$MESSAGE"
}

kitodo_production_task_action_close() {
MESSAGE="${1:-OCR processing completed}"
kitodo_production_task_action 5 "$MESSAGE"
}

# exit in async or sync mode
close() {
if test -n "$ACTIVEMQ" -a -n "$ACTIVEMQ_CLIENT" -a -n "$TASK_ID" -a -n "$PROCESS_ID"; then
if test -n "$ACTIVEMQ" -a "$ACTIVEMQ" != ":" -a -n "$TASK_ID"; then
logger -p user.info -t $TASK "ocr_exit in async mode - immediate termination of the script"
# prevent any RETVAL from being written yet
trap - EXIT
Expand Down
5 changes: 4 additions & 1 deletion process_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ ENVIRONMENT VARIABLES:
CONTROLLER: host name and port of OCR-D Controller for processing
ACTIVEMQ: URL of ActiveMQ server for result callback (optional)
ACTIVEMQ_QUEUE: Protocol type handling result callbacks. Choose between "FinalizeTaskQueue" (default) or "TaskActionQueue" (optional)
ACTIVEMQ_CLIENT: path to ActiveMQ client library JAR file (optional)
EOF
exit;;
Expand Down Expand Up @@ -88,6 +89,8 @@ init "$@"

pre_sync_workdir

kitodo_production_task_action_process

ocrd_exec ocrd_import_workdir ocrd_validate_workflow ocrd_process_workflow

post_sync_workdir
Expand All @@ -96,7 +99,7 @@ init "$@"

post_process_to_procdir

close_task
kitodo_production_task_action_close

) |& tee -a $WORKDIR/ocrd.log 2>&1 | logger -p user.info -t $TASK &>/dev/null & # without output redirect, ssh will not close the connection upon exit, cf. #9

Expand Down
4 changes: 3 additions & 1 deletion process_mets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ init "$@"

pre_sync_workdir

kitodo_production_task_action_process

ocrd_exec ocrd_enter_workdir ocrd_validate_workflow ocrd_process_workflow

post_sync_workdir
Expand All @@ -90,7 +92,7 @@ init "$@"

post_process_to_mets

close_task
kitodo_production_task_action_close

) |& tee -a $WORKDIR/ocrd.log 2>&1 | logger -p user.info -t $TASK &>/dev/null & # without output redirect, ssh will not close the connection upon exit, cf. #9

Expand Down

0 comments on commit d4a1229

Please sign in to comment.