Example #1
0
    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)
Example #2
0
    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))
Example #3
0
    def test_kill(self):
        child_pid = os.fork()
        if child_pid == 0:
            # Child - Sleep for 100 seconds.  We'll be killed anyway, but in
            # the event that this test fails, we don't want to leave the
            # orphaned process around forever.
            time.sleep(100)
            self.fail()

        with NamedTemporaryFile() as tf:
            writefile(tf.name, str(child_pid))
            self.assertTrue(daemon.kill(tf.name, self.logger))

        # This should return true because there is no file and therefore
        # the service is already dead
        self.assertTrue(daemon.kill(tf.name, self.logger))
Example #4
0
    def test_kill(self):
        child_pid = os.fork()
        if child_pid == 0:
            # Child - Sleep for 100 seconds.  We'll be killed anyway, but in
            # the event that this test fails, we don't want to leave the
            # orphaned process around forever.
            time.sleep(100)
            self.fail()

        with NamedTemporaryFile() as tf:
            writefile(tf.name, str(child_pid))
            self.assertTrue(daemon.kill(tf.name, self.logger))

        # This should return true because there is no file and therefore
        # the service is already dead
        self.assertTrue(daemon.kill(tf.name, self.logger))
Example #5
0
    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)
Example #6
0
    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))