From b96e183b00146d5dd7590b3b81bf4b027ddbf20e Mon Sep 17 00:00:00 2001 From: "H. Shay" Date: Mon, 8 May 2023 11:08:03 -0700 Subject: [PATCH 1/6] expose `run_as_background_process` in the module API --- synapse/module_api/__init__.py | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 4b59e6825b51..e2e9c35549a7 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -17,6 +17,7 @@ from typing import ( TYPE_CHECKING, Any, + Awaitable, Callable, Collection, Dict, @@ -1666,6 +1667,46 @@ async def set_displayname( deactivation=deactivation, ) + R = TypeVar("R") + + def run_as_background_process( + self, + desc: str, + func: Callable[..., Awaitable[Optional[R]]], + *args: Any, + bg_start_span: bool = True, + **kwargs: Any, + ) -> "defer.Deferred[Optional[R]]": + """Run the given function in its own logcontext, with resource metrics + + This should be used to wrap processes which are fired off to run in the + background, instead of being associated with a particular request. + + It returns a Deferred which completes when the function completes, but it doesn't + follow the synapse logcontext rules, which makes it appropriate for passing to + clock.looping_call and friends (or for firing-and-forgetting in the middle of a + normal synapse async function). + + Added in Synapse v1.84.0 + + Args: + desc: a description for this background process type + func: a function, which may return a Deferred or a coroutine + bg_start_span: Whether to start an opentracing span. Defaults to True. + Should only be disabled for processes that will not log to or tag + a span. + args: positional args for func + kwargs: keyword args for func + + Returns: + Deferred which returns the result of func, or `None` if func raises. + Note that the returned Deferred does not follow the synapse logcontext + rules. + """ + return run_as_background_process( + desc, func, *args, bg_start_span=bg_start_span, **kwargs + ) + class PublicRoomListManager: """Contains methods for adding to, removing from and querying whether a room From a107fe2f9e6f22ad88e983468be945f860a28b71 Mon Sep 17 00:00:00 2001 From: "H. Shay" Date: Thu, 11 May 2023 15:37:20 -0700 Subject: [PATCH 2/6] newsfragment --- changelog.d/15577.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/15577.misc diff --git a/changelog.d/15577.misc b/changelog.d/15577.misc new file mode 100644 index 000000000000..354e48735a1e --- /dev/null +++ b/changelog.d/15577.misc @@ -0,0 +1 @@ +Expose `run_as_background_process` in the module API. \ No newline at end of file From 06462ca671416726d7530f922376997877f7885d Mon Sep 17 00:00:00 2001 From: "H. Shay" Date: Fri, 12 May 2023 09:30:29 -0700 Subject: [PATCH 3/6] export `run_as_background_process` rather than making part of API --- synapse/module_api/__init__.py | 41 +--------------------------------- 1 file changed, 1 insertion(+), 40 deletions(-) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index e2e9c35549a7..6d730244afc1 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -157,6 +157,7 @@ "parse_json_object_from_request", "respond_with_html", "run_in_background", + "run_as_background_process", "cached", "NOT_SPAM", "UserID", @@ -1667,46 +1668,6 @@ async def set_displayname( deactivation=deactivation, ) - R = TypeVar("R") - - def run_as_background_process( - self, - desc: str, - func: Callable[..., Awaitable[Optional[R]]], - *args: Any, - bg_start_span: bool = True, - **kwargs: Any, - ) -> "defer.Deferred[Optional[R]]": - """Run the given function in its own logcontext, with resource metrics - - This should be used to wrap processes which are fired off to run in the - background, instead of being associated with a particular request. - - It returns a Deferred which completes when the function completes, but it doesn't - follow the synapse logcontext rules, which makes it appropriate for passing to - clock.looping_call and friends (or for firing-and-forgetting in the middle of a - normal synapse async function). - - Added in Synapse v1.84.0 - - Args: - desc: a description for this background process type - func: a function, which may return a Deferred or a coroutine - bg_start_span: Whether to start an opentracing span. Defaults to True. - Should only be disabled for processes that will not log to or tag - a span. - args: positional args for func - kwargs: keyword args for func - - Returns: - Deferred which returns the result of func, or `None` if func raises. - Note that the returned Deferred does not follow the synapse logcontext - rules. - """ - return run_as_background_process( - desc, func, *args, bg_start_span=bg_start_span, **kwargs - ) - class PublicRoomListManager: """Contains methods for adding to, removing from and querying whether a room From 2c09e508b2fedf7205316b3e9040f7ad81fc7bde Mon Sep 17 00:00:00 2001 From: "H. Shay" Date: Fri, 12 May 2023 09:39:19 -0700 Subject: [PATCH 4/6] lint --- synapse/module_api/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 6d730244afc1..2c9d181acf36 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -17,7 +17,6 @@ from typing import ( TYPE_CHECKING, Any, - Awaitable, Callable, Collection, Dict, From 70aec5f3d0147cfea8a2e2062af6ae0014805f68 Mon Sep 17 00:00:00 2001 From: Shay Date: Fri, 12 May 2023 09:47:49 -0700 Subject: [PATCH 5/6] Update 15577.misc --- changelog.d/15577.misc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/15577.misc b/changelog.d/15577.misc index 354e48735a1e..4d8abe3a36fb 100644 --- a/changelog.d/15577.misc +++ b/changelog.d/15577.misc @@ -1 +1 @@ -Expose `run_as_background_process` in the module API. \ No newline at end of file +Export `run_as_background_process` via the `module_api` class. From b19a8ab3eeb2965d24ab445e3a5b7492bc76bcea Mon Sep 17 00:00:00 2001 From: Shay Date: Mon, 15 May 2023 12:17:19 -0700 Subject: [PATCH 6/6] Update changelog.d/15577.misc Co-authored-by: Sean Quah <8349537+squahtx@users.noreply.github.com> --- changelog.d/15577.misc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/15577.misc b/changelog.d/15577.misc index 4d8abe3a36fb..74a7f495deae 100644 --- a/changelog.d/15577.misc +++ b/changelog.d/15577.misc @@ -1 +1 @@ -Export `run_as_background_process` via the `module_api` class. +Export `run_as_background_process` from the module API.