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 test_status(self): # Make sure we can poll a fake pidfile for the unittest process with NamedTemporaryFile() as tf: writefile(tf.name, str(os.getpid())) self.assertTrue(daemon.status(tf.name, self.logger)) # This should fail since the pidfile is deleted self.assertFalse(daemon.status(tf.name, self.logger)) # We should get an EPERM trying to poll pid with NamedTemporaryFile() as tf: writefile(tf.name, '1') self.assertFalse(daemon.status(tf.name, self.logger))
def preprocessOptions(self): """Processes "action" oriented options.""" if self.getOption('runit_install'): self._install() if HAS_DAEMONIZE: # Act on the --status if present. if self.status: if daemon.status(pidfile=self.pidfile, logger=self.logger): sys.exit(0) else: sys.exit(1) # Act on the --kill flag if present. if self.kill: if daemon.kill(pidfile=self.pidfile, logger=self.logger): sys.exit(0) else: sys.exit(1) if self.options.tasks == []: print("Available Tasks:") for t in self.tasks: print(" - %s" % t.__name__) sys.exit(1)
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))
def checkdaemon(): try: return daemon.status(tf.name, self.logger) except ValueError: return False