class WsgidAppTest(unittest.TestCase): @patch('sys.stderr') def setUp(self, *args): self.init = CommandInit() self.empty_apppath = os.path.join(FIXTURE, 'empty-app') self.init.run(FakeOptions(app_path=self.empty_apppath)) self.empty_wsgidapp = WsgidApp(self.empty_apppath) def test_check_valid_wsgid_app_folder(self): self.assertTrue(self.empty_wsgidapp.is_valid(), "Did not recognize as a valid wsgid app folder") def test_return_empty_master_pids(self): self.assertEquals([], self.empty_wsgidapp.master_pids()) def test_return_empty_worker_pids(self): self.assertEquals([], self.empty_wsgidapp.worker_pids()) @patch('sys.stderr') def test_return_pids(self, *args): app = os.path.join(FIXTURE, 'app-with-pids') self.init.run(FakeOptions(app_path=app)) open(os.path.join(app, 'pid/master/3345.pid'), 'w') open(os.path.join(app, 'pid/master/2938.pid'), 'w') open(os.path.join(app, 'pid/master/no-pid.pid'), 'w') open(os.path.join(app, 'pid/worker/8756.pid'), 'w') open(os.path.join(app, 'pid/worker/3948.pid'), 'w') open(os.path.join(app, 'pid/worker/invalid.pid'), 'w') wsgidapp = WsgidApp(app) # Invalid pidfiles must be ignored self.assertEquals([2938, 3345], wsgidapp.master_pids()) self.assertEquals([3948, 8756], wsgidapp.worker_pids())
def test_return_pids(self, *args): app = os.path.join(FIXTURE, 'app-with-pids') self.init.run(FakeOptions(app_path=app)) open(os.path.join(app, 'pid/master/3345.pid'), 'w') open(os.path.join(app, 'pid/master/2938.pid'), 'w') open(os.path.join(app, 'pid/master/no-pid.pid'), 'w') open(os.path.join(app, 'pid/worker/8756.pid'), 'w') open(os.path.join(app, 'pid/worker/3948.pid'), 'w') open(os.path.join(app, 'pid/worker/invalid.pid'), 'w') wsgidapp = WsgidApp(app) # Invalid pidfiles must be ignored self.assertEquals([2938, 3345], wsgidapp.master_pids()) self.assertEquals([3948, 8756], wsgidapp.worker_pids())
def test_clean_pid_files_on_keyboard_interrupt(self): path = os.path.join(FIXTURES_PATH, 'clean-pids-app') initcmd = CommandInit() opts = FakeOptions(app_path=path) initcmd.run(opts) open(os.path.join(path, 'pid/master/3340.pid'), 'w') open(os.path.join(path, 'pid/worker/2736.pid'), 'w') open(os.path.join(path, 'pid/worker/3847.pid'), 'w') with patch('os.wait'): with patch('os.getpid'): os.getpid.side_effect = lambda: 3340 os.wait.side_effect = KeyboardInterrupt() cli = Cli() cli.options = opts cli.log = Mock() cli.workers = [2736, 3847] cli._wait_workers() app = WsgidApp(path) self.assertEquals([], app.master_pids()) self.assertEquals([], app.worker_pids())
def test_clean_pid_files_on_keyboard_interrupt(self, *args): path = os.path.join(FIXTURES_PATH, 'clean-pids-app') initcmd = CommandInit() opts = FakeOptions(app_path=path) initcmd.run(opts) open(os.path.join(path, 'pid/master/3340.pid'), 'w') open(os.path.join(path, 'pid/worker/2736.pid'), 'w') open(os.path.join(path, 'pid/worker/3847.pid'), 'w') with patch('os.wait'): with patch('os.getpid'): os.getpid.side_effect = lambda: 3340 os.wait.side_effect = KeyboardInterrupt() cli = Cli() cli.options = opts cli.log = Mock() cli.workers = [2736, 3847] cli._wait_workers() app = WsgidApp(path) self.assertEquals([], app.master_pids()) self.assertEquals([], app.worker_pids())
def run(self, options, **kwargs): app = WsgidApp(options.app_path) status = {True: 'Running', False: 'Stopped'} sys.stdout.write("Status: {status}\n".format(status=status[self._any_running(app.worker_pids())])) sys.stdout.write("Master pid(s): {pids}\n".format(pids=", ".join([str(p) for p in app.master_pids()]))) sys.stdout.write("Worker pid(s): {pids}\n".format(pids=", ".join([self._prepare_pid(p) for p in app.worker_pids()])))
def _restart(self, options): wsgidapp = WsgidApp(options.app_path) logger().info("Restarting worker processes at {0}...".format( options.app_path)) pids = wsgidapp.worker_pids() self._kill_pids(pids, options.send_signal, 'worker')
def _stop(self, options): wsgidapp = WsgidApp(options.app_path) logger().info("Stopping master processes at {0}...".format( options.app_path)) pids = wsgidapp.master_pids() self._kill_pids(pids, options.send_signal, 'master')
def _restart(self, options): wsgidapp = WsgidApp(options.app_path) logger.info("Restarting worker processes at {0}...".format(options.app_path)) pids = wsgidapp.worker_pids() self._kill_pids(pids, options.send_signal, 'worker')
def _stop(self, options): wsgidapp = WsgidApp(options.app_path) logger.info("Stopping master processes at {0}...".format(options.app_path)) pids = wsgidapp.master_pids() self._kill_pids(pids, options.send_signal, 'master')
def setUp(self, *args): self.init = CommandInit() self.empty_apppath = os.path.join(FIXTURE, 'empty-app') self.init.run(FakeOptions(app_path=self.empty_apppath)) self.empty_wsgidapp = WsgidApp(self.empty_apppath)