Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Export run_as_background_process from the module API #15577

Merged
merged 7 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/15577.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expose `run_as_background_process` in the module API.
41 changes: 41 additions & 0 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
Collection,
Dict,
Expand Down Expand Up @@ -1666,6 +1667,46 @@ async def set_displayname(
deactivation=deactivation,
)

R = TypeVar("R")

def run_as_background_process(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we expose run_in_background in a different way that avoids all this. Can we follow the same pattern?

"run_in_background",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that is easier, thanks for the suggestion!

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).
H-Shay marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down