Source code for rattail.filemon.actions
# -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2014 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 Affero 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
File Monitor Actions
"""
from __future__ import unicode_literals
import os
import sys
import time
import Queue
import logging
log = logging.getLogger(__name__)
[docs]class Action(object):
"""
Base class for file monitor actions.
"""
def __init__(self, config):
self.config = config
def __call__(self, *args, **kwargs):
"""
This method must be implemented in the subclass; it defines what the
action actually *does*. The file monitor will invoke this method for
all new files which are discovered.
"""
raise NotImplementedError
[docs]class StopProcessing(Exception):
"""
Simple exception to indicate action processing should stop. This is really
only useful for tests.
"""
[docs]def invoke_action(action, path):
"""
Invoke a single action on a file, retrying as necessary.
"""
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.exception(u"attempt #{0} failed for action {1} (giving up) on "
u"file: {2}".format(attempts, repr(action.spec), repr(path)))
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.
"""