/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/bzr/smart/signals.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-18 18:23:32 UTC
  • mto: This revision was merged to the branch mainline in revision 7197.
  • Revision ID: jelmer@jelmer.uk-20181118182332-viz1qvqese2mo9i6
Fix some more Bazaar references.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Signal handling for the smart server code."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
import signal
 
22
import weakref
 
23
 
 
24
from ... import trace
 
25
 
 
26
 
 
27
# I'm pretty sure this has to be global, since signal handling is per-process.
 
28
_on_sighup = None
 
29
# TODO: Using a dict means that the order of calls is unordered. We could use a
 
30
#       list and then do something like LIFO ordering. A dict was chosen so
 
31
#       that you could have a key to easily remove your entry. However, you
 
32
#       could just use the callable itself as the indexed part, and even in
 
33
#       large cases, we shouldn't have more than 100 or so callbacks
 
34
#       registered.
 
35
 
 
36
 
 
37
def _sighup_handler(signal_number, interrupted_frame):
 
38
    """This is the actual function that is registered for handling SIGHUP.
 
39
 
 
40
    It will call out to all the registered functions, letting them know that a
 
41
    graceful termination has been requested.
 
42
    """
 
43
    if _on_sighup is None:
 
44
        return
 
45
    trace.mutter('Caught SIGHUP, sending graceful shutdown requests.')
 
46
    for ref in _on_sighup.valuerefs():
 
47
        try:
 
48
            cb = ref()
 
49
            if cb is not None:
 
50
                cb()
 
51
        except KeyboardInterrupt:
 
52
            raise
 
53
        except Exception:
 
54
            trace.mutter('Error occurred while running SIGHUP handlers:')
 
55
            trace.log_exception_quietly()
 
56
 
 
57
 
 
58
def install_sighup_handler():
 
59
    """Setup a handler for the SIGHUP signal."""
 
60
    if getattr(signal, "SIGHUP", None) is None:
 
61
        # If we can't install SIGHUP, there is no reason (yet) to do graceful
 
62
        # shutdown.
 
63
        old_signal = None
 
64
    else:
 
65
        old_signal = signal.signal(signal.SIGHUP, _sighup_handler)
 
66
    old_dict = _setup_on_hangup_dict()
 
67
    return old_signal, old_dict
 
68
 
 
69
 
 
70
def _setup_on_hangup_dict():
 
71
    """Create something for _on_sighup.
 
72
 
 
73
    This is done when we install the sighup handler, and for tests that want to
 
74
    test the functionality. If this hasn'nt been called, then
 
75
    register_on_hangup is a no-op. As is unregister_on_hangup.
 
76
    """
 
77
    global _on_sighup
 
78
    old = _on_sighup
 
79
    _on_sighup = weakref.WeakValueDictionary()
 
80
    return old
 
81
 
 
82
 
 
83
def restore_sighup_handler(orig):
 
84
    """Pass in the returned value from install_sighup_handler to reset."""
 
85
    global _on_sighup
 
86
    old_signal, old_dict = orig
 
87
    if old_signal is not None:
 
88
        signal.signal(signal.SIGHUP, old_signal)
 
89
    _on_sighup = old_dict
 
90
 
 
91
 
 
92
# TODO: Should these be single-use callables? Meaning that once we've triggered
 
93
#       SIGHUP and called them, they should auto-remove themselves? I don't
 
94
#       think so. Callers need to clean up during shutdown anyway, so that we
 
95
#       don't end up with lots of garbage in the _on_sighup dict. On the other
 
96
#       hand, we made _on_sighup a WeakValueDictionary in case cleanups didn't
 
97
#       get fired properly. Maybe we just assume we don't have to do it?
 
98
def register_on_hangup(identifier, a_callable):
 
99
    """Register for us to call a_callable as part of a graceful shutdown."""
 
100
    if _on_sighup is None:
 
101
        return
 
102
    _on_sighup[identifier] = a_callable
 
103
 
 
104
 
 
105
def unregister_on_hangup(identifier):
 
106
    """Remove a callback from being called during sighup."""
 
107
    if _on_sighup is None:
 
108
        return
 
109
    try:
 
110
        del _on_sighup[identifier]
 
111
    except KeyboardInterrupt:
 
112
        raise
 
113
    except Exception:
 
114
        # This usually runs as a tear-down step. So we don't want to propagate
 
115
        # most exceptions.
 
116
        trace.mutter('Error occurred during unregister_on_hangup:')
 
117
        trace.log_exception_quietly()