2014-08-12 10:10:52 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 10:10:52 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2014-08-12 22:14:34 -04:00
|
|
|
|
2017-11-14 06:22:42 -05:00
|
|
|
import logging
|
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
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-04-08 06:01:38 -04:00
|
|
|
def user_left_room(distributor, user, room_id):
|
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
|
|
|
|
|
|
|
|
2019-05-14 13:53:09 -04:00
|
|
|
# XXX: this is no longer used. We should probably kill it.
|
2016-04-08 06:01:38 -04:00
|
|
|
def user_joined_room(distributor, user, room_id):
|
2018-07-18 10:33:13 -04:00
|
|
|
distributor.fire("user_joined_room", user=user, room_id=room_id)
|
2016-04-08 06:01:38 -04:00
|
|
|
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
class Distributor(object):
|
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
|
2018-07-18 10:33:13 -04:00
|
|
|
def __init__(self):
|
2014-08-12 10:10:52 -04:00
|
|
|
self.signals = {}
|
|
|
|
self.pre_registration = {}
|
|
|
|
|
|
|
|
def declare(self, name):
|
|
|
|
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)
|
|
|
|
|
|
|
|
def observe(self, name, observer):
|
|
|
|
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)
|
|
|
|
|
|
|
|
def fire(self, name, *args, **kwargs):
|
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
|
|
|
|
|
|
|
|
|
|
|
class Signal(object):
|
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
|
2018-07-18 10:33:13 -04:00
|
|
|
def __init__(self, name):
|
2014-08-12 10:10:52 -04:00
|
|
|
self.name = name
|
|
|
|
self.observers = []
|
|
|
|
|
|
|
|
def observe(self, observer):
|
|
|
|
"""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)
|
|
|
|
|
|
|
|
def fire(self, *args, **kwargs):
|
|
|
|
"""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
|
|
|
|
2015-05-12 08:14:48 -04:00
|
|
|
def do(observer):
|
|
|
|
def eb(failure):
|
|
|
|
logger.warning(
|
|
|
|
"%s signal observer %s failed: %r",
|
2019-06-20 05:32:02 -04:00
|
|
|
self.name,
|
|
|
|
observer,
|
|
|
|
failure,
|
2015-05-12 08:14:48 -04:00
|
|
|
exc_info=(
|
|
|
|
failure.type,
|
|
|
|
failure.value,
|
2019-06-20 05:32:02 -04:00
|
|
|
failure.getTracebackObject(),
|
|
|
|
),
|
|
|
|
)
|
2016-02-04 05:22:44 -05:00
|
|
|
|
2015-05-12 08:14:48 -04:00
|
|
|
return defer.maybeDeferred(observer, *args, **kwargs).addErrback(eb)
|
|
|
|
|
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
|
|
|
|
2016-02-04 05:22:44 -05:00
|
|
|
def __repr__(self):
|
|
|
|
return "<Signal name=%r>" % (self.name,)
|