diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2017-11-10 13:20:07 -0500 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2017-11-10 13:20:07 -0500 |
commit | cf9e8d5d377e486991e28b0ebe1f6ad2564c54f5 (patch) | |
tree | d93eb7219a6f57d7e1c54559f32cdb7bc7ee7e1f /cherrypy | |
parent | 33f13b72ba6e3ff0dc9d1953c8697250a340b392 (diff) | |
download | cherrypy-git-cf9e8d5d377e486991e28b0ebe1f6ad2564c54f5.tar.gz |
Extract static method for daemonization for testing #1659.
Diffstat (limited to 'cherrypy')
-rw-r--r-- | cherrypy/process/plugins.py | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/cherrypy/process/plugins.py b/cherrypy/process/plugins.py index f667f278..9b9bc1b8 100644 --- a/cherrypy/process/plugins.py +++ b/cherrypy/process/plugins.py @@ -372,6 +372,15 @@ class Daemonizer(SimplePlugin): 'Daemonizing now may cause strange failures.' % threading.enumerate(), level=30) + self.daemonize(self.stdin, self.stdout, self.stderr, self.bus.log) + + self.finalized = True + start.priority = 65 + + @staticmethod + def daemonize( + stdin='/dev/null', stdout='/dev/null', stderr='/dev/null', + logger=lambda msg: None): # See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 # (or http://www.faqs.org/faqs/unix-faq/programmer/faq/ section 1.7) # and http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012 @@ -388,7 +397,7 @@ class Daemonizer(SimplePlugin): pass else: # This is the first parent. Exit, now that we've forked. - self.bus.log('Forking once.') + logger('Forking once.') os._exit(0) except OSError: # Python raises OSError rather than returning negative numbers. @@ -402,7 +411,7 @@ class Daemonizer(SimplePlugin): try: pid = os.fork() if pid > 0: - self.bus.log('Forking twice.') + logger('Forking twice.') os._exit(0) # Exit second parent except OSError: exc = sys.exc_info()[1] @@ -411,9 +420,9 @@ class Daemonizer(SimplePlugin): os.umask(0) - si = open(self.stdin, 'r') - so = open(self.stdout, 'a+') - se = open(self.stderr, 'a+') + si = open(stdin, 'r') + so = open(stdout, 'a+') + se = open(stderr, 'a+') # os.dup2(fd, fd2) will close fd2 if necessary, # so we don't explicitly close stdin/out/err. @@ -422,9 +431,7 @@ class Daemonizer(SimplePlugin): os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) - self.bus.log('Daemonized to PID: %s' % os.getpid()) - self.finalized = True - start.priority = 65 + logger('Daemonized to PID: %s' % os.getpid()) class PIDFile(SimplePlugin): |