sul-dlss/common-accessioning

View on GitHub
bin/abbyy_watcher

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true

# Continually monitor ABBYY directories and report job status to SDR

require_relative '../config/boot'

# Set log level with LOG_LEVEL; share the logger across the stack
logger = Logger.new(STDOUT, progname: 'abbyy_watcher', level: ENV['LOG_LEVEL'] || 'INFO')
Listen.logger = logger

# Set up the watcher and enable polling since we're accessing the Samba share
# If the ABBYY directories aren't set up correctly, bail out
begin
  watcher = Dor::TextExtraction::Abbyy::FileWatcher.new(logger:, listener_options: { force_polling: true })
rescue ArgumentError => e
  logger.fatal e.message
  Honeybadger.notify(e)
  exit Errno::ENOENT::Errno
rescue StandardError => e
  logger.fatal e.message
  Honeybadger.notify(e)
  exit Errno::ENOMSG::Errno # cause is unknown/unexpected, hence the generic error
end

# Run forever; clean up & exit when signalled (e.g. with CTRL-C)
begin
  logger.info 'Starting...'
  logger.info "Watching #{watcher.result_xml_path} and #{watcher.exceptions_path} for ABBYY result XML files"
  watcher.start
  sleep
rescue SignalException => e
  errno = SystemCallError.new(e.message, e.signo)
  logger.info "Received signal #{e.signo} (#{errno.detailed_message}). Exiting..."
  # This is the standard way of stopping this process, so no Honeybadger alert
  watcher.stop
  exit e.signo
rescue StandardError => e
  logger.fatal e.message
  Honeybadger.notify(e)
  exit Errno::ENOMSG::Errno # cause is unknown/unexpected, hence the generic error
end