Patchwork [2,of,4] cmdutil.service: move pidfile writing to a local function

login
register
mail settings
Submitter Siddharth Agarwal
Date Oct. 2, 2013, 11:19 p.m.
Message ID <e4b7774aec7e068677e8.1380755984@dev1091.prn1.facebook.com>
Download mbox | patch
Permalink /patch/2714/
State Accepted
Commit edce20ebe1f3434367796747f802fe74e23ede9b
Headers show

Comments

Siddharth Agarwal - Oct. 2, 2013, 11:19 p.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1380752270 25200
#      Wed Oct 02 15:17:50 2013 -0700
# Node ID e4b7774aec7e068677e811ddf7ddd24ed9ce3d65
# Parent  63a4ad5b170b08c5b1b33b992710c3da3b518b16
cmdutil.service: move pidfile writing to a local function

An upcoming patch will reuse this code.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -468,6 +468,13 @@ 
     runargs=None, appendpid=False):
     '''Run a command as a service.'''
 
+    def writepid(pid):
+        if opts['pid_file']:
+            mode = appendpid and 'a' or 'w'
+            fp = open(opts['pid_file'], mode)
+            fp.write(str(pid) + '\n')
+            fp.close()
+
     if opts['daemon'] and not opts['daemon_pipefds']:
         # Signal child process startup with file removal
         lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-')
@@ -504,11 +511,7 @@ 
     if initfn:
         initfn()
 
-    if opts['pid_file']:
-        mode = appendpid and 'a' or 'w'
-        fp = open(opts['pid_file'], mode)
-        fp.write(str(os.getpid()) + '\n')
-        fp.close()
+    writepid(os.getpid())
 
     if opts['daemon_pipefds']:
         lockpath = opts['daemon_pipefds']