Exemplo n.º 1
0
    def emit(self, record):
        """This function is called each time a  message is logged.

           In our design, it's role is to invoke a specific handler
           for corresponding notification type, registered before
           via :meth:`register` function by caller.

           Specific notification handler should prepare translated
           message string, split one event for particular users
           and execute :meth:`send_notification` function for each user
           who should be notified.
        """

        #
        # Emit is called with a lock held, see
        # http://docs.python.org/2/library/logging.html#logging.Handler.handle
        #

        if not NotificationHandler.loaded_notifications and \
                'oioioi.notifications' in settings.INSTALLED_APPS:
            load_modules('notifications')
            NotificationHandler.loaded_notifications = True

        if hasattr(record, 'notification'):
            notification_type = getattr(record, 'notification')
            if notification_type in NotificationHandler.notification_functions:
                notification_function = NotificationHandler \
                    .notification_functions[notification_type]
                notification_function(record)
            else:
                logger.error("Internal error in notification module:"
                        " Tried to handle a non-exisitent notification \"%s\""
                        " Please check, if the notification is"
                        " registered correctly.", notification_type)
Exemplo n.º 2
0
    def emit(self, record):
        """This function is called each time a  message is logged.

           In our design, it's role is to invoke a specific handler
           for corresponding notification type, registered before
           via :meth:`register` function by caller.

           Specific notification handler should prepare translated
           message string, split one event for particular users
           and execute :meth:`send_notification` function for each user
           who should be notified.
        """

        #
        # Emit is called with a lock held, see
        # http://docs.python.org/2/library/logging.html#logging.Handler.handle
        #

        if not NotificationHandler.loaded_notifications:
            load_modules('notifications')
            NotificationHandler.loaded_notifications = True

        if hasattr(record, 'notification'):
            notification_type = getattr(record, 'notification')
            if notification_type in NotificationHandler.notification_functions:
                notification_function = NotificationHandler \
                    .notification_functions[notification_type]
                notification_function(record)
            else:
                logger.error(
                    "Internal error in notification module:"
                    " Tried to handle a non-exisitent notification \"%s\""
                    " Please check, if the notification is"
                    " registered correctly.", notification_type)
Exemplo n.º 3
0
def evalmgr_job(env):
    r"""Takes environment and evaluates it according to its recipe.

    To queue environment one shouldn't call ``evalmgr_job.apply_async``
    or ``evalmgr_job.delay`` directly. Use ``delay_environ`` instead.

    It needs some env elements to be set:

     ``env['recipe']`` should be a list of tuples
         (``phaseName``, ``handler_fun`` [, ``kwargs``])

         handler_fun should satisfy this definition:
             def handler_fun(``env``, ``\*\*kwargs``):

         kwargs will be passed to ``handler_fun``

         current environment would be passed as ``env``

     It's guaranteed that handler functions would be run in order they're
     listed and that environment modified by previous function will be
     passed to next function unchanged.

     Before running the job, its unique identifier is generated and saved in
     ``env['job_id']``.

     A job may be transferred to an external evaluation system using
     ``transfer_job``. A recipe handler which want to transfer job should
     return environ processed by ``transfer_job``, like::

       def prepare_transfer_handler(environ, **kwargs):
         do_some_important_stuff(environ, **kwargs)
         return transfer_job(environ,
                             'some.module.transfer_function',
                             'functions.that.merges.saved.env.and.result')

     If during any of the phases an exception other than Ignore is thrown,
     and ``env['error_handlers']`` is present (it should be in the same
     format as recipe), functions listed there are called with
     additional ``exc_info`` argument, which is the ``sys.exc_info()``
     triple. If any exceptions are thrown there, they are reported to
     the logs and ignored.

     If a celery.exceptions.Ignore is thrown, then execution of environment
     is stopped. One who does it is responsible for handling corresponding
     QueuedJob object.

     Returns environment (a processed copy of given environment).
    """

    # pylint: disable=global-statement,broad-except
    global loaded_controllers

    # load controllers to avoid late mix-ins to them
    if not loaded_controllers:
        load_modules('controllers')
        loaded_controllers = True

    env = copy.deepcopy(env)

    try:
        if 'job_id' not in env:
            raise RuntimeError('No job_id found in environ')
        if 'recipe' not in env:
            raise RuntimeError(
                'No recipe found in job environment. '
                'Did you forget to set environ["run_externally"]?')
        if 'error' in env:
            raise RuntimeError(
                'Error from workers:\n%s\nTB:\n%s' %
                (env['error']['message'], env['error']['traceback']))
        _mark_job_state(env, 'PROGRESS')
        while True:
            recipe = env.get('recipe')
            if not recipe:
                env = _job_finished(env)
                break
            phase = recipe[0]
            env['recipe'] = recipe[1:]
            env = _run_phase(env, phase)
            if 'transfer' in env:
                env = _transfer_job(env, **env.pop('transfer'))
                break
        return env

    # Throwing up celery.exceptions.Ignore is necessary for our custom revoke
    # mechanism. Basically, one of the handlers in job's recipe throws Ignore
    # if the submission had been revoked and this exception has to be passed
    # up so that celery recognizes it and stops execution of this job.
    except Ignore:
        raise
    # pylint: disable=broad-except
    except Exception:
        return _run_error_handlers(env, sys.exc_info())
