hgweb: add --port-file flag

Summary:
This allows us to specify `-p 0 --port-file X` to get the port assigned in
an atomic way. So there won't be "server failed to start" caused by race
conditions in tests.

Reviewed By: DurhamG

Differential Revision: D6925397

fbshipit-source-id: 5bbb61b7eb2695f0a673afdb0730d2a61827f8b3
This commit is contained in:
Jun Wu 2018-02-07 15:18:27 -08:00 committed by Saurabh Singh
parent 694229631f
commit bc139eb86b
4 changed files with 15 additions and 3 deletions

View File

@ -4681,6 +4681,8 @@ def root(ui, repo):
('', 'webdir-conf', '', _('name of the hgweb config file (DEPRECATED)'),
_('FILE')),
('', 'pid-file', '', _('name of file to write process ID to'), _('FILE')),
('', 'port-file', '',
_("name of file to write port to (useful with '-p 0')"), _('FILE')),
('', 'stdio', None, _('for remote clients (ADVANCED)')),
('', 'cmdserver', '', _('for remote clients (ADVANCED)'), _('MODE')),
('t', 'templates', '', _('web templates to use'), _('TEMPLATE')),

View File

@ -54,7 +54,11 @@ class httpservice(object):
util.setsignalhandler()
self.httpd = server.create_server(self.ui, self.app)
if self.opts['port'] and not self.ui.verbose:
portfile = self.opts.get('port_file')
if portfile:
util.writefile(portfile, '%s' % self.httpd.port)
if (self.opts['port'] or portfile) and not self.ui.verbose:
return
if self.httpd.prefix:
@ -75,7 +79,7 @@ class httpservice(object):
fqaddr = self.httpd.fqaddr
if r':' in fqaddr:
fqaddr = r'[%s]' % fqaddr
if self.opts['port']:
if self.opts['port'] or portfile:
write = self.ui.status
else:
write = self.ui.write

View File

@ -40,6 +40,9 @@ def runservice(opts, parentfn=None, initfn=None, runfn=None, logfile=None,
# Signal child process startup with file removal
lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-')
os.close(lockfd)
portpath = opts.get('port_file')
if portpath:
util.tryunlink(portpath)
try:
if not runargs:
runargs = util.hgcmd() + pycompat.sysargv[1:]
@ -54,6 +57,8 @@ def runservice(opts, parentfn=None, initfn=None, runfn=None, logfile=None,
del runargs[i:i + 2]
break
def condfn():
if portpath and not os.path.exists(portpath):
return False
return not os.path.exists(lockpath)
pid = util.rundetached(runargs, condfn)
if pid < 0:

View File

@ -187,6 +187,7 @@ Show the options for the "serve" command
--pager
--pid-file
--port
--port-file
--prefix
--profile
--quiet
@ -238,7 +239,7 @@ Show all commands + options
pull: update, force, rev, bookmark, branch, ssh, remotecmd, insecure
push: force, rev, bookmark, branch, new-branch, pushvars, ssh, remotecmd, insecure
remove: after, force, subrepos, include, exclude
serve: accesslog, daemon, daemon-postexec, errorlog, port, address, prefix, name, web-conf, webdir-conf, pid-file, stdio, cmdserver, templates, style, ipv6, certificate, read-only, subrepos
serve: accesslog, daemon, daemon-postexec, errorlog, port, address, prefix, name, web-conf, webdir-conf, pid-file, port-file, stdio, cmdserver, templates, style, ipv6, certificate, read-only, subrepos
status: all, modified, added, removed, deleted, clean, unknown, ignored, no-status, terse, copies, print0, rev, change, include, exclude, subrepos, template
summary: remote
update: clean, check, merge, date, rev, tool