]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Make Daemon pidfile arg optional
authorAngus Lees <gus@inodes.org>
Fri, 15 May 2015 06:39:58 +0000 (16:39 +1000)
committerAngus Lees <gus@inodes.org>
Wed, 24 Jun 2015 23:35:55 +0000 (09:35 +1000)
Some uses of Daemon don't need a pidfile (eg: privsep).

Change-Id: Ic49e841f2f80000f1fd2f2738ab24e703b370d73

neutron/agent/linux/daemon.py
neutron/tests/unit/agent/linux/test_daemon.py

index b4c7853b54a7327fbac63cf28db2ff7a5e9ee6a7..670a239898b3a533d5649c4e29d0e191f7eee0ea 100644 (file)
@@ -160,11 +160,13 @@ class Daemon(object):
     def __init__(self, pidfile, stdin=DEVNULL, stdout=DEVNULL,
                  stderr=DEVNULL, procname='python', uuid=None,
                  user=None, group=None, watch_log=True):
+        """Note: pidfile may be None."""
         self.stdin = stdin
         self.stdout = stdout
         self.stderr = stderr
         self.procname = procname
-        self.pidfile = Pidfile(pidfile, procname, uuid)
+        self.pidfile = (Pidfile(pidfile, procname, uuid)
+                        if pidfile is not None else None)
         self.user = user
         self.group = group
         self.watch_log = watch_log
@@ -202,13 +204,15 @@ class Daemon(object):
         os.dup2(stdout.fileno(), sys.stdout.fileno())
         os.dup2(stderr.fileno(), sys.stderr.fileno())
 
-        # write pidfile
-        atexit.register(self.delete_pid)
-        signal.signal(signal.SIGTERM, self.handle_sigterm)
-        self.pidfile.write(os.getpid())
+        if self.pidfile is not None:
+            # write pidfile
+            atexit.register(self.delete_pid)
+            signal.signal(signal.SIGTERM, self.handle_sigterm)
+            self.pidfile.write(os.getpid())
 
     def delete_pid(self):
-        os.remove(str(self.pidfile))
+        if self.pidfile is not None:
+            os.remove(str(self.pidfile))
 
     def handle_sigterm(self, signum, frame):
         sys.exit(0)
@@ -216,7 +220,7 @@ class Daemon(object):
     def start(self):
         """Start the daemon."""
 
-        if self.pidfile.is_running():
+        if self.pidfile is not None and self.pidfile.is_running():
             self.pidfile.unlock()
             LOG.error(_LE('Pidfile %s already exist. Daemon already '
                           'running?'), self.pidfile)
index 1ff0cc12268223ab7ad428fcb330e8cb6ce9cf52..22af854e862ad542dc1c26cd034433a62d563353 100644 (file)
@@ -223,6 +223,11 @@ class TestDaemon(base.BaseTestCase):
         d = daemon.Daemon('pidfile')
         self.assertEqual(d.procname, 'python')
 
+    def test_init_nopidfile(self):
+        d = daemon.Daemon(pidfile=None)
+        self.assertEqual(d.procname, 'python')
+        self.assertFalse(self.pidfile.called)
+
     def test_fork_parent(self):
         self.os.fork.return_value = 1
         d = daemon.Daemon('pidfile')