# Path: kale/exceptions.py
# class ImproperlyConfiguredException(Exception):
# class TaskException(Exception):
# class ChubbyTaskException(TaskException):
# class TimeoutException(TaskException):
# class InvalidTimeLimitTaskException(TaskException):
# class InvalidTaskDelayException(TaskException):
# class BlacklistedException(TaskException):
# class QueueException(Exception):
# class SendMessagesException(QueueException):
# class DeleteMessagesException(QueueException):
# class ChangeMessagesVisibilityException(QueueException):
#     def __init__(self, msg_count):
#     def __init__(self, msg_count):
#     def __init__(self, msg_count):


import errno
import os
import signal
from contextlib import contextmanager
from kale import exceptions
"""Module containing timeout methods for monitoring tasks.

Modeled after:
http://stackoverflow.com/a/601168/854976
"""
from __future__ import absolute_import




@contextmanager
def time_limit(seconds, error_message=os.strerror(errno.ETIME)):
    """Context manager for handling method timeouts.

    Usage:
        try:
            with time_limit(10):
                some_function()
        except TimeoutException:
            # error handling here

    :param int seconds: seconds before timeout.
    :param str error_message: error message.
    :raises: kale.exceptions.TimeoutException
    """

    def _handle_timeout(signum, frame):
        """Handle timout signal."""

# Path: kale/exceptions.py
# class ImproperlyConfiguredException(Exception):
# class TaskException(Exception):
# class ChubbyTaskException(TaskException):
# class TimeoutException(TaskException):
# class InvalidTimeLimitTaskException(TaskException):
# class InvalidTaskDelayException(TaskException):
# class BlacklistedException(TaskException):
# class QueueException(Exception):
# class SendMessagesException(QueueException):
# class DeleteMessagesException(QueueException):
# class ChangeMessagesVisibilityException(QueueException):
#     def __init__(self, msg_count):
#     def __init__(self, msg_count):
#     def __init__(self, msg_count):


# Path: kale/timeout.py
import errno
import os
import signal
from contextlib import contextmanager
from kale import exceptions

"""Module containing timeout methods for monitoring tasks.

Modeled after:
http://stackoverflow.com/a/601168/854976
"""
from __future__ import absolute_import




@contextmanager
def time_limit(seconds, error_message=os.strerror(errno.ETIME)):
    """Context manager for handling method timeouts.

    Usage:
        try:
            with time_limit(10):
                some_function()
        except TimeoutException:
            # error handling here

    :param int seconds: seconds before timeout.
    :param str error_message: error message.
    :raises: kale.exceptions.TimeoutException
    """

    def _handle_timeout(signum, frame):
        """Handle timout signal."""

        raise exceptions.TimeoutException(error_message)