comparison mercurial/cmdutil.py @ 19867:edce20ebe1f3

cmdutil.service: move pidfile writing to a local function An upcoming patch will reuse this code.
author Siddharth Agarwal <sid0@fb.com>
date Wed, 02 Oct 2013 15:17:50 -0700
parents 061ce98c888d
children 0532c8f8e911
comparison
equal deleted inserted replaced
19866:993b24488679 19867:edce20ebe1f3
465 return errors != 0 465 return errors != 0
466 466
467 def service(opts, parentfn=None, initfn=None, runfn=None, logfile=None, 467 def service(opts, parentfn=None, initfn=None, runfn=None, logfile=None,
468 runargs=None, appendpid=False): 468 runargs=None, appendpid=False):
469 '''Run a command as a service.''' 469 '''Run a command as a service.'''
470
471 def writepid(pid):
472 if opts['pid_file']:
473 mode = appendpid and 'a' or 'w'
474 fp = open(opts['pid_file'], mode)
475 fp.write(str(pid) + '\n')
476 fp.close()
470 477
471 if opts['daemon'] and not opts['daemon_pipefds']: 478 if opts['daemon'] and not opts['daemon_pipefds']:
472 # Signal child process startup with file removal 479 # Signal child process startup with file removal
473 lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-') 480 lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-')
474 os.close(lockfd) 481 os.close(lockfd)
502 return 509 return
503 510
504 if initfn: 511 if initfn:
505 initfn() 512 initfn()
506 513
507 if opts['pid_file']: 514 writepid(os.getpid())
508 mode = appendpid and 'a' or 'w'
509 fp = open(opts['pid_file'], mode)
510 fp.write(str(os.getpid()) + '\n')
511 fp.close()
512 515
513 if opts['daemon_pipefds']: 516 if opts['daemon_pipefds']:
514 lockpath = opts['daemon_pipefds'] 517 lockpath = opts['daemon_pipefds']
515 try: 518 try:
516 os.setsid() 519 os.setsid()