Actor: Improve script logging and error handling

- Initialize log file at `/tmp/docling.log` and redirect all output to it
- Remove exit on error trap, now only logs error line numbers
- Use temporary directory for timestamp file
- Capture Docling exit code and handle errors more gracefully
- Update log file references to use `LOG_FILE` variable
- Remove local log file during cleanup

Signed-off-by: Václav Vančura <commit@vancura.dev>
This commit is contained in:
Václav Vančura 2025-01-28 16:09:03 +01:00 committed by Adam Kliment
parent ff7d64b421
commit 7d651eb61f

View File

@ -2,8 +2,19 @@
# --- Setup Error Handling --- # --- Setup Error Handling ---
# Initialize log file first
LOG_FILE="/tmp/docling.log"
touch "$LOG_FILE" || {
echo "Fatal: Cannot create log file at $LOG_FILE"
exit 1
}
# Ensure all output is logged
exec 1> >(tee -a "$LOG_FILE")
exec 2> >(tee -a "$LOG_FILE" >&2)
# Exit the script if any command fails. # Exit the script if any command fails.
trap 'echo "Error on line $LINENO"; exit 1' ERR trap 'echo "Error on line $LINENO"' ERR
set -e set -e
# --- Validate Docling installation --- # --- Validate Docling installation ---
@ -62,19 +73,25 @@ echo "Processing document with Docling CLI..."
echo "Running: $DOC_CONVERT_CMD" echo "Running: $DOC_CONVERT_CMD"
# Create a timestamp file to ensure the document is processed only once. # Create a timestamp file to ensure the document is processed only once.
touch docling.timestamp || { TIMESTAMP_FILE="/tmp/docling.timestamp"
touch "$TIMESTAMP_FILE" || {
echo "Error: Failed to create timestamp file" echo "Error: Failed to create timestamp file"
exit 1 exit 1
} }
# Execute the command and capture both stdout and stderr. # Execute the command
eval "$DOC_CONVERT_CMD" >docling.log 2>&1 || { set +e # Temporarily disable exit on error to handle the error ourselves
cat docling.log eval "$DOC_CONVERT_CMD"
echo "Error: Docling command failed" DOCLING_EXIT_CODE=$?
exit 1 set -e # Re-enable exit on error
}
GENERATED_FILE="$(find . -type f -name "*.${OUTPUT_FORMAT}" -newer docling.timestamp)" # Check if the command failed and handle the error
if [ $DOCLING_EXIT_CODE -ne 0 ]; then
echo "Error: Docling command failed with exit code $DOCLING_EXIT_CODE"
exit 1
fi
GENERATED_FILE="$(find . -type f -name "*.${OUTPUT_FORMAT}" -newer "$TIMESTAMP_FILE")"
# If no generated file is found, exit with an error. # If no generated file is found, exit with an error.
if [ -z "$GENERATED_FILE" ]; then if [ -z "$GENERATED_FILE" ]; then
@ -108,10 +125,10 @@ apify actor:set-value "OUTPUT_RESULT" --contentType "application/$OUTPUT_FORMAT"
exit 1 exit 1
} }
if [ -f "docling.log" ]; then if [ -f "$LOG_FILE" ]; then
if [ -s "docling.log" ]; then if [ -s "$LOG_FILE" ]; then
echo "Log file is not empty, pushing to Key-Value Store (record key: DOCLING_LOG)..." echo "Log file is not empty, pushing to Key-Value Store (record key: DOCLING_LOG)..."
apify actor:set-value "DOCLING_LOG" --contentType "text/plain" <"docling.log" || { apify actor:set-value "DOCLING_LOG" --contentType "text/plain" <"$LOG_FILE" || {
echo "Warning: Failed to push the log file to the Key-Value Store" echo "Warning: Failed to push the log file to the Key-Value Store"
} }
else else
@ -125,7 +142,7 @@ fi
cleanup() { cleanup() {
local exit_code=$? local exit_code=$?
rm -f docling.timestamp docling.log || true rm -f "$TIMESTAMP_FILE" || true
exit $exit_code exit $exit_code
} }