Exemplo n.º 4
0
                "setting"
            )

        return request.contest

    def _send_error_email(self, request, contests):
        context = self._error_email_context(request, contests)
        message = self._error_email_message(context)
        subject = render_to_string(
                'contestexcl/exclusive_contests_error_subject.txt')
        subject = ' '.join(subject.strip().splitlines())
        mail_admins(subject, message)

    def _error_email_message(self, context):
        return render_to_string(
            'contestexcl/exclusive_contests_error_email.txt',
            context
        )

    def _error_email_context(self, request, contests):
        contests_data = [(cnst.name, cnst.id) for cnst in contests]
        return {
            'contests': contests_data,
            'username': request.user.username
        }

# This causes adding all mixins to ExclusiveContestMiddleware.
# This is needed as middlewares are instantiated before importing
# INSTALLED_APPS, so mixins would be late.
load_modules('middleware')
Exemplo n.º 5
0
def evalmgr_job(env):
    r"""Takes environment and evaluates it according to its recipe.

       It needs some env elements to be set:

        ``env['recipe']`` should be a list of tuples
            (``phaseName``, ``handler_fun`` [, ``kwargs``])

            handler_fun should satisfy this definition:
                def handler_fun(``env``, ``\*\*kwargs``):

            kwargs will be passed to ``handler_fun``

            current environment would be passed as ``env``

        It's guaranteed that handler functions would be run in order they're
        listed and that environment modified by previous function will be
        passed to next function unchanged.

        Before running the job, its unique identifier is generated and saved in
        ``env['job_id']``.

        If during any of the phases an exception is thrown, and
        ``env['error_handlers']`` is present (it should be in the same
        format as recipe), functions listed there are called with
        additional ``exc_info`` argument, which is the ``sys.exc_info()``
        triple. If any exceptions are thrown there, they are reported to
        the logs and ignored.

        Returns environment (a processed copy of given environment).
    """

    # pylint: disable=global-statement,broad-except
    global loaded_controllers

    # load controllers to avoid late mix-ins to them
    if not loaded_controllers:
        load_modules('controllers')
        loaded_controllers = True

    env = copy.deepcopy(env)
    env['job_id'] = evalmgr_job.request.id

    try:
        if 'recipe' not in env:
            raise RuntimeError('No recipe found in job environment. '
                    'Did you forget to set environ["run_externally"]?')
        if 'error' in env:
            raise RuntimeError('Error from workers:\n%s\nTB:\n%s' %
                (env['error']['message'], env['error']['traceback']))
        while True:
            recipe = env.get('recipe')
            if not recipe:
                break
            phase = recipe[0]
            env['recipe'] = recipe[1:]
            env = _run_phase(env, phase)
            if 'workers_jobs' in env:
                send_async_jobs(env)
                break
        return env

    # Throwing up celery.exceptions.Ignore is necessary for our custom revoke
    # mechanism. Basically, one of the handlers in job's recipe throws Ignore
    # if the submission had been revoked and this exception has to be passed
    # up so that celery recognizes it and stops execution of this job.
    except Ignore:
        raise
    # pylint: disable=broad-except
    except Exception:
        return _run_error_handlers(env, sys.exc_info())
