def test_initgroups(self): cmd = sys.executable args = [SLEEP % 2] gid = os.getgid() uid = os.getuid() p1 = Process('test', '1', cmd, args=args, gid=gid, uid=uid) p1.stop()
def test_rlimits(self): script_file = self.get_tmpfile(RLIMIT) output_file = self.get_tmpfile() cmd = sys.executable args = [script_file, output_file] rlimits = {'nofile': 20, 'nproc': 20} process = Process('test', cmd, args=args, rlimits=rlimits) try: # wait for the process to finish while process.status == RUNNING: time.sleep(1) finally: process.stop() with open(output_file, 'r') as f: output = {} for line in f.readlines(): limit, value = line.rstrip().split('=', 1) output[limit] = value def srt2ints(val): return [circus.py3compat.long(key) for key in val[1:-1].split(',')] wanted = [circus.py3compat.long(20), circus.py3compat.long(20)] self.assertEqual(srt2ints(output['NOFILE']), wanted) self.assertEqual(srt2ints(output['NPROC']), wanted)
def test_rlimits(self): script_file = self.get_tmpfile(RLIMIT) output_file = self.get_tmpfile() cmd = sys.executable args = [script_file, output_file] rlimits = {'nofile': 20, 'nproc': 20} process = Process('test', cmd, args=args, rlimits=rlimits) poll_for(output_file, 'END') process.stop() with open(output_file, 'r') as f: output = {} for line in f.readlines(): line = line.rstrip() line = line.split('=', 1) if len(line) != 2: continue limit, value = line output[limit] = value def srt2ints(val): return [circus.py3compat.long(key) for key in val[1:-1].split(',')] wanted = [circus.py3compat.long(20), circus.py3compat.long(20)] self.assertEqual(srt2ints(output['NOFILE']), wanted) self.assertEqual(srt2ints(output['NPROC']), wanted)
def test_initgroups(self): cmd = sys.executable args = ['import time; time.sleep(2)'] gid = os.getgid() uid = os.getuid() p1 = Process('1', cmd, args=args, gid=gid, uid=uid) p1.stop()
def test_issue310(self): ''' https://github.com/mozilla-services/circus/pull/310 Allow $(circus.sockets.name) to be used in args. ''' conf = get_config(_CONF['issue310']) watcher = Watcher.load_from_config(conf['watchers'][0]) socket = CircusSocket.load_from_config(conf['sockets'][0]) try: watcher.initialize(None, {'web': socket}, None) process = Process(watcher._nextwid, watcher.cmd, args=watcher.args, working_dir=watcher.working_dir, shell=watcher.shell, uid=watcher.uid, gid=watcher.gid, env=watcher.env, rlimits=watcher.rlimits, spawn=False, executable=watcher.executable, use_fds=watcher.use_sockets, watcher=watcher) sockets_fds = watcher._get_sockets_fds() formatted_args = process.format_args(sockets_fds=sockets_fds) fd = sockets_fds['web'] self.assertEqual(formatted_args, ['foo', '--fd', str(fd)]) finally: socket.close()
def test_issue310(self): """ https://github.com/mozilla-services/circus/pull/310 Allow $(circus.sockets.name) to be used in args. """ conf = get_config(_CONF["issue310"]) watcher = Watcher.load_from_config(conf["watchers"][0]) socket = CircusSocket.load_from_config(conf["sockets"][0]) watcher.initialize(None, {"web": socket}, None) process = Process( watcher._process_counter, watcher.cmd, args=watcher.args, working_dir=watcher.working_dir, shell=watcher.shell, uid=watcher.uid, gid=watcher.gid, env=watcher.env, rlimits=watcher.rlimits, spawn=False, executable=watcher.executable, use_fds=watcher.use_sockets, watcher=watcher, ) fd = watcher._get_sockets_fds()["web"] formatted_args = process.format_args() self.assertEquals(formatted_args, ["foo", "--fd", str(fd)])
def test_initgroups(self): cmd = sys.executable args = [SLEEP % 2] gid = os.getgid() uid = os.getuid() p1 = Process('1', cmd, args=args, gid=gid, uid=uid) p1.stop()
def test_rlimits(self): script_file = self.get_tmpfile(RLIMIT) output_file = self.get_tmpfile() cmd = sys.executable args = [script_file, output_file] rlimits = {"nofile": 20, "nproc": 20} process = Process("test", cmd, args=args, rlimits=rlimits) poll_for(output_file, "END") process.stop() with open(output_file, "r") as f: output = {} for line in f.readlines(): line = line.rstrip() line = line.split("=", 1) if len(line) != 2: continue limit, value = line output[limit] = value def srt2ints(val): return [circus.py3compat.long(key) for key in val[1:-1].split(",")] wanted = [circus.py3compat.long(20), circus.py3compat.long(20)] self.assertEqual(srt2ints(output["NOFILE"]), wanted) self.assertEqual(srt2ints(output["NPROC"]), wanted)
def test_rlimits(self): script_file = self.get_tmpfile(RLIMIT) output_file = self.get_tmpfile() cmd = PYTHON args = [script_file, output_file] rlimits = {'nofile': 20, 'nproc': 20} process = Process('test', cmd, args=args, rlimits=rlimits) poll_for(output_file, 'END') process.stop() with open(output_file, 'r') as f: output = {} for line in f.readlines(): line = line.rstrip() line = line.split('=', 1) if len(line) != 2: continue limit, value = line output[limit] = value def srt2ints(val): return [circus.py3compat.long(key) for key in val[1:-1].split(',')] wanted = [circus.py3compat.long(20), circus.py3compat.long(20)] self.assertEqual(srt2ints(output['NOFILE']), wanted) self.assertEqual(srt2ints(output['NPROC']), wanted)
def test_process_parameters(self): # all the options passed to the process should be available by the # command / process p1 = Process('1', 'make-me-a-coffee', '$(circus.wid) --type $(circus.env.type)', shell=False, spawn=False, env={'type': 'macchiato'}) self.assertEquals(['make-me-a-coffee', '1', '--type', 'macchiato'], p1.format_args()) p2 = Process('1', 'yeah $(CIRCUS.WID)', spawn=False) self.assertEquals(['yeah', '1'], p2.format_args()) os.environ['coffee_type'] = 'american' p3 = Process('1', 'yeah $(circus.env.type)', shell=False, spawn=False, env={'type': 'macchiato'}) self.assertEquals(['yeah', 'macchiato'], p3.format_args()) os.environ.pop('coffee_type')
def load(watcher_conf): watcher = Watcher.load_from_config(watcher_conf.copy()) process = Process(watcher._nextwid, watcher.cmd, args=watcher.args, working_dir=watcher.working_dir, shell=watcher.shell, uid=watcher.uid, gid=watcher.gid, env=watcher.env, rlimits=watcher.rlimits, spawn=False, executable=watcher.executable, use_fds=watcher.use_sockets, watcher=watcher) return process.format_args()
def test_process_parameters(self): # all the options passed to the process should be available by the # command / process p1 = Process('1', 'make-me-a-coffee', '$(circus.wid) --type $(circus.env.type)', shell=False, spawn=False, env={'type': 'macchiato'}) self.assertEquals(['make-me-a-coffee', '1', '--type', 'macchiato'], p1.format_args()) p2 = Process('1', 'yeah $(CIRCUS.WID)', spawn=False) self.assertEquals(['yeah', '1'], p2.format_args())
def test_streams(self): script_file = self.get_tmpfile(VERBOSE) output_file = self.get_tmpfile() cmd = sys.executable args = [script_file, output_file] # 1. streams sent to /dev/null process = Process("test", cmd, args=args, close_child_stdout=True, close_child_stderr=True) try: poll_for(output_file, "END") # the pipes should be empty self.assertEqual(process.stdout.read(), b"") self.assertEqual(process.stderr.read(), b"") finally: process.stop() # 2. streams sent to /dev/null, no PIPEs output_file = self.get_tmpfile() args[1] = output_file process = Process( "test", cmd, args=args, close_child_stdout=True, close_child_stderr=True, pipe_stdout=False, pipe_stderr=False, ) try: poll_for(output_file, "END") # the pipes should be unexistant self.assertTrue(process.stdout is None) self.assertTrue(process.stderr is None) finally: process.stop() # 3. streams & pipes open output_file = self.get_tmpfile() args[1] = output_file process = Process("test", cmd, args=args) try: poll_for(output_file, "END") # the pipes should be unexistant self.assertEqual(len(process.stdout.read()), 2890) self.assertEqual(len(process.stderr.read()), 2890) finally: process.stop()
def test_streams(self): script_file = self.get_tmpfile(VERBOSE) cmd = sys.executable args = [script_file] # 1. streams sent to /dev/null process = Process('test', cmd, args=args, close_child_stdout=True, close_child_stderr=True) # wait for the process to finish while process.status == RUNNING: time.sleep(1) # the pipes should be empty self.assertEqual(process.stdout.read(), '') self.assertEqual(process.stderr.read(), '') # 2. streams sent to /dev/null, no PIPEs process = Process('test', cmd, args=args, close_child_stdout=True, close_child_stderr=True, pipe_stdout=False, pipe_stderr=False) # wait for the process to finish while process.status == RUNNING: time.sleep(1) # the pipes should be unexistant self.assertTrue(process.stdout is None) self.assertTrue(process.stderr is None) # 3. streams & pipes open process = Process('test', cmd, args=args) # wait for the process to finish while process.status == RUNNING: time.sleep(1) # the pipes should be unexistant self.assertEqual(len(process.stdout.read()), 2890) self.assertEqual(len(process.stderr.read()), 2890)
def load(watcher_conf): watcher = Watcher.load_from_config(watcher_conf.copy()) # Make sure we don't close the sockets as we will be # launching the Watcher with IS_WINDOWS=True watcher.use_sockets = True process = Process(watcher._nextwid, watcher.cmd, args=watcher.args, working_dir=watcher.working_dir, shell=watcher.shell, uid=watcher.uid, gid=watcher.gid, env=watcher.env, rlimits=watcher.rlimits, spawn=False, executable=watcher.executable, use_fds=watcher.use_sockets, watcher=watcher) return process.format_args()
def load(watcher_conf): watcher = Watcher.load_from_config(watcher_conf.copy()) # Make sure we don't close the sockets as we will be # launching the Watcher with IS_WINDOWS=True watcher.use_sockets = True process = Process('test', watcher._nextwid, watcher.cmd, args=watcher.args, working_dir=watcher.working_dir, shell=watcher.shell, uid=watcher.uid, gid=watcher.gid, env=watcher.env, rlimits=watcher.rlimits, spawn=False, executable=watcher.executable, use_fds=watcher.use_sockets, watcher=watcher) return process.format_args()
def spawn_process(self): """Spawn process. Return True if ok, False if the watcher must be stopped """ if self.is_stopped(): return True if not self.call_hook('before_spawn'): return False cmd = util.replace_gnu_args(self.cmd, sockets=self._get_sockets_fds(), env=self.env) nb_tries = 0 while nb_tries < self.max_retry or self.max_retry == -1: process = None pipe_stdout = self.stdout_redirector is not None pipe_stderr = self.stderr_redirector is not None try: process = Process(self._nextwid, cmd, args=self.args, working_dir=self.working_dir, shell=self.shell, uid=self.uid, gid=self.gid, env=self.env, rlimits=self.rlimits, executable=self.executable, use_fds=self.use_sockets, watcher=self, pipe_stdout=pipe_stdout, pipe_stderr=pipe_stderr, close_child_stdout=self.close_child_stdout, close_child_stderr=self.close_child_stderr) # stream stderr/stdout if configured if pipe_stdout and self.stdout_redirector is not None: self.stdout_redirector.add_redirection('stdout', process, process.stdout) if pipe_stderr and self.stderr_redirector is not None: self.stderr_redirector.add_redirection('stderr', process, process.stderr) self.processes[process.pid] = process logger.debug('running %s process [pid %d]', self.name, process.pid) except OSError as e: logger.warning('error in %r: %s', self.name, str(e)) if process is None: nb_tries += 1 continue else: self.notify_event("spawn", {"process_pid": process.pid, "time": time.time()}) return True return False
def spawn_process(self): """Spawn process. """ if self.stopped: return cmd = util.replace_gnu_args(self.cmd, sockets=self._get_sockets_fds()) self._process_counter += 1 nb_tries = 0 pipe_stdout = self.stdout_redirector is not None pipe_stderr = self.stderr_redirector is not None while nb_tries < self.max_retry or self.max_retry == -1: process = None try: process = Process(self._process_counter, cmd, args=self.args, working_dir=self.working_dir, shell=self.shell, uid=self.uid, gid=self.gid, env=self.env, rlimits=self.rlimits, executable=self.executable, use_fds=self.use_sockets, watcher=self, pipe_stdout=pipe_stdout, pipe_stderr=pipe_stderr, close_child_stdout=self.close_child_stdout, close_child_stderr=self.close_child_stderr) # stream stderr/stdout if configured if pipe_stdout: self.stdout_redirector.add_redirection( 'stdout', process, process.stdout) if pipe_stderr: self.stderr_redirector.add_redirection( 'stderr', process, process.stderr) self.processes[process.pid] = process logger.debug('running %s process [pid %d]', self.name, process.pid) except OSError, e: logger.warning('error in %r: %s', self.name, str(e)) if process is None: nb_tries += 1 continue else: self.notify_event("spawn", { "process_pid": process.pid, "time": time.time() }) time.sleep(self.warmup_delay) return
def test_comparison(self): cmd = sys.executable args = ['import time; time.sleep(2)', ] p1 = Process('1', cmd, args=args) p2 = Process('2', cmd, args=args) self.assertTrue(p1 < p2) self.assertFalse(p1 == p2) self.assertTrue(p1 == p1) p1.stop() p2.stop()
def test_streams(self): script_file = self.get_tmpfile(VERBOSE) cmd = sys.executable args = [script_file] # 1. streams sent to /dev/null process = Process('test', cmd, args=args, close_child_stdout=True, close_child_stderr=True) try: # wait for the process to finish while process.status == RUNNING: time.sleep(1) # the pipes should be empty self.assertEqual(process.stdout.read(), b'') self.assertEqual(process.stderr.read(), b'') finally: process.stop() # 2. streams sent to /dev/null, no PIPEs process = Process('test', cmd, args=args, close_child_stdout=True, close_child_stderr=True, pipe_stdout=False, pipe_stderr=False) try: # wait for the process to finish while process.status == RUNNING: time.sleep(1) # the pipes should be unexistant self.assertTrue(process.stdout is None) self.assertTrue(process.stderr is None) finally: process.stop() # 3. streams & pipes open process = Process('test', cmd, args=args) try: # wait for the process to finish while process.status == RUNNING: time.sleep(1) # the pipes should be unexistant self.assertEqual(len(process.stdout.read()), 2890) self.assertEqual(len(process.stderr.read()), 2890) finally: process.stop()
def spawn_process(self): """Spawn process. """ if self.stopped: return def _repl(matchobj): name = matchobj.group(1) if name in self.sockets: return str(self.sockets[name].fileno()) return '${socket:%s}' % name cmd = re.sub('\$\{socket\:(\w+)\}', _repl, self.cmd) self._process_counter += 1 nb_tries = 0 while nb_tries < self.max_retry: process = None try: process = Process(self._process_counter, cmd, args=self.args, working_dir=self.working_dir, shell=self.shell, uid=self.uid, gid=self.gid, env=self.env, rlimits=self.rlimits, executable=self.executable, use_fds=self.use_sockets) # stream stderr/stdout if configured if self.stdout_redirector is not None: self.stdout_redirector.add_redirection( 'stdout', process, process.stdout) if self.stderr_redirector is not None: self.stderr_redirector.add_redirection( 'stderr', process, process.stderr) self.processes[process.pid] = process logger.debug('running %s process [pid %d]', self.name, process.pid) except OSError, e: logger.warning('error in %r: %s', self.name, str(e)) if process is None: nb_tries += 1 continue else: self.notify_event("spawn", { "process_pid": process.pid, "time": time.time() }) time.sleep(self.warmup_delay) return
def test_issue310(self): """ https://github.com/mozilla-services/circus/pull/310 Allow $(circus.sockets.name) to be used in args. """ conf = get_config(_CONF["issue310"]) watcher = Watcher.load_from_config(conf["watchers"][0]) socket = CircusSocket.load_from_config(conf["sockets"][0]) try: watcher.initialize(None, {"web": socket}, None) if IS_WINDOWS: # We can't close the sockets on Windows as we # are redirecting stdout watcher.use_sockets = True process = Process( watcher._nextwid, watcher.cmd, args=watcher.args, working_dir=watcher.working_dir, shell=watcher.shell, uid=watcher.uid, gid=watcher.gid, env=watcher.env, rlimits=watcher.rlimits, spawn=False, executable=watcher.executable, use_fds=watcher.use_sockets, watcher=watcher, ) sockets_fds = watcher._get_sockets_fds() formatted_args = process.format_args(sockets_fds=sockets_fds) fd = sockets_fds["web"] self.assertEqual(formatted_args, ["foo", "--fd", str(fd)]) finally: socket.close()
def main(): if os.environ.get("SETTINGS_TYPE"): execution_type = os.environ.get("SETTINGS_TYPE") else: execution_type = SETTINGS_TYPE tenants = [] processes = [] def exit_program(*args): for p in processes: logger.info("Process finished %s" % p.pid) p.stop() sys.exit(0) signal.signal(signal.SIGINT, exit_program) while (True): try: conn = None if execution_type == 'production': conn = mysql.connect(charset=DB_CHARSET, use_unicode=True, host=DB_HOST, user=DB_USER, passwd=DB_PASSWD, db=DB_NAME) elif execution_type == "test": import sqlite3 as lite conn = lite.connect("{0}{1}.db".format(INSTALLATION_PATH, DB_NAME)) cursor = conn.cursor() cursor.execute( 'SELECT * FROM {0}.cloto_tenantinfo'.format(DB_NAME)) data = cursor.fetchall() if tenants.__len__() < data.__len__(): for tenant in data: if tenant not in tenants: tenants.append(tenant) id = tenant[0] process = Process( 'Top', 'python ' + ENVIRONMENTS_PATH + ' %s' % id) logger.info("Starting new environment for %s - pid: " % id) logger.info(ENVIRONMENTS_PATH) processes.append(process) except mysql.Error, e: logger.error("Error %s:" % e.args[0]) except Exception, e: logger.error("Error %s" % e.message)
def test_comparison(self): cmd = PYTHON args = ['import time; time.sleep(2)', ] p1 = Process('test', 1, cmd, args=args, use_fds=USE_FDS) # Make sure the two processes are launched with a measurable # difference. (precsion error on Windows) time.sleep(0.01) p2 = Process('test', 2, cmd, args=args, use_fds=USE_FDS) self.assertTrue(p1 < p2) self.assertFalse(p1 == p2) self.assertTrue(p1 == p1) p1.stop() p2.stop()
def test_base(self): cmd = sys.executable args = "-c 'import time; time.sleep(2)'" process = Process('test', cmd, args=args, shell=False) try: info = process.info() self.assertEqual(process.pid, info['pid']) age = process.age() self.assertTrue(age > 0.) self.assertFalse(process.is_child(0)) finally: process.stop()
def test_base(self): cmd = "%s -c 'import time; time.sleep(2)'" process = Process('test', cmd % sys.executable, shell=True) try: info = process.info() self.assertEqual(process.pid, info['pid']) age = process.age() self.assertTrue(age > 0.) self.assertFalse(process.is_child(0)) finally: process.stop()
def test_base(self): cmd = sys.executable args = "-c 'import time; time.sleep(2)'" process = Process("test", cmd, args=args, shell=False) try: info = process.info() self.assertEqual(process.pid, info["pid"]) age = process.age() self.assertTrue(age > 0.0) self.assertFalse(process.is_child(0)) finally: process.stop()
def test_base(self): cmd = PYTHON args = "-c 'import time; time.sleep(10)'" process = Process('test', 1, cmd, args=args, shell=False, use_fds=USE_FDS) try: info = process.info() self.assertEqual(process.pid, info['pid']) # Make sure the process lived a measurable amount of time # (precision error on Windows) time.sleep(0.01) age = process.age() self.assertTrue(age > 0.) self.assertFalse(process.is_child(0)) finally: process.stop()
def spawn_process(self): """Spawn process. """ if self.stopped: return self._process_counter += 1 nb_tries = 0 while nb_tries < self.max_retry: process = None try: process = Process(self._process_counter, self.cmd, args=self.args, working_dir=self.working_dir, shell=self.shell, uid=self.uid, gid=self.gid, env=self.env, rlimits=self.rlimits, executable=self.executable) # stream stderr/stdout if configured if self.stdout_redirector is not None: self.stdout_redirector.add_redirection('stdout', process, process.stdout) if self.stderr_redirector is not None: self.stderr_redirector.add_redirection('stderr', process, process.stderr) self.processes[self._process_counter] = process self.pids[process.pid] = process.wid logger.debug('running %s process [pid %d]', self.name, process.pid) except OSError, e: logger.warning('error in %r: %s', self.name, str(e)) if process is None: nb_tries += 1 continue else: self.send_msg("spawn", {"process_id": process.wid, "process_pid": process.pid, "time": time.time()}) time.sleep(self.warmup_delay) return
def test_comparison(self): cmd = PYTHON args = ['import time; time.sleep(2)', ] p1 = Process('1', cmd, args=args, use_fds=USE_FDS) # Make sure the two processes are launched with a measurable # difference. (precsion error on Windows) time.sleep(0.01) p2 = Process('2', cmd, args=args, use_fds=USE_FDS) self.assertTrue(p1 < p2) self.assertFalse(p1 == p2) self.assertTrue(p1 == p1) p1.stop() p2.stop()
def test_base(self): cmd = PYTHON args = "-c 'import time; time.sleep(10)'" process = Process('test', cmd, args=args, shell=False, use_fds=USE_FDS) try: info = process.info() self.assertEqual(process.pid, info['pid']) # Make sure the process lived a measurable amount of time # (precision error on Windows) time.sleep(0.01) age = process.age() self.assertTrue(age > 0.) self.assertFalse(process.is_child(0)) finally: process.stop()
def test_process_parameters(self): # all the options passed to the process should be available by the # command / process p1 = Process('1', 'make-me-a-coffee', '$(circus.wid) --type $(circus.env.type)', shell=False, spawn=False, env={'type': 'macchiato'}, use_fds=USE_FDS) self.assertEqual(['make-me-a-coffee', '1', '--type', 'macchiato'], p1.format_args()) p2 = Process('1', 'yeah $(CIRCUS.WID)', spawn=False, use_fds=USE_FDS) self.assertEqual(['yeah', '1'], p2.format_args()) os.environ['coffee_type'] = 'american' p3 = Process('1', 'yeah $(circus.env.type)', shell=False, spawn=False, env={'type': 'macchiato'}, use_fds=USE_FDS) self.assertEqual(['yeah', 'macchiato'], p3.format_args()) os.environ.pop('coffee_type')
def test_process_parameters(self): # all the options passed to the process should be available by the # command / process p1 = Process( "1", "make-me-a-coffee", "$(circus.wid) --type $(circus.env.type)", shell=False, spawn=False, env={"type": "macchiato"}, ) self.assertEqual(["make-me-a-coffee", "1", "--type", "macchiato"], p1.format_args()) p2 = Process("1", "yeah $(CIRCUS.WID)", spawn=False) self.assertEqual(["yeah", "1"], p2.format_args()) os.environ["coffee_type"] = "american" p3 = Process("1", "yeah $(circus.env.type)", shell=False, spawn=False, env={"type": "macchiato"}) self.assertEqual(["yeah", "macchiato"], p3.format_args()) os.environ.pop("coffee_type")
def test_streams(self): script_file = self.get_tmpfile(VERBOSE) output_file = self.get_tmpfile() cmd = sys.executable args = [script_file, output_file] # 1. streams sent to /dev/null process = Process('test', cmd, args=args, close_child_stdout=True, close_child_stderr=True) try: poll_for(output_file, 'END') # the pipes should be empty self.assertEqual(process.stdout.read(), b'') self.assertEqual(process.stderr.read(), b'') finally: process.stop() # 2. streams sent to /dev/null, no PIPEs output_file = self.get_tmpfile() args[1] = output_file process = Process('test', cmd, args=args, close_child_stdout=True, close_child_stderr=True, pipe_stdout=False, pipe_stderr=False) try: poll_for(output_file, 'END') # the pipes should be unexistant self.assertTrue(process.stdout is None) self.assertTrue(process.stderr is None) finally: process.stop() # 3. streams & pipes open output_file = self.get_tmpfile() args[1] = output_file process = Process('test', cmd, args=args) try: poll_for(output_file, 'END') # the pipes should be unexistant self.assertEqual(len(process.stdout.read()), 2890) self.assertEqual(len(process.stderr.read()), 2890) finally: process.stop()