def setUp(self): os.environ["TEST_VAR_B"] = "aaaa" class MovotoMock(object): def __getattr__(self, name): if name == "settings": return SettingsMock() else: return MovotoMock() class SettingsMock(object): def __getattr__(self, name): if name.startswith("_"): raise AttributeError if name == "not_exists": raise AttributeError return "yeo" sys.modules["Utilities"] = MovotoMock() sys.modules["Utilities.movoto"] = MovotoMock() self.origin_modules = {} for m, i in sys.modules.items(): if (m == "kipp" or m.startswith("kipp.") or m == "Utilities" or m.startswith("Utilities.")): self.origin_modules.update({m: i}) from kipp.runner.runner import runner, setup_settings setup_settings() self.runner = runner opt.set_option("timeout", None) opt.set_option("lock", False)
def setUp(self): os.environ['TEST_VAR_B'] = 'aaaa' class MovotoMock(object): def __getattr__(self, name): if name == 'settings': return SettingsMock() else: return MovotoMock() class SettingsMock(object): def __getattr__(self, name): if name.startswith('_'): raise AttributeError if name == 'not_exists': raise AttributeError return 'yeo' sys.modules['Utilities'] = MovotoMock() sys.modules['Utilities.movoto'] = MovotoMock() self.origin_modules = {} for m, i in sys.modules.items(): if m == 'kipp' or m.startswith('kipp.') \ or m =='Utilities' or m.startswith('Utilities.'): self.origin_modules.update({m: i}) from kipp.runner.runner import runner, setup_settings setup_settings() self.runner = runner opt.set_option('timeout', None) opt.set_option('lock', False)
def setup_arguments(): opt.add_argument("-t", "--timeout", type=int, default=0, help="seconds") opt.add_argument( "-ms", "--minimal_running_seconds", type=int, default=30, help="minimal running seconds", ) opt.add_argument( "-l", "--lock", action="store_true", default=False, help="only allow single running", ) opt.add_argument("--debug", action="store_true", default=False) opt.add_argument("command", nargs=argparse.REMAINDER) opt.parse_args() if not opt.command: raise AttributeError( "You should run like ``python -m runner <COMMAND>``") opt.set_option("command", " ".join(opt.command))
def runner(command): process = err_msg = None opt.set_option("runner_command_start_at", time.time()) try: opt.set_option("runner_monitor", RunStatsMonitor(command=command, args=sys.argv[1:-1])) clean_monitor_logs() catch_sys_quit_signal() opt.runner_monitor.start() get_logger().info("kipp.runner for %s", command) opt.set_option( "runner_command_start_at", time.time()) # override start_at before real process starting process = subprocess.Popen( [command], shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE, preexec_fn=os.setsid, ) if opt.timeout: r = wait_process_done(process, opt.timeout) else: r = process.communicate() if process.returncode != 0: err_msg = r[1] raise RuntimeError(err_msg) except BaseException as err: get_logger().exception(err) err_msg = traceback.format_exc() opt.runner_monitor.fail(err_msg) # Not send email in kipp.runner # if time.time() - opt.runner_command_start_at < opt.minimal_running_seconds: # send_alert_email(err_msg) raise else: get_logger().info("successed: %s", command) opt.runner_monitor.success() finally: kill_process(process)
def setup_settings(): opt.set_option("executor", ThreadPoolExecutor(2)) opt.set_option("sender", EmailSender(host=opt.SMTP_HOST))