"""
sentry.tasks.base
~~~~~~~~~~~~~~~~~

:copyright: (c) 2010-2014 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import

from celery.task import current
from functools import wraps

from sentry.celery import app
from sentry.utils import metrics


def instrumented_task(name, stat_suffix=None, **kwargs):
    def wrapped(func):
        @wraps(func)
        def _wrapped(*args, **kwargs):
            key = 'jobs.duration'
            if stat_suffix:
                instance = '{}.{}'.format(name, stat_suffix(*args, **kwargs))
            else:
                instance = name
            with metrics.timer(key, instance=instance):
                result = func(*args, **kwargs)
            return result
        return app.task(name=name, **kwargs)(_wrapped)
    return wrapped


def retry(func):
    @wraps(func)
    def wrapped(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as exc:
            current.retry(exc=exc)
    return wrapped

@@ -231,16 +231,72 @@
 current%0A
+from raven.contrib.django.models import client as Raven%0A
 from fun
@@ -767,16 +767,41 @@
 tance):%0A
+                try:%0A    
         
@@ -831,32 +831,99 @@
 args, **kwargs)%0A
+                finally:%0A                    Raven.context.clear()%0A
             retu

Clear context for celery tasks