Exemplo n.º 6
0
        if not hasattr(request, 'user'):
            raise ImproperlyConfigured(
                "django.contrib.auth.middleware.AuthenticationMiddleware is"
                "required. If you have it installed check if it comes before "
                "ExclusiveContestsMiddleware in your MIDDLEWARE_CLASSES"
                "setting")

        return request.contest

    def _send_error_email(self, request, contests):
        context = self._error_email_context(request, contests)
        message = self._error_email_message(context)
        subject = render_to_string(
            'contestexcl/exclusive-contests-error-subject.txt')
        subject = ' '.join(subject.strip().splitlines())
        mail_admins(subject, message)

    def _error_email_message(self, context):
        return render_to_string(
            'contestexcl/exclusive-contests-error-email.txt', context)

    def _error_email_context(self, request, contests):
        contests_data = [(cnst.name, cnst.id) for cnst in contests]
        return {'contests': contests_data, 'username': request.user.username}


# This causes adding all mixins to ExclusiveContestMiddleware.
# This is needed as middlewares are instantiated before importing
# INSTALLED_APPS, so mixins would be late.
load_modules('middleware')
Exemplo n.º 7
0
def evalmgr_job(env):
    r"""Takes environment and evaluates it according to its recipe.

       To queue environment one shouldn't call ``evalmgr_job.apply_async``
       or ``evalmgr_job.delay`` directly. Use ``delay_environ`` instead.

       It needs some env elements to be set:

        ``env['recipe']`` should be a list of tuples
            (``phaseName``, ``handler_fun`` [, ``kwargs``])

            handler_fun should satisfy this definition:
                def handler_fun(``env``, ``\*\*kwargs``):

            kwargs will be passed to ``handler_fun``

            current environment would be passed as ``env``

        It's guaranteed that handler functions would be run in order they're
        listed and that environment modified by previous function will be
        passed to next function unchanged.

        Before running the job, its unique identifier is generated and saved in
        ``env['job_id']``.

        A job may be transferred to an external evaluation system using
        ``transfer_job``. A recipe handler which want to transfer job should
        return environ processed by ``transfer_job``, like::

          def prepare_transfer_handler(environ, **kwargs):
            do_some_important_stuff(environ, **kwargs)
            return transfer_job(environ,
                                'some.module.transfer_function',
                                'functions.that.merges.saved.env.and.result')

        If during any of the phases an exception other than Ignore is thrown,
        and ``env['error_handlers']`` is present (it should be in the same
        format as recipe), functions listed there are called with
        additional ``exc_info`` argument, which is the ``sys.exc_info()``
        triple. If any exceptions are thrown there, they are reported to
        the logs and ignored.

        If a celery.exceptions.Ignore is thrown, then execution of environment
        is stopped. One who does it is responsible for handling corresponding
        QueuedJob object.

        Returns environment (a processed copy of given environment).
    """

    # pylint: disable=global-statement,broad-except
    global loaded_controllers

    # load controllers to avoid late mix-ins to them
    if not loaded_controllers:
        load_modules('controllers')
        loaded_controllers = True

    env = copy.deepcopy(env)

    try:
        if 'job_id' not in env:
            raise RuntimeError('No job_id found in environ')
        if 'recipe' not in env:
            raise RuntimeError('No recipe found in job environment. '
                    'Did you forget to set environ["run_externally"]?')
        if 'error' in env:
            raise RuntimeError('Error from workers:\n%s\nTB:\n%s' %
                (env['error']['message'], env['error']['traceback']))
        _mark_job_state(env, 'PROGRESS')
        while True:
            recipe = env.get('recipe')
            if not recipe:
                env = _job_finished(env)
                break
            phase = recipe[0]
            env['recipe'] = recipe[1:]
            env = _run_phase(env, phase)
            if 'transfer' in env:
                env = _transfer_job(env, **env.pop('transfer'))
                break
        return env

    # Throwing up celery.exceptions.Ignore is necessary for our custom revoke
    # mechanism. Basically, one of the handlers in job's recipe throws Ignore
    # if the submission had been revoked and this exception has to be passed
    # up so that celery recognizes it and stops execution of this job.
    except Ignore:
        raise
    # pylint: disable=broad-except
    except Exception:
        return _run_error_handlers(env, sys.exc_info())