def test_daemonize(self): if not HAS_DAEMONIZE: raise Skip("need `daemonize` for this test case") def daemon_helper(): time.sleep(100) self.fail() with NamedTemporaryFile() as tf: try: daemon.daemonize(daemon_helper, name='sparts_unittest', pidfile=tf.name, logger=self.logger) except SystemExit: # Catch the daemonize library's attempt to sys.exit() pass def checkdaemon(): try: return daemon.status(tf.name, self.logger) except ValueError: return False # Eliminate the race condition waiting for daemonize.Daemonize() # to create *and* write the pid to the the pidfile. timer.run_until_true(checkdaemon, timeout=1.0) self.assertTrue(daemon.status(tf.name, self.logger)) self.assertTrue(daemon.kill(tf.name, self.logger)) self.assertFalse(daemon.status(tf.name, self.logger))
def initFromOptions(cls, ns, name=None): """Starts this service, arguments from `ns`""" instance = cls(ns) if name is not None: instance.name = name instance.preprocessOptions() if HAS_DAEMONIZE and ns.daemon: daemon.daemonize( command=functools.partial(cls._runloop, instance), name=instance.name, pidfile=ns.pidfile, logger=instance.logger, ) else: return cls._runloop(instance)
def test_daemonize(self): if not HAS_DAEMONIZE: raise Skip("need `daemonize` for this test case") def daemon_helper(): time.sleep(100) self.fail() with NamedTemporaryFile() as tf: # Fork so daemonizing the current process does not mess up with the # test suite. child_pid = os.fork() if child_pid == 0: try: daemon.daemonize(daemon_helper, name='sparts_unittest', pidfile=tf.name, logger=self.logger) except SystemExit: # Catch the daemonize library's attempt to sys.exit() pass else: def checkdaemon(): try: return daemon.status(tf.name, self.logger) except ValueError: return False # Eliminate the race condition waiting for # daemonize.Daemonize() to create *and* write the pid to the # pidfile. timer.run_until_true(checkdaemon, timeout=1.0) self.assertTrue(daemon.status(tf.name, self.logger)) self.assertTrue(daemon.kill(tf.name, self.logger)) self.assertFalse(daemon.status(tf.name, self.logger))