~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/pidfile.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2008-06-05 03:26:13 UTC
  • mfrom: (6343.3.10 appserver-layer)
  • Revision ID: launchpad@pqm.canonical.com-20080605032613-n7auocswwz5lpdh6
[r=flacoste][!log] Added an AppServerLayer which fires off the app
        server in a child process,
        now providing a framework for tests which need to talk to a real live
        HTTP server (etc.).

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
from canonical.config import config
12
12
 
13
 
def pidfile_path(service_name):
 
13
def pidfile_path(service_name, use_config=None):
14
14
    """Return the full pidfile path for the given service
15
15
 
16
 
    >>> pidfile_path('nuts') == '/tmp/%s-nuts.pid' % config.instance_name
 
16
    >>> pidfile_path('nuts') == '/var/tmp/%s-nuts.pid' % config.instance_name
17
17
    True
 
18
 
 
19
    You can pass in your own config instance to use.
 
20
 
 
21
    >>> class MyConfig:
 
22
    ...     class canonical:
 
23
    ...         pid_dir = '/var/tmp'
 
24
    ...     instance_name = 'blah'
 
25
    >>> pidfile_path('beans', MyConfig)
 
26
    '/var/tmp/blah-beans.pid'
18
27
    """
19
 
    return os.path.join(config.canonical.pid_dir, '%s-%s.pid' % (
20
 
        config.instance_name, service_name
 
28
    if use_config is None:
 
29
        use_config = config
 
30
    return os.path.join(use_config.canonical.pid_dir, '%s-%s.pid' % (
 
31
        use_config.instance_name, service_name
21
32
        ))
22
33
 
23
34
 
96
107
    os.rename(tempname, pidfile)
97
108
 
98
109
 
99
 
def remove_pidfile(service_name):
 
110
def remove_pidfile(service_name, use_config=None):
100
111
    """Remove the PID file.
101
112
 
102
113
    This should only be needed if you are overriding the default SIGTERM
103
114
    signal handler.
 
115
 
 
116
    >>> path = pidfile_path('legumes')
 
117
    >>> file = open(path, 'w')
 
118
    >>> try:
 
119
    ...     print >> file, os.getpid()
 
120
    ... finally:
 
121
    ...     file.close()
 
122
    >>> remove_pidfile('legumes')
 
123
    >>> os.path.exists(path)
 
124
    False
 
125
 
 
126
    You can also pass in your own config instance, in which case the pid does
 
127
    not need to match the current process's pid.
 
128
 
 
129
    >>> class MyConfig:
 
130
    ...     class canonical:
 
131
    ...         pid_dir = '/var/tmp'
 
132
    ...     instance_name = 'blah'
 
133
    >>> path = pidfile_path('pits', MyConfig)
 
134
 
 
135
    >>> file = open(path, 'w')
 
136
    >>> try:
 
137
    ...     print >> file, os.getpid() + 1
 
138
    ... finally:
 
139
    ...     file.close()
 
140
    >>> remove_pidfile('pits', MyConfig)
 
141
    >>> os.path.exists(path)
 
142
    False
104
143
    """
105
 
    pidfile = pidfile_path(service_name)
106
 
    if os.path.exists(pidfile):
107
 
        # Check that the PID is actually ours in case something overwrote
108
 
        # it or we are forked.
109
 
        pid = open(pidfile).read()
110
 
        try:
111
 
            pid = int(pid)
112
 
        except ValueError:
113
 
            raise ValueError("Invalid PID %s" % repr(pid))
114
 
        if pid == os.getpid():
115
 
            os.unlink(pidfile)
116
 
 
117
 
 
118
 
def get_pid(service_name):
 
144
    pidfile = pidfile_path(service_name, use_config)
 
145
    pid = get_pid(service_name, use_config)
 
146
    if pid is None:
 
147
        return
 
148
    if use_config is not None or pid == os.getpid():
 
149
        os.unlink(pidfile)
 
150
 
 
151
 
 
152
def get_pid(service_name, use_config=None):
119
153
    """Return the PID for the given service as an integer, or None
120
154
 
121
155
    May raise a ValueError if the PID file is corrupt.
134
168
    >>> remove_pidfile('nuts')
135
169
    >>> get_pid('nuts') is None
136
170
    True
 
171
 
 
172
    You can also pass in your own config instance.
 
173
 
 
174
    >>> class MyConfig:
 
175
    ...     class canonical:
 
176
    ...         pid_dir = '/var/tmp'
 
177
    ...     instance_name = 'blah'
 
178
    >>> path = pidfile_path('beans', MyConfig)
 
179
    >>> path
 
180
    '/var/tmp/blah-beans.pid'
 
181
    >>> file = open(path, 'w')
 
182
    >>> try:
 
183
    ...     print >> file, 72
 
184
    ... finally:
 
185
    ...     file.close()
 
186
    >>> get_pid('beans', MyConfig)
 
187
    72
 
188
    >>> os.remove(path)
137
189
    """
138
 
    pidfile = pidfile_path(service_name)
 
190
    pidfile = pidfile_path(service_name, use_config)
139
191
    try:
140
192
        pid = open(pidfile).read()
141
193
        return int(pid)