cubicweb/debug.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Mon, 30 Mar 2020 15:23:56 +0200
changeset 12960 2c3a8a4f5a02
parent 12802 f6662c603730
permissions -rw-r--r--
[server] extract creating a new cnxset in a _new_cnxset() helper So we can move logic specific to _CnxSetPool here.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12755
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     1
# copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     3
#
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     4
# This file is part of CubicWeb.
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     5
#
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     9
# any later version.
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    10
#
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    14
# details.
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    15
#
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    18
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    19
from logging import getLogger
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    20
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    21
logger = getLogger('cubicweb')
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    22
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    23
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    24
SUBSCRIBERS = {
12764
fb97669efcaa [debug-toolbar] add cw general panel with controller
Laurent Peuch <cortex@worlddomination.be>
parents: 12761
diff changeset
    25
    "controller": [],
12755
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    26
    "rql": [],
12761
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12755
diff changeset
    27
    "sql": [],
12766
682d0790997f [debug-toolbar] add registry panel
Laurent Peuch <cortex@worlddomination.be>
parents: 12764
diff changeset
    28
    "vreg": [],
12768
7d2c61d40fe9 [debug-toolbar] add registry decisions debug panel
Laurent Peuch <cortex@worlddomination.be>
parents: 12766
diff changeset
    29
    "registry_decisions": [],
12755
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    30
}
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    31
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    32
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    33
def subscribe_to_debug_channel(channel, subscriber):
12802
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    34
    """
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    35
    Allow to subscribe a callable to one of the debug channels.
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    36
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    37
    The channel must be one of: %s
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    38
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    39
    And the callable need to accept one argument.
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    40
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    41
    It will raise Exception if the channel doesn't exist.
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    42
    """ % SUBSCRIBERS.keys()
12755
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    43
    if channel not in SUBSCRIBERS.keys():
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    44
        raise Exception("debug channel '%s' doesn't exist" % channel)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    45
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    46
    SUBSCRIBERS[channel].append(subscriber)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    47
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    48
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    49
def unsubscribe_to_debug_channel(channel, subscriber):
12802
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    50
    """
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    51
    Unsubscribe a callable from a channel. It will raise Exception if the
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    52
    channel doesn't exist nor
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    53
    """
12755
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    54
    if channel not in SUBSCRIBERS.keys():
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    55
        raise Exception("debug channel '%s' doesn't exist" % channel)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    56
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    57
    if subscriber not in SUBSCRIBERS[channel]:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    58
        raise Exception("subscriber '%s' is not in debug channel '%s'" % (subscriber, channel))
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    59
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    60
    SUBSCRIBERS[channel].remove(subscriber)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    61
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    62
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    63
def emit_to_debug_channel(channel, message):
12802
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    64
    """
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    65
    Send a message to a specified debug channel that will call all its
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    66
    subscribers.
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    67
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    68
    It will raise Exception if the channel doesn't exist.
f6662c603730 [debug_channels/doc] add docstring to functions
Laurent Peuch <cortex@worlddomination.be>
parents: 12768
diff changeset
    69
    """
12755
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    70
    if channel not in SUBSCRIBERS.keys():
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    71
        raise Exception("debug channel '%s' doesn't exist" % channel)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    72
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    73
    for subscriber in SUBSCRIBERS[channel]:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    74
        try:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    75
            subscriber(message)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    76
        except Exception:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    77
            logger.error("Failed to send debug message '%s' to subscriber '%s'", message, subscriber, exc_info=True)