~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/server/daemon.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
 
55
55
 
56
56
class Daemon:
57
 
    """ A background process 
58
 
    
 
57
    """ A background process
 
58
 
59
59
    Represent a background process, which may be running or not. The
60
60
    process can be started, stopped, restarted or killed.
61
 
    """       
 
61
    """
62
62
    commandPrefix = 'do_'
63
 
    
 
63
 
64
64
    def __init__(self, name, function, *args, **kw):
65
65
        """ Create a daemon
66
 
        
 
66
 
67
67
        @param name: name of the process (determines pid filename, too)
68
68
        @param function: the server main function, will block until the
69
69
            server is done.
75
75
        self.args = args
76
76
        self.kw = kw
77
77
        self.pidFile = os.path.abspath(name + '.pid')
78
 
    
 
78
 
79
79
    # --------------------------------------------------------------------
80
80
    # Commands
81
81
 
97
97
            self.removePID()
98
98
 
99
99
    def do_stop(self):
100
 
        """ Stop the daemon process 
101
 
        
 
100
        """ Stop the daemon process
 
101
 
102
102
        Terminate or raise an error we can't handle here. On success,
103
103
        the pid file will be cleaned by the terminated process.
104
104
        """
109
109
 
110
110
    def do_kill(self):
111
111
        """ Kill the daemon process
112
 
        
 
112
 
113
113
        Kill or raise an error which we can't handle here. Clean the
114
114
        pid file for the killed process.
115
115
        """
116
116
        running, pid = self.status()
117
117
        if not running:
118
 
            return self.log("%s is not running" % self.name)            
 
118
            return self.log("%s is not running" % self.name)
119
119
        os.kill(pid, signal.SIGKILL)
120
120
        self.removePID()
121
 
                        
 
121
 
122
122
    def do_restart(self):
123
123
        """ stop, wait until pid file gone and start again """
124
124
        running, pid = self.status()
125
125
        if not running:
126
126
            self.log("%s is not running, trying to start" % self.name)
127
127
        else:
128
 
            self.do_stop()            
 
128
            self.do_stop()
129
129
        timeoutSeconds = 2.0
130
130
        start = time.time()
131
131
        while time.time() - start < timeoutSeconds:
132
 
            running, pid =  self.status()
 
132
            running, pid = self.status()
133
133
            if not running:
134
134
                break
135
135
            time.sleep(0.1)
163
163
 
164
164
    def readPID(self):
165
165
        """ Return the pid from the pid file
166
 
        
 
166
 
167
167
        If there is no pid file, return None. If pid file is corrupted,
168
168
        remove it. If its not readable, raise.
169
169
        """
177
177
            self.warn("removing corrupted pid file: %s" % self.pidFile)
178
178
            self.removePID()
179
179
        return pid
180
 
    
 
180
 
181
181
    def daemonize(self):
182
182
        """ Make the current process a daemon
183
 
        
 
183
 
184
184
        See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16
185
185
        """
186
186
        if os.fork():   # launch child and...
188
188
        os.setsid()
189
189
        if os.fork():   # launch child and...
190
190
            os._exit(0) # kill off parent again.
191
 
        os.umask(077)
192
 
        null=os.open('/dev/null', os.O_RDWR)
 
191
        os.umask(0077)
 
192
        null = os.open('/dev/null', os.O_RDWR)
193
193
        for i in range(3):
194
194
            try:
195
195
                os.dup2(null, i)
196
196
            except OSError, e:
197
197
                if e.errno != errno.EBADF:
198
198
                    raise
199
 
        os.close(null)    
 
199
        os.close(null)
200
200
 
201
201
    def writePID(self):
202
202
        pid = str(os.getpid())
203
203
        open(self.pidFile, 'wb').write(pid)
204
 
    
 
204
 
205
205
    def removePID(self):
206
206
        try:
207
207
            os.remove(self.pidFile)
218
218
 
219
219
 
220
220
class DaemonScript(Daemon):
221
 
    """ A script controlling a daemon 
222
 
    
 
221
    """ A script controlling a daemon
 
222
 
223
223
    TODO: add --pid-dir option?
224
224
    """
225
225
 
229
229
        if args == 1:
230
230
            self.usage('nothing to do')
231
231
        elif args > 2:
232
 
            self.usage("too many arguments")        
 
232
            self.usage("too many arguments")
233
233
        try:
234
234
            command = sys.argv[1]
235
235
            func = getattr(self, self.commandPrefix + command)
237
237
        except AttributeError:
238
238
            self.usage('unknown command %r' % command)
239
239
        except Exception, why:
240
 
            sys.exit("error: %s" % str(why))    
 
240
            sys.exit("error: %s" % str(why))
241
241
 
242
242
    def usage(self, message):
243
243
        sys.stderr.write('error: %s\n' % message)
258
258
 
259
259
@copyright: 2004-2005 Thomas Waldmann, Nir Soffer
260
260
@license: GNU GPL, see COPYING for details.
261
 
""" % {'name': self.name,}
 
261
""" % {
 
262
    'name': self.name,
 
263
}
 
264