summary refs log tree commit diff
path: root/synapse/metrics
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2019-09-24 14:16:16 +0100
committerErik Johnston <erik@matrix.org>2019-09-24 15:53:17 +0100
commit367158a609d18b6dbd143f8bee0529e743d5b5a4 (patch)
treec458c34ce5351bbbc3a6d450776114e26541e492 /synapse/metrics
parentTest background update (diff)
downloadsynapse-367158a609d18b6dbd143f8bee0529e743d5b5a4.tar.xz
Add wrap_as_background_process decorator.
This does the same thing as `run_as_background_process` but means we
don't need to create superfluous functions.
Diffstat (limited to 'synapse/metrics')
-rw-r--r--synapse/metrics/background_process_metrics.py29
1 files changed, 28 insertions, 1 deletions
diff --git a/synapse/metrics/background_process_metrics.py b/synapse/metrics/background_process_metrics.py
index edd6b42db3..b24e2fab4a 100644
--- a/synapse/metrics/background_process_metrics.py
+++ b/synapse/metrics/background_process_metrics.py
@@ -15,6 +15,8 @@
 
 import logging
 import threading
+from asyncio import iscoroutine
+from functools import wraps
 
 import six
 
@@ -197,7 +199,15 @@ def run_as_background_process(desc, func, *args, **kwargs):
                 _background_processes.setdefault(desc, set()).add(proc)
 
             try:
-                yield func(*args, **kwargs)
+                # We ensureDeferred here to handle coroutines
+                result = func(*args, **kwargs)
+
+                # We need this check because ensureDeferred doesn't like when
+                # func doesn't return a Deferred or coroutine.
+                if iscoroutine(result):
+                    result = defer.ensureDeferred(result)
+
+                return (yield result)
             except Exception:
                 logger.exception("Background process '%s' threw an exception", desc)
             finally:
@@ -208,3 +218,20 @@ def run_as_background_process(desc, func, *args, **kwargs):
 
     with PreserveLoggingContext():
         return run()
+
+
+def wrap_as_background_process(desc):
+    """Decorator that wraps a function that gets called as a background
+    process.
+
+    Equivalent of calling the function with `run_as_background_process`
+    """
+
+    def wrap_as_background_process_inner(func):
+        @wraps(func)
+        def wrap_as_background_process_inner_2(*args, **kwargs):
+            return run_as_background_process(desc, func, *args, **kwargs)
+
+        return wrap_as_background_process_inner_2
+
+    return wrap_as_background_process_inner