diff options
author | Erik Johnston <erik@matrix.org> | 2019-09-26 10:23:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-26 10:23:54 +0100 |
commit | d64b70ada299c58fa7d2b4ed8d83f03594cfd938 (patch) | |
tree | 71dd953794ce378ef0e911665e0c8f5f8b22b57e /synapse/metrics/background_process_metrics.py | |
parent | Merge pull request #6089 from matrix-org/erikj/cleanup_user_ips (diff) | |
parent | Update comments and docstring (diff) | |
download | synapse-d64b70ada299c58fa7d2b4ed8d83f03594cfd938.tar.xz |
Merge pull request #6098 from matrix-org/erikj/cleanup_user_ips_2
Prune old rows in user_ips tables.
Diffstat (limited to 'synapse/metrics/background_process_metrics.py')
-rw-r--r-- | synapse/metrics/background_process_metrics.py | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/synapse/metrics/background_process_metrics.py b/synapse/metrics/background_process_metrics.py index edd6b42db3..c53d2a0d40 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 @@ -173,7 +175,7 @@ def run_as_background_process(desc, func, *args, **kwargs): Args: desc (str): a description for this background process type - func: a function, which may return a Deferred + func: a function, which may return a Deferred or a coroutine args: positional args for func kwargs: keyword args for func @@ -197,7 +199,17 @@ def run_as_background_process(desc, func, *args, **kwargs): _background_processes.setdefault(desc, set()).add(proc) try: - yield func(*args, **kwargs) + result = func(*args, **kwargs) + + # We probably don't have an ensureDeferred in our call stack to handle + # coroutine results, so we need to ensureDeferred here. + # + # But we need this check because ensureDeferred doesn't like being + # called on immediate values (as opposed to Deferreds or coroutines). + if iscoroutine(result): + result = defer.ensureDeferred(result) + + return (yield result) except Exception: logger.exception("Background process '%s' threw an exception", desc) finally: @@ -208,3 +220,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 |