2014-08-12 10:10:52 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2014-08-12 10:10:52 -04:00
|
|
|
#
|
|
|
|
#
|
2017-11-14 06:22:42 -05:00
|
|
|
import logging
|
2022-05-09 06:27:39 -04:00
|
|
|
from typing import (
|
|
|
|
Any,
|
|
|
|
Awaitable,
|
|
|
|
Callable,
|
|
|
|
Dict,
|
|
|
|
Generic,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
TypeVar,
|
|
|
|
Union,
|
|
|
|
)
|
|
|
|
|
|
|
|
from typing_extensions import ParamSpec
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2017-11-14 06:22:42 -05:00
|
|
|
from twisted.internet import defer
|
2015-05-12 08:14:48 -04:00
|
|
|
|
2019-07-03 10:07:04 -04:00
|
|
|
from synapse.logging.context import make_deferred_yieldable, run_in_background
|
2018-07-18 10:33:13 -04:00
|
|
|
from synapse.metrics.background_process_metrics import run_as_background_process
|
2021-11-16 08:47:36 -05:00
|
|
|
from synapse.types import UserID
|
2020-12-11 14:05:15 -05:00
|
|
|
from synapse.util.async_helpers import maybe_awaitable
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-11-16 08:47:36 -05:00
|
|
|
def user_left_room(distributor: "Distributor", user: UserID, room_id: str) -> None:
|
2018-07-18 10:33:13 -04:00
|
|
|
distributor.fire("user_left_room", user=user, room_id=room_id)
|
2016-04-08 06:01:38 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class Distributor:
|
2014-08-12 10:10:52 -04:00
|
|
|
"""A central dispatch point for loosely-connected pieces of code to
|
|
|
|
register, observe, and fire signals.
|
|
|
|
|
|
|
|
Signals are named simply by strings.
|
|
|
|
|
|
|
|
TODO(paul): It would be nice to give signals stronger object identities,
|
2020-07-09 09:52:58 -04:00
|
|
|
so we can attach metadata, docstrings, detect typos, etc... But this
|
2014-08-12 10:10:52 -04:00
|
|
|
model will do for today.
|
|
|
|
"""
|
|
|
|
|
2021-09-10 12:03:18 -04:00
|
|
|
def __init__(self) -> None:
|
|
|
|
self.signals: Dict[str, Signal] = {}
|
|
|
|
self.pre_registration: Dict[str, List[Callable]] = {}
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2021-09-10 12:03:18 -04:00
|
|
|
def declare(self, name: str) -> None:
|
2014-08-12 10:10:52 -04:00
|
|
|
if name in self.signals:
|
|
|
|
raise KeyError("%r already has a signal named %s" % (self, name))
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
self.signals[name] = Signal(name)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
if name in self.pre_registration:
|
|
|
|
signal = self.signals[name]
|
|
|
|
for observer in self.pre_registration[name]:
|
|
|
|
signal.observe(observer)
|
|
|
|
|
2021-09-10 12:03:18 -04:00
|
|
|
def observe(self, name: str, observer: Callable) -> None:
|
2014-08-12 10:10:52 -04:00
|
|
|
if name in self.signals:
|
|
|
|
self.signals[name].observe(observer)
|
|
|
|
else:
|
|
|
|
# TODO: Avoid strong ordering dependency by allowing people to
|
|
|
|
# pre-register observations on signals that don't exist yet.
|
|
|
|
if name not in self.pre_registration:
|
|
|
|
self.pre_registration[name] = []
|
|
|
|
self.pre_registration[name].append(observer)
|
|
|
|
|
2021-11-16 08:47:36 -05:00
|
|
|
def fire(self, name: str, *args: Any, **kwargs: Any) -> None:
|
2018-07-18 09:35:24 -04:00
|
|
|
"""Dispatches the given signal to the registered observers.
|
|
|
|
|
|
|
|
Runs the observers as a background process. Does not return a deferred.
|
|
|
|
"""
|
2014-08-12 10:10:52 -04:00
|
|
|
if name not in self.signals:
|
|
|
|
raise KeyError("%r does not have a signal named %s" % (self, name))
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
run_as_background_process(name, self.signals[name].fire, *args, **kwargs)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
|
2022-05-09 06:27:39 -04:00
|
|
|
P = ParamSpec("P")
|
|
|
|
R = TypeVar("R")
|
|
|
|
|
|
|
|
|
|
|
|
class Signal(Generic[P]):
|
2014-08-12 10:10:52 -04:00
|
|
|
"""A Signal is a dispatch point that stores a list of callables as
|
|
|
|
observers of it.
|
|
|
|
|
|
|
|
Signals can be "fired", meaning that every callable observing it is
|
|
|
|
invoked. Firing a signal does not change its state; it can be fired again
|
|
|
|
at any later point. Firing a signal passes any arguments from the fire
|
|
|
|
method into all of the observers.
|
|
|
|
"""
|
|
|
|
|
2021-09-10 12:03:18 -04:00
|
|
|
def __init__(self, name: str):
|
|
|
|
self.name: str = name
|
2022-05-09 06:27:39 -04:00
|
|
|
self.observers: List[Callable[P, Any]] = []
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2022-05-09 06:27:39 -04:00
|
|
|
def observe(self, observer: Callable[P, Any]) -> None:
|
2014-08-12 10:10:52 -04:00
|
|
|
"""Adds a new callable to the observer list which will be invoked by
|
|
|
|
the 'fire' method.
|
|
|
|
|
|
|
|
Each observer callable may return a Deferred."""
|
|
|
|
self.observers.append(observer)
|
|
|
|
|
2022-05-09 06:27:39 -04:00
|
|
|
def fire(self, *args: P.args, **kwargs: P.kwargs) -> "defer.Deferred[List[Any]]":
|
2014-08-12 10:10:52 -04:00
|
|
|
"""Invokes every callable in the observer list, passing in the args and
|
|
|
|
kwargs. Exceptions thrown by observers are logged but ignored. It is
|
|
|
|
not an error to fire a signal with no observers.
|
|
|
|
|
|
|
|
Returns a Deferred that will complete when all the observers have
|
|
|
|
completed."""
|
2015-05-08 14:53:34 -04:00
|
|
|
|
2022-05-09 06:27:39 -04:00
|
|
|
async def do(observer: Callable[P, Union[R, Awaitable[R]]]) -> Optional[R]:
|
2020-09-09 12:22:00 -04:00
|
|
|
try:
|
2020-12-11 14:05:15 -05:00
|
|
|
return await maybe_awaitable(observer(*args, **kwargs))
|
2020-09-09 12:22:00 -04:00
|
|
|
except Exception as e:
|
2015-05-12 08:14:48 -04:00
|
|
|
logger.warning(
|
2021-02-16 17:32:34 -05:00
|
|
|
"%s signal observer %s failed: %r",
|
|
|
|
self.name,
|
|
|
|
observer,
|
|
|
|
e,
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2022-05-09 06:27:39 -04:00
|
|
|
return None
|
2016-02-04 05:22:44 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
deferreds = [run_in_background(do, o) for o in self.observers]
|
2015-05-12 08:14:48 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
return make_deferred_yieldable(
|
|
|
|
defer.gatherResults(deferreds, consumeErrors=True)
|
|
|
|
)
|
2015-05-12 08:14:48 -04:00
|
|
|
|
2021-11-16 08:47:36 -05:00
|
|
|
def __repr__(self) -> str:
|
2016-02-04 05:22:44 -05:00
|
|
|
return "<Signal name=%r>" % (self.name,)
|