def _run_command_and_check_ret_code(command, env, alert_disabled, retry_count=0, watch_type=None, zk_path=None, notification_timestamp=None, value=None, downloader_report_metadadata=False): try: # There are two code paths for metadata report # (1) downloader_report_metadadata is True. It is set to true # when _refresh_all_commands() get called. When set to true, # zk_download_data reports metadata to the flask endpoint # /admin/report_metadata. # (2) downloader_report_metadadata is False. When a node change is # observed by watcher, only serverset metadata # is calculated and updated in zk_update_monitor itself. if downloader_report_metadadata: command += " --report-metadata" command += EXTRA_FLAGS_FOR_ZK_DOWNLOAD_DATA subprocess.check_call(command, shell=True, env=env) log.debug("Command %s is executed successfully." % command) if (not downloader_report_metadadata) \ and watch_type == 'serverset' \ and zk_path and notification_timestamp and value: update_serverset_metadata(zk_path, notification_timestamp, value) if zk_path in _REJECTED_UPDATED_SERVERSETS: _REJECTED_UPDATED_SERVERSETS.remove(zk_path) except subprocess.CalledProcessError as e: error_code = e.returncode if error_code in _IGNORED_RET_CODES: log.debug("Got return code '%d' for command '%s' and ignored" % (error_code, command)) if (not downloader_report_metadadata) \ and watch_type == 'serverset' \ and zk_path and notification_timestamp and value: update_serverset_metadata(zk_path, notification_timestamp, value) elif error_code in _RETRY_RET_CODES and retry_count < _MAX_RETRY_COUNT: retry_count += 1 log.info("Retry command after %d seconds" % (_RETRY_WAIT_TIME_IN_SECONDS)) gevent.sleep(_RETRY_WAIT_TIME_IN_SECONDS) _run_command_and_check_ret_code(command, env, alert_disabled, retry_count, watch_type, zk_path, notification_timestamp, value) else: log.error("Got return code %d for command '%s'" % (error_code, command)) if error_code in _SERVERSET_REJECTION_ERROR_CODES and zk_path: if zk_path not in _REJECTED_UPDATED_SERVERSETS: _REJECTED_UPDATED_SERVERSETS.add(zk_path)
def _run_command_and_check_ret_code(command, env, alert_disabled, retry_count=0, watch_type=None, zk_path=None, notification_timestamp=None, value=None, downloader_report_metadadata=False): try: # There are two code paths for metadata report # (1) downloader_report_metadadata is True. It is set to true # when _refresh_all_commands() get called. When set to true, # zk_download_data reports metadata to the flask endpoint # /admin/report_metadata. # (2) downloader_report_metadadata is False. When a node change is # observed by watcher, only serverset metadata # is calculated and updated in zk_update_monitor itself. if downloader_report_metadadata: command += " --report-metadata" global _INITIALIZATION_COMPLETED if not _INITIALIZATION_COMPLETED: gevent.sleep(random.uniform(0, 2.0)) command += EXTRA_FLAGS_FOR_ZK_DOWNLOAD_DATA ret = subprocess.check_call(command, shell=True, env=env) log.debug("Command %s is executed successfully." % command) if (not downloader_report_metadadata) \ and watch_type == 'serverset' \ and zk_path and notification_timestamp and value: update_serverset_metadata(zk_path, notification_timestamp, value) if zk_path in _REJECTED_UPDATED_SERVERSETS: _REJECTED_UPDATED_SERVERSETS.remove(zk_path) _PATH_TO_DATA[zk_path].update({"run_command_ret": ret}) except subprocess.CalledProcessError as e: error_code = e.returncode _PATH_TO_DATA[zk_path].update({'run_command_ret': error_code}) if error_code in _IGNORED_RET_CODES: log.debug("Got return code '%d' for command '%s' and ignored" % (error_code, command)) if (not downloader_report_metadadata) \ and watch_type == 'serverset' \ and zk_path and notification_timestamp and value: update_serverset_metadata(zk_path, notification_timestamp, value) elif error_code in _RETRY_RET_CODES and retry_count < _MAX_RETRY_COUNT: retry_count += 1 log.info("Retry command after %d seconds" % (_RETRY_WAIT_TIME_IN_SECONDS)) gevent.sleep(_RETRY_WAIT_TIME_IN_SECONDS) _run_command_and_check_ret_code(command, env, alert_disabled, retry_count, watch_type, zk_path, notification_timestamp, value) else: if error_code in _SERVERSET_REJECTION_ERROR_CODES and zk_path: if zk_path not in _REJECTED_UPDATED_SERVERSETS: _REJECTED_UPDATED_SERVERSETS.add(zk_path)
def test_check_call(): subprocess.check_call('ls /'.split(' ')) with assert_raises(OSError): subprocess.check_call('/donotexist/poorexec')