# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2023 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
File Monitor Actions
"""
import os
import queue
import sys
import time
import socket
import subprocess
import logging
from traceback import format_exception
from rattail.config import parse_bool, parse_list
from rattail.monitoring import MonitorAction
from rattail.exceptions import StopProcessing
log = logging.getLogger(__name__)
[docs]
class Action(MonitorAction):
"""
Base class for file monitor actions.
"""
[docs]
class CommandAction(Action):
"""
Simple file monitor action which can execute a command as a subprocess.
"""
def __init__(self, config, cmd):
self.config = config
self.cmd = cmd
self.app = config.get_app()
self.model = self.app.model
self.enum = self.app.enum
def __call__(self, path, **kwargs):
"""
Run the requested command.
"""
filename = os.path.basename(path)
# TODO: this really should default to False instead
shell = parse_bool(kwargs.pop('shell', True))
if shell:
# TODO: probably shoudn't use format() b/c who knows what is in
# that command line, that might trigger errors
cmd = self.cmd.format(path=path, filename=filename)
else:
cmd = []
for term in parse_list(self.cmd):
term = term.replace('{path}', path)
term = term.replace('{filename}', filename)
cmd.append(term)
log.debug("final command to run is: %s", cmd)
subprocess.check_call(cmd, shell=shell)
[docs]
def invoke_action(action, path):
"""
Invoke a single action on a file, retrying as necessary.
"""
app = action.config.get_app()
attempts = 0
errtype = None
while True:
attempts += 1
log.debug(u"invoking action {0} (attempt #{1} of {2}) on file: {3}".format(
repr(action.spec), attempts, action.retry_attempts, repr(path)))
try:
action.action(path, *action.args, **action.kwargs)
except Exception as error:
# If we've reached our final attempt, stop retrying.
if attempts >= action.retry_attempts:
log.debug("attempt #{} failed for action '{}' (giving up) on "
"file: {}".format(attempts, action.spec, path),
exc_info=True)
exc_type, exc, traceback = sys.exc_info()
app.send_email('filemon_action_error', {
'hostname': socket.gethostname(),
'path': path,
'action': action,
'attempts': attempts,
'error': exc,
'traceback': ''.join(format_exception(exc_type, exc, traceback)).strip(),
})
raise
# If this exception is not the first, and is of a different type
# than seen previously, do *not* continue to retry.
if errtype is not None and not isinstance(error, errtype):
log.exception(u"new exception differs from previous one(s), giving up on "
u"action {0} for file: {1}".format(repr(action.spec), repr(path)))
raise
# Record the type of exception seen, and pause for next retry.
log.warning(u"attempt #{0} failed for action {1} on file: {2}".format(
attempts, repr(action.spec), repr(path)), exc_info=True)
errtype = type(error)
log.debug(u"pausing for {0} seconds before making attempt #{1} of {2}".format(
action.retry_delay, attempts + 1, action.retry_attempts))
if action.retry_delay:
time.sleep(action.retry_delay)
else:
# No error, invocation successful.
log.debug(u"attempt #{0} succeeded for action {1} on file: {2}".format(
attempts, repr(action.spec), repr(path)))
break
[docs]
def raise_exception(path, message=u"Fake error for testing"):
"""
File monitor action which always raises an exception.
This is meant to be a simple way to test the error handling of a file
monitor. For example, whether or not file processing continues for
subsequent files after the first error is encountered. If logging
configuration dictates that an email should be sent, it will of course test
that as well.
"""
raise Exception(u'{0}: {1}'.format(message, path))
[docs]
def noop(path):
"""
File monitor action which does nothing at all.
This exists for the sake of tests. I doubt it's useful in any other
context.
"""