Patchwork [1,of,2] server: refactor 'daemon_postexec' instructions into a dictionary

login
register
mail settings
Submitter Matt Harbison
Date March 31, 2018, 3:39 a.m.
Message ID <6e77d0a68122a6513521.1522467591@Envy>
Download mbox | patch
Permalink /patch/30050/
State Accepted
Headers show

Comments

Matt Harbison - March 31, 2018, 3:39 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1522466506 14400
#      Fri Mar 30 23:21:46 2018 -0400
# Node ID 6e77d0a68122a6513521c94c4b8256b7e6da6f93
# Parent  45aa79668d4ee38866b0829786da076ef59b0381
server: refactor 'daemon_postexec' instructions into a dictionary
Yuya Nishihara - March 31, 2018, 10:07 a.m.
On Fri, 30 Mar 2018 23:39:51 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1522466506 14400
> #      Fri Mar 30 23:21:46 2018 -0400
> # Node ID 6e77d0a68122a6513521c94c4b8256b7e6da6f93
> # Parent  45aa79668d4ee38866b0829786da076ef59b0381
> server: refactor 'daemon_postexec' instructions into a dictionary

Queued, thanks.

> +    if opts['daemon_postexec']:
> +        for inst in opts['daemon_postexec']:
> +            if inst.startswith('unlink:'):
> +                postexecargs['unlink'] = inst[7:]
> +            elif inst.startswith('chdir:'):
> +                postexecargs['chdir'] = inst[6:]
> +            elif inst != 'none':
> +                raise error.Abort(_('invalid value for --daemon-postexec: %s')
> +                                  % inst)

[...]

> -    if opts['daemon_postexec']:
> +    if postexecargs:

Dropped this change since --daemon-postexec argument may be 'none', but it
should still be daemonized.

Patch

diff --git a/mercurial/server.py b/mercurial/server.py
--- a/mercurial/server.py
+++ b/mercurial/server.py
@@ -30,26 +30,35 @@  def runservice(opts, parentfn=None, init
                runargs=None, appendpid=False):
     '''Run a command as a service.'''
 
+    postexecargs = {}
+
+    if opts['daemon_postexec']:
+        for inst in opts['daemon_postexec']:
+            if inst.startswith('unlink:'):
+                postexecargs['unlink'] = inst[7:]
+            elif inst.startswith('chdir:'):
+                postexecargs['chdir'] = inst[6:]
+            elif inst != 'none':
+                raise error.Abort(_('invalid value for --daemon-postexec: %s')
+                                  % inst)
+
     # When daemonized on Windows, redirect stdout/stderr to the lockfile (which
     # gets cleaned up after the child is up and running), so that the parent can
     # read and print the error if this child dies early.  See 594dd384803c.  On
     # other platforms, the child can write to the parent's stdio directly, until
     # it is redirected prior to runfn().
     if pycompat.iswindows and opts['daemon_postexec']:
-        for inst in opts['daemon_postexec']:
-            if inst.startswith('unlink:'):
-                lockpath = inst[7:]
-                if os.path.exists(lockpath):
-                    procutil.stdout.flush()
-                    procutil.stderr.flush()
+        if 'unlink' in postexecargs and os.path.exists(postexecargs['unlink']):
+            procutil.stdout.flush()
+            procutil.stderr.flush()
 
-                    fd = os.open(lockpath,
-                                 os.O_WRONLY | os.O_APPEND | os.O_BINARY)
-                    try:
-                        os.dup2(fd, 1)
-                        os.dup2(fd, 2)
-                    finally:
-                        os.close(fd)
+            fd = os.open(postexecargs['unlink'],
+                         os.O_WRONLY | os.O_APPEND | os.O_BINARY)
+            try:
+                os.dup2(fd, 1)
+                os.dup2(fd, 2)
+            finally:
+                os.close(fd)
 
     def writepid(pid):
         if opts['pid_file']:
@@ -103,21 +112,14 @@  def runservice(opts, parentfn=None, init
     if not opts['daemon']:
         writepid(procutil.getpid())
 
-    if opts['daemon_postexec']:
+    if postexecargs:
         try:
             os.setsid()
         except AttributeError:
             pass
 
-        lockpath = None
-        for inst in opts['daemon_postexec']:
-            if inst.startswith('unlink:'):
-                lockpath = inst[7:]
-            elif inst.startswith('chdir:'):
-                os.chdir(inst[6:])
-            elif inst != 'none':
-                raise error.Abort(_('invalid value for --daemon-postexec: %s')
-                                  % inst)
+        if 'chdir' in postexecargs:
+            os.chdir(postexecargs['chdir'])
         procutil.hidewindow()
         procutil.stdout.flush()
         procutil.stderr.flush()
@@ -137,8 +139,8 @@  def runservice(opts, parentfn=None, init
 
         # Only unlink after redirecting stdout/stderr, so Windows doesn't
         # complain about a sharing violation.
-        if lockpath:
-            os.unlink(lockpath)
+        if 'unlink' in postexecargs:
+            os.unlink(postexecargs['unlink'])
 
     if runfn:
         return runfn()