Beispiel #1
0
    def assertTimeElapsed(self, time_sleep, max_appx, min_appx, appx):
        t = TaskTimerStats()
        t.enabled = True
        t.run("foo", "bar", [], {})
        self.assertTrue(t.time_start)
        time.sleep(time_sleep)
        time_stop = t.stop()
        self.assertTrue(time_stop)
        self.assertFalse(time_stop > max_appx)
        self.assertFalse(time_stop <= min_appx)

        strstop = str(time_stop)[0:3]
        # Time elapsed is approximately 0.1 seconds.
        self.assertTrue(strstop == appx)
Beispiel #2
0
    def execute(self):
        # Convenience variables
        fun = self.fun
        task_id = self.task_id
        task_name = self.task_name
        args = self.args
        kwargs = self.kwargs

        # Run task loader init handler.
        current_loader.on_task_init(task_id, fun)

        # Backend process cleanup
        default_backend.process_cleanup()

        # Send pre-run signal.
        signals.task_prerun.send(sender=fun, task_id=task_id, task=fun, args=args, kwargs=kwargs)

        retval = None
        timer_stat = TaskTimerStats.start(task_id, task_name, args, kwargs)
        try:
            result = fun(*args, **kwargs)
        except (SystemExit, KeyboardInterrupt):
            raise
        except RetryTaskError, exc:
            retval = self.handle_retry(exc, sys.exc_info())
Beispiel #3
0
 def test_not_enabled(self):
     t = TaskTimerStats()
     t.enabled = False
     self.assertFalse(t.publish(isnot="enabled"))
     self.assertFalse(getattr(t, "time_start", None))
     t.run("foo", "bar", [], {})
     t.stop()
Beispiel #4
0
def jail(task_id, task_name, func, args, kwargs):
    """Wraps the task in a jail, which catches all exceptions, and
    saves the status and result of the task execution to the task
    meta backend.

    If the call was successful, it saves the result to the task result
    backend, and sets the task status to ``"DONE"``.

    If the call results in an exception, it saves the exception as the task
    result, and sets the task status to ``"FAILURE"``.

    :param task_id: The id of the task.
    :param task_name: The name of the task.
    :param func: Callable object to execute.
    :param args: List of positional args to pass on to the function.
    :param kwargs: Keyword arguments mapping to pass on to the function.

    :returns: the function return value on success, or
        the exception instance on failure.

    """
    ignore_result = getattr(func, "ignore_result", False)
    timer_stat = TaskTimerStats.start(task_id, task_name, args, kwargs)

    # See: http://groups.google.com/group/django-users/browse_thread/
    #       thread/78200863d0c07c6d/38402e76cf3233e8?hl=en&lnk=gst&
    #       q=multiprocessing#38402e76cf3233e8
    from django.db import connection
    connection.close()

    # Reset cache connection only if using memcached/libmemcached
    from django.core import cache
    # XXX At Opera we use a custom memcached backend that uses libmemcached
    # instead of libmemcache (cmemcache). Should find a better solution for
    # this, but for now "memcached" should probably be unique enough of a
    # string to not make problems.
    cache_backend = cache.settings.CACHE_BACKEND
    if hasattr(cache, "parse_backend_uri"):
        cache_scheme = cache.parse_backend_uri(cache_backend)[0]
    else:
        # Django <= 1.0.2
        cache_scheme = cache_backend.split(":", 1)[0]
    if "memcached" in cache_scheme:
        cache.cache.close()

    # Backend process cleanup
    default_backend.process_cleanup()

    try:
        result = func(*args, **kwargs)
    except (SystemExit, KeyboardInterrupt):
        raise
    except Exception, exc:
        stored_exc = default_backend.mark_as_failure(task_id, exc)
        type_, _, tb = sys.exc_info()
        retval = ExceptionInfo((type_, stored_exc, tb))
Beispiel #5
0
    def execute(self):
        # Run task loader init handler.
        self.loader.on_task_init(self.task_id, self.task)

        # Backend process cleanup
        self.task.backend.process_cleanup()

        timer_stat = TaskTimerStats.start(self.task_id, self.task_name,
                                          self.args, self.kwargs)
        try:
            return self._trace()
        finally:
            timer_stat.stop()
Beispiel #6
0
    def test_collect_report_dump(self):
        timer1 = TaskTimerStats()
        timer1.enabled = True
        timer1.run("foo", "bar", [], {})
        timer2 = TaskTimerStats()
        timer2.enabled = True
        timer2.run("foo", "bar", [], {})
        timer3 = TaskTimerStats()
        timer3.enabled = True
        timer3.run("foo", "bar", [], {})
        for timer in (timer1, timer2, timer3):
            timer.stop()


        # Collect
        self.s.collect()
        self.assertEquals(self.s.total_tasks_processed, 3)

        # Report
        with OverrideStdout() as outs:
            stdout, stderr = outs
            self.s.report()
            self.assertTrue(
                "Total processing time by task type:" in stdout.getvalue())

        # Dump to cache
        self.s.dump_to_cache()