##// END OF EJS Templates
Refactor commands.serve to allow other commands to run as services....
Bryan O'Sullivan -
r4380:e89f9afc default
parent child Browse files
Show More
@@ -200,6 +200,50 b' def addremove(repo, pats=[], opts={}, wl'
200 if not dry_run:
200 if not dry_run:
201 repo.copy(old, new, wlock=wlock)
201 repo.copy(old, new, wlock=wlock)
202
202
203 def service(opts, parentfn=None, initfn=None, runfn=None):
204 '''Run a command as a service.'''
205
206 if opts['daemon'] and not opts['daemon_pipefds']:
207 rfd, wfd = os.pipe()
208 args = sys.argv[:]
209 args.append('--daemon-pipefds=%d,%d' % (rfd, wfd))
210 pid = os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0),
211 args[0], args)
212 os.close(wfd)
213 os.read(rfd, 1)
214 if parentfn:
215 return parentfn(pid)
216 else:
217 os._exit(0)
218
219 if initfn:
220 initfn()
221
222 if opts['pid_file']:
223 fp = open(opts['pid_file'], 'w')
224 fp.write(str(os.getpid()) + '\n')
225 fp.close()
226
227 if opts['daemon_pipefds']:
228 rfd, wfd = [int(x) for x in opts['daemon_pipefds'].split(',')]
229 os.close(rfd)
230 try:
231 os.setsid()
232 except AttributeError:
233 pass
234 os.write(wfd, 'y')
235 os.close(wfd)
236 sys.stdout.flush()
237 sys.stderr.flush()
238 fd = os.open(util.nulldev, os.O_RDWR)
239 if fd != 0: os.dup2(fd, 0)
240 if fd != 1: os.dup2(fd, 1)
241 if fd != 2: os.dup2(fd, 2)
242 if fd not in (0, 1, 2): os.close(fd)
243
244 if runfn:
245 return runfn()
246
203 class changeset_printer(object):
247 class changeset_printer(object):
204 '''show changeset information when templating not requested.'''
248 '''show changeset information when templating not requested.'''
205
249
@@ -2375,44 +2375,27 b' def serve(ui, repo, **opts):'
2375 raise hg.RepoError(_("There is no Mercurial repository here"
2375 raise hg.RepoError(_("There is no Mercurial repository here"
2376 " (.hg not found)"))
2376 " (.hg not found)"))
2377
2377
2378 if opts['daemon'] and not opts['daemon_pipefds']:
2378 class service:
2379 rfd, wfd = os.pipe()
2379 def init(self):
2380 args = sys.argv[:]
2380 try:
2381 args.append('--daemon-pipefds=%d,%d' % (rfd, wfd))
2381 self.httpd = hgweb.server.create_server(parentui, repo)
2382 pid = os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0),
2382 except socket.error, inst:
2383 args[0], args)
2383 raise util.Abort(_('cannot start server: ') + inst.args[1])
2384 os.close(wfd)
2384
2385 os.read(rfd, 1)
2385 if not ui.verbose: return
2386 os._exit(0)
2386
2387
2387 if httpd.port != 80:
2388 httpd = hgweb.server.create_server(parentui, repo)
2388 ui.status(_('listening at http://%s:%d/\n') %
2389
2389 (httpd.addr, httpd.port))
2390 if ui.verbose:
2390 else:
2391 if httpd.port != 80:
2391 ui.status(_('listening at http://%s/\n') % httpd.addr)
2392 ui.status(_('listening at http://%s:%d/\n') %
2392
2393 (httpd.addr, httpd.port))
2393 def run(self):
2394 else:
2394 self.httpd.serve_forever()
2395 ui.status(_('listening at http://%s/\n') % httpd.addr)
2395
2396
2396 service = service()
2397 if opts['pid_file']:
2397
2398 fp = open(opts['pid_file'], 'w')
2398 cmdutil.service(opts, initfn=service.init, runfn=service.run)
2399 fp.write(str(os.getpid()) + '\n')
2400 fp.close()
2401
2402 if opts['daemon_pipefds']:
2403 rfd, wfd = [int(x) for x in opts['daemon_pipefds'].split(',')]
2404 os.close(rfd)
2405 os.write(wfd, 'y')
2406 os.close(wfd)
2407 sys.stdout.flush()
2408 sys.stderr.flush()
2409 fd = os.open(util.nulldev, os.O_RDWR)
2410 if fd != 0: os.dup2(fd, 0)
2411 if fd != 1: os.dup2(fd, 1)
2412 if fd != 2: os.dup2(fd, 2)
2413 if fd not in (0, 1, 2): os.close(fd)
2414
2415 httpd.serve_forever()
2416
2399
2417 def status(ui, repo, *pats, **opts):
2400 def status(ui, repo, *pats, **opts):
2418 """show changed files in the working directory
2401 """show changed files in the working directory
General Comments 0
You need to be logged in to leave comments. Login now