def test_stop(self): testing.disable_logging() flexmock(subprocess)\ .should_receive('call')\ .and_return(0) self.assertEqual(True, god_interface.stop("watch_name")) flexmock(subprocess)\ .should_receive('call')\ .and_return(1) self.assertEqual(False, god_interface.stop("watch_name"))
def stop_worker(self, json_request): """ Stops the god watch for queues of an application on the current node. Args: json_request: A JSON string with the queue name for which we're stopping its queues. Returns: A JSON string with the result. """ request = self.__parse_json_and_validate_tags(json_request, self.STOP_WORKERS_TAGS) if 'error' in request: return json.dumps(request) app_id = request['app_id'] watch = "celery-" + str(app_id) try: if god_interface.stop(watch): stop_command = self.get_worker_stop_command(app_id) os.system(stop_command) TaskQueueConfig.remove_config_files(app_id) result = {'error': False} else: result = {'error': True, 'reason': "Unable to stop watch %s" % watch} except OSError, os_error: result = {'error': True, 'reason' : str(os_error)}
def stop_app_instance(app_name, port): """ Stops a Google App Engine application process instance on current machine. Args: app_name: Name of application to stop port: The port the application is running on Returns: True on success, False otherwise """ if not misc.is_app_name_valid(app_name): logging.error("Unable to kill app process %s on port %d because of " +\ "invalid name for application"%(app_name, int(port))) return False logging.info("Stopping application %s"%app_name) watch = "app___" + app_name + "-" + str(port) god_result = god_interface.stop(watch) # hack: God fails to shutdown processes so we do it via a system command # TODO: fix it or find an alternative to god pid_file = constants.APP_PID_DIR + app_name + '-' + port pid = file_io.read(pid_file) if str(port).isdigit(): if subprocess.call(['kill', '-9', pid]) != 0: logging.error("Unable to kill app process %s on port %d with pid %s"%\ (app_name, int(port), str(pid))) file_io.delete(pid_file) return god_result
def stop_worker(self, json_request): """ Stops the god watch for queues of an application on the current node. Args: json_request: A JSON string with the queue name for which we're stopping its queues. Returns: A JSON string with the result. """ request = self.__parse_json_and_validate_tags(json_request, self.STOP_WORKERS_TAGS) if 'error' in request: return json.dumps(request) app_id = request['app_id'] watch = "celery-" + str(app_id) try: if god_interface.stop(watch): stop_command = self.get_worker_stop_command(app_id) os.system(stop_command) TaskQueueConfig.remove_config_files(app_id) result = {'error': False} else: result = { 'error': True, 'reason': "Unable to stop watch %s" % watch } except OSError, os_error: result = {'error': True, 'reason': str(os_error)}
def stop_app(app_name): """ Stops all process instances of a Google App Engine application on this machine. Args: app_name: Name of application to stop Returns: True on success, False otherwise """ if not misc.is_app_name_valid(app_name): logging.error("Unable to kill app process %s on because of " +\ "invalid name for application"%(app_name)) return False logging.info("Stopping application %s"%app_name) watch = "app___" + app_name god_result = god_interface.stop(watch) if not god_result: logging.error("Unable to shut down god interface for watch %s"%watch) return False # hack: God fails to shutdown processes so we do it via a system command # TODO: fix it or find an alternative to god cmd = "ps -ef | grep \"dev_appserver\|AppServer_Java\" | grep " + \ app_name + " | grep -v grep | grep cookie_secret | awk '{print $2}' " +\ "| xargs kill -9" ret = os.system(cmd) if ret != 0: logging.error("Unable to shut down processes for app %s with exit value %d"\ %(app_name, ret)) return False cmd = "rm -f " + constants.APP_PID_DIR + app_name + "-*" ret = os.system(cmd) if ret != 0: logging.error("Unable to remove PID files for app %s with exit value %d"\ %(app_name, ret)) return False return True
def start_app(config): """ Starts a Google App Engine application on this machine. It will start it up and then proceed to fetch the main page. Args: config: a dictionary that contains app_name: Name of the application to start app_port: Port to start on language: What language the app is written in load_balancer_ip: Public ip of load balancer load_balancer_port: Port of load balancer xmpp_ip: IP of XMPP service dblocations: List of database locations env_vars: A dict of environment variables that should be passed to the app. Returns: PID of process on success, -1 otherwise """ config = convert_config_from_json(config) if config == None: logging.error("Invalid configuration for application") return BAD_PID if not misc.is_app_name_valid(config['app_name']): logging.error("Invalid app name for application: " +\ config['app_name']) return BAD_PID logging.info("Starting %s application %s"%(config['language'], config['app_name'])) start_cmd = "" stop_cmd = "" env_vars = config['env_vars'] watch = "app___" + config['app_name'] if config['language'] == constants.PYTHON or \ config['language'] == constants.PYTHON27 or \ config['language'] == constants.GO: start_cmd = create_python_start_cmd(config['app_name'], config['load_balancer_ip'], config['app_port'], config['load_balancer_ip'], config['load_balancer_port'], config['xmpp_ip'], config['dblocations'], config['language']) logging.info(start_cmd) stop_cmd = create_python_stop_cmd(config['app_port'], config['language']) env_vars.update(create_python_app_env(config['load_balancer_ip'], config['load_balancer_port'], config['app_name'])) elif config['language'] == constants.JAVA: copy_successful = copy_modified_jars(config['app_name']) if not copy_successful: return BAD_PID start_cmd = create_java_start_cmd(config['app_name'], config['app_port'], config['load_balancer_ip'], config['load_balancer_port'], config['dblocations']) stop_cmd = create_java_stop_cmd(config['app_port']) env_vars.update(create_java_app_env()) else: logging.error("Unknown application language %s for appname %s"\ %(config['language'], config['app_name'])) return BAD_PID logging.info("Start command: " + str(start_cmd)) logging.info("Stop command: " + str(stop_cmd)) logging.info("Environment variables: " +str(env_vars)) config_file_loc = god_app_configuration.create_config_file(str(watch), str(start_cmd), str(stop_cmd), [config['app_port']], env_vars) if not god_interface.start(config_file_loc, watch): logging.error("Unable to start application server with god") return BAD_PID if not wait_on_app(int(config['app_port'])): logging.error("Application server did not come up in time, " + \ "removing god watch") god_interface.stop(watch) return BAD_PID pid = get_pid_from_port(config['app_port']) pid_file = constants.APP_PID_DIR + config['app_name'] + '-' +\ str(config['app_port']) file_io.write(pid_file, str(pid)) return pid
def start_app(config): """ Starts a Google App Engine application on this machine. It will start it up and then proceed to fetch the main page. Args: config: a dictionary that contains app_name: Name of the application to start app_port: Port to start on language: What language the app is written in load_balancer_ip: Public ip of load balancer load_balancer_port: Port of load balancer xmpp_ip: IP of XMPP service dblocations: List of database locations Returns: PID of process on success, -1 otherwise """ config = convert_config_from_json(config) if config == None: logging.error("Invalid configuration for application") return BAD_PID logging.info("Starting %s application %s"%(config['language'], config['app_name'])) start_cmd = "" stop_cmd = "" env_vars = {} watch = "app___" + config['app_name'] if config['language'] == constants.PYTHON or \ config['language'] == constants.GO: start_cmd = create_python_start_cmd(config['app_name'], config['load_balancer_ip'], config['app_port'], config['load_balancer_ip'], config['load_balancer_port'], config['xmpp_ip'], config['dblocations']) stop_cmd = create_python_stop_cmd(config['app_port']) env_vars = create_python_app_env(config['load_balancer_ip'], config['load_balancer_port'], config['app_name']) elif config['language'] == constants.JAVA: start_cmd = create_java_start_cmd(config['app_name'], config['app_port'], config['load_balancer_ip'], config['load_balancer_port'], config['dblocations']) stop_cmd = create_java_stop_cmd(config['app_port']) env_vars = create_java_app_env() else: logging.error("Unknown application language %s for appname %s"\ %(config['language'], config['app_name'])) return BAD_PID logging.info("Start command: " + str(start_cmd)) logging.info("Stop command: " + str(stop_cmd)) logging.info("Environment variables: " +str(env_vars)) config_file_loc = god_app_interface.create_config_file(str(watch), str(start_cmd), str(stop_cmd), [config['app_port']], env_vars) if not god_interface.start(config_file_loc, watch): logging.error("Unable to start application server with god") return BAD_PID if not wait_on_app(int(config['app_port'])): logging.error("Application server did not come up in time, " + \ "removing god watch") god_interface.stop(watch) return BAD_PID pid = get_pid_from_port(config['app_port']) pid_file = constants.APP_PID_DIR + config['app_name'] + '-' +\ str(config['app_port']) file_io.write(pid_file, str(pid)) return pid