本文整理匯總了Python中app.util.network.Network.post方法的典型用法代碼示例。如果您正苦於以下問題:Python Network.post方法的具體用法?Python Network.post怎麽用?Python Network.post使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類app.util.network.Network
的用法示例。
在下文中一共展示了Network.post方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ClusterSlave
# 需要導入模塊: from app.util.network import Network [as 別名]
# 或者: from app.util.network.Network import post [as 別名]
#.........這裏部分代碼省略.........
"""
Called from setup_build(). Do asynchronous setup for the build so that we can make the call to setup_build()
non-blocking.
"""
# todo(joey): It's strange that the project_type is setting up the executors, which in turn set up projects.
# todo(joey): I think this can be untangled a bit -- we should call executor.configure_project_type() here.
self._project_type.setup_build(executors, project_type_params)
self._logger.info('Build setup complete for build {}.', self._current_build_id)
self._setup_complete_event.set() # free any subjob threads that are waiting for setup to complete
def teardown_build(self, build_id=None):
"""
Called at the end of each build on each slave before it reports back to the master that it is idle again.
:param build_id: The build id to teardown -- this parameter is used solely for correctness checking of the
master, to make sure that the master is not erroneously sending teardown commands for other builds.
:type build_id: int | None
"""
if self._current_build_id is None:
raise BadRequestError('Tried to teardown a build but no build is active on this slave.')
if build_id is not None and build_id != self._current_build_id:
raise BadRequestError('Tried to teardown build {}, '
'but slave is running build {}!'.format(build_id, self._current_build_id))
self._logger.info('Executing teardown for build {}.', self._current_build_id)
SafeThread(target=self._async_teardown_build).start()
def _async_teardown_build(self, should_disconnect_from_master=False):
"""
Called from teardown_build(). Do asynchronous teardown for the build so that we can make the call to
teardown_build() non-blocking. Also take care of posting back to the master when teardown is complete.
"""
if self._project_type:
self._project_type.teardown_build()
self._logger.info('Build teardown complete for build {}.', self._current_build_id)
self._current_build_id = None
self._project_type = None
if not should_disconnect_from_master:
# report back to master that this slave is finished with teardown and ready for a new build
self._logger.info('Notifying master that this slave is ready for new builds.')
idle_url = self._master_api.url('slave', self._slave_id, 'idle')
response = self._network.post(idle_url)
if response.status_code != http.client.OK:
raise RuntimeError("Could not post teardown completion to master at {}".format(idle_url))
elif self._is_master_responsive():
# report back to master that this slave is shutting down and should not receive new builds
self._logger.info('Notifying master to disconnect this slave.')
disconnect_url = self._master_api.url('slave', self._slave_id, 'disconnect')
response = self._network.post(disconnect_url)
if response.status_code != http.client.OK:
self._logger.error('Could not post disconnect notification to master at {}'.format(disconnect_url))
def connect_to_master(self, master_url=None):
"""
Notify the master that this slave exists.
:param master_url: The URL of the master service. If none specified, defaults to localhost:43000.
:type master_url: str
"""
self._master_url = master_url or 'localhost:43000'
self._master_api = UrlBuilder(self._master_url)
示例2: ClusterSlave
# 需要導入模塊: from app.util.network import Network [as 別名]
# 或者: from app.util.network.Network import post [as 別名]
#.........這裏部分代碼省略.........
self._project_type.run_job_config_setup()
except SetupFailureError as ex:
self._logger.error(ex)
self._logger.info('Notifying master that build setup has failed for build {}.', self._current_build_id)
self._notify_master_of_state_change(SlaveState.SETUP_FAILED)
else:
self._logger.info('Notifying master that build setup is complete for build {}.', self._current_build_id)
self._notify_master_of_state_change(SlaveState.SETUP_COMPLETED)
def teardown_build(self, build_id=None):
"""
Called at the end of each build on each slave before it reports back to the master that it is idle again.
:param build_id: The build id to teardown -- this parameter is used solely for correctness checking of the
master, to make sure that the master is not erroneously sending teardown commands for other builds.
:type build_id: int | None
"""
if self._current_build_id is None:
raise BadRequestError('Tried to teardown a build but no build is active on this slave.')
if build_id is not None and build_id != self._current_build_id:
raise BadRequestError('Tried to teardown build {}, '
'but slave is running build {}!'.format(build_id, self._current_build_id))
SafeThread(
target=self._async_teardown_build,
name='Bld{}-Teardwn'.format(build_id)
).start()
def _async_teardown_build(self):
"""
Called from teardown_build(). Do asynchronous teardown for the build so that we can make the call to
teardown_build() non-blocking. Also take care of posting back to the master when teardown is complete.
"""
self._do_build_teardown_and_reset()
while not self._idle_executors.full():
time.sleep(1)
self._send_master_idle_notification()
def _do_build_teardown_and_reset(self, timeout=None):
"""
Kill any currently running subjobs. Run the teardown_build commands for the current build (with an optional
timeout). Clear attributes related to the currently running build.
:param timeout: A maximum time in seconds to allow the teardown process to run before killing
:type timeout: int | None
"""
# Kill all subjob executors' processes. This only has an effect if we are tearing down before a build completes.
for executor in self.executors_by_id.values():
executor.kill()
# Order matters! Spend the coin if it has been initialized.
if not self._build_teardown_coin or not self._build_teardown_coin.spend() or not self._project_type:
return # There is no build to tear down or teardown is already in progress.
self._logger.info('Executing teardown for build {}.', self._current_build_id)
# todo: Catch exceptions raised during teardown_build so we don't skip notifying master of idle/disconnect.
self._project_type.teardown_build(timeout=timeout)
self._logger.info('Build teardown complete for build {}.', self._current_build_id)
self._current_build_id = None
self._project_type = None
def _send_master_idle_notification(self):
if not self._is_master_responsive():
self._logger.notice('Could not post idle notification to master because master is unresponsive.')
示例3: Counter
# 需要導入模塊: from app.util.network import Network [as 別名]
# 或者: from app.util.network.Network import post [as 別名]
class Slave:
API_VERSION = 'v1'
_slave_id_counter = Counter()
def __init__(self, slave_url, num_executors, slave_session_id=None):
"""
:type slave_url: str
:type num_executors: int
:type slave_session_id: str
"""
self.url = slave_url
self.num_executors = num_executors
self.id = self._slave_id_counter.increment()
self._num_executors_in_use = Counter()
self._network = Network(min_connection_poolsize=num_executors)
self.current_build_id = None
self._last_heartbeat_time = datetime.now()
self._is_alive = True
self._is_in_shutdown_mode = False
self._slave_api = UrlBuilder(slave_url, self.API_VERSION)
self._session_id = slave_session_id
self._logger = log.get_logger(__name__)
def __str__(self):
return '<slave #{} - {}>'.format(self.id, self.url)
def api_representation(self):
return {
'url': self.url,
'id': self.id,
'session_id': self._session_id,
'num_executors': self.num_executors,
'num_executors_in_use': self.num_executors_in_use(),
'current_build_id': self.current_build_id,
'is_alive': self.is_alive(),
'is_in_shutdown_mode': self._is_in_shutdown_mode,
}
def mark_as_idle(self):
"""
Do bookkeeping when this slave becomes idle. Error if the slave cannot be idle.
If the slave is in shutdown mode, clear the build_id, kill the slave, and raise an error.
"""
if self._num_executors_in_use.value() != 0:
raise Exception('Trying to mark slave idle while {} executors still in use.',
self._num_executors_in_use.value())
self.current_build_id = None
if self._is_in_shutdown_mode:
self.kill()
self._remove_slave_from_registry()
raise SlaveMarkedForShutdownError
def setup(self, build: Build, executor_start_index: int) -> bool:
"""
Execute a setup command on the slave for the specified build. The setup process executes asynchronously on the
slave and the slave will alert the master when setup is complete and it is ready to start working on subjobs.
:param build: The build to set up this slave to work on
:param executor_start_index: The index the slave should number its executors from for this build
:return: Whether or not the call to start setup on the slave was successful
"""
slave_project_type_params = build.build_request.build_parameters().copy()
slave_project_type_params.update(build.project_type.slave_param_overrides())
setup_url = self._slave_api.url('build', build.build_id(), 'setup')
post_data = {
'project_type_params': slave_project_type_params,
'build_executor_start_index': executor_start_index,
}
self.current_build_id = build.build_id()
try:
self._network.post_with_digest(setup_url, post_data, Secret.get())
except (requests.ConnectionError, requests.Timeout) as ex:
self._logger.warning('Setup call to {} failed with {}: {}.', self, ex.__class__.__name__, str(ex))
self.mark_dead()
return False
return True
def teardown(self):
"""
Tell the slave to run the build teardown
"""
if not self.is_alive():
self._logger.notice('Teardown request to slave {} was not sent since slave is disconnected.', self.url)
return
teardown_url = self._slave_api.url('build', self.current_build_id, 'teardown')
try:
self._network.post(teardown_url)
except (requests.ConnectionError, requests.Timeout):
self._logger.warning('Teardown request to slave failed because slave is unresponsive.')
self.mark_dead()
def start_subjob(self, subjob: Subjob):
"""
Send a subjob of a build to this slave. The slave must have already run setup for the corresponding build.
:param subjob: The subjob to send to this slave
#.........這裏部分代碼省略.........
示例4: Slave
# 需要導入模塊: from app.util.network import Network [as 別名]
# 或者: from app.util.network.Network import post [as 別名]
class Slave(object):
_slave_id_counter = Counter()
def __init__(self, slave_url, num_executors):
"""
:type slave_url: str
:type num_executors: int
"""
self.url = slave_url
self.num_executors = num_executors
self.id = self._slave_id_counter.increment()
self._num_executors_in_use = Counter()
self._network = Network(min_connection_poolsize=num_executors)
self.current_build_id = None
self.is_alive = True
self._slave_api = UrlBuilder(slave_url, app.master.cluster_master.ClusterMaster.API_VERSION)
def api_representation(self):
return {
'url': self.url,
'id': self.id,
'num_executors': self.num_executors,
'num_executors_in_use': self.num_executors_in_use(),
'current_build_id': self.current_build(),
}
def mark_as_idle(self):
"""
Do bookkeeping when this slave becomes idle. Error if the slave cannot be idle.
"""
if self._num_executors_in_use.value() != 0:
raise Exception('Trying to mark slave idle while {} executors still in use.',
self._num_executors_in_use.value())
self.current_build_id = None
def setup(self, build_id, project_type_params):
"""
Execute a setup command on the slave for the specified build. The command is executed asynchronously from the
perspective of this method, but any subjobs will block until the slave finishes executing the setup command.
:param build_id: The build id that this setup command is for.
:type build_id: int
:param project_type_params: The parameters that define the project type this build will execute in
:typeproject_type_paramss: dict
"""
setup_url = self._slave_api.url('build', build_id, 'setup')
post_data = {
'project_type_params': project_type_params,
}
self._network.post_with_digest(setup_url, post_data, Secret.get())
def teardown(self):
"""
Tell the slave to run the build teardown
"""
teardown_url = self._slave_api.url('build', self.current_build_id, 'teardown')
self._network.post(teardown_url)
def start_subjob(self, subjob):
"""
:type subjob: Subjob
"""
if not self.is_alive:
raise RuntimeError('Tried to start a subjob on a dead slave! ({}, id: {})'.format(self.url, self.id))
SafeThread(target=self._async_start_subjob, args=(subjob,)).start()
self.current_build_id = subjob.build_id()
def _async_start_subjob(self, subjob):
"""
:type subjob: Subjob
"""
execution_url = self._slave_api.url('build', subjob.build_id(), 'subjob', subjob.subjob_id())
post_data = {
'subjob_artifact_dir': subjob.artifact_dir(),
'atomic_commands': subjob.atomic_commands(),
}
response = self._network.post_with_digest(execution_url, post_data, Secret.get(), error_on_failure=True)
subjob_executor_id = response.json().get('executor_id')
analytics.record_event(analytics.MASTER_TRIGGERED_SUBJOB, executor_id=subjob_executor_id,
build_id=subjob.build_id(), subjob_id=subjob.subjob_id(), slave_url=self.url)
def num_executors_in_use(self):
return self._num_executors_in_use.value()
def claim_executor(self):
new_count = self._num_executors_in_use.increment()
if new_count > self.num_executors:
raise Exception('Cannot claim executor on slave {}. No executors left.'.format(self.url))
return new_count
def free_executor(self):
new_count = self._num_executors_in_use.decrement()
if new_count < 0:
raise Exception('Cannot free executor on slave {}. All are free.'.format(self.url))
return new_count
#.........這裏部分代碼省略.........
示例5: Slave
# 需要導入模塊: from app.util.network import Network [as 別名]
# 或者: from app.util.network.Network import post [as 別名]
class Slave(object):
API_VERSION = 'v1'
_slave_id_counter = Counter()
def __init__(self, slave_url, num_executors):
"""
:type slave_url: str
:type num_executors: int
"""
self.url = slave_url
self.num_executors = num_executors
self.id = self._slave_id_counter.increment()
self._num_executors_in_use = Counter()
self._network = Network(min_connection_poolsize=num_executors)
self.current_build_id = None
self._is_alive = True
self._slave_api = UrlBuilder(slave_url, self.API_VERSION)
self._logger = log.get_logger(__name__)
def api_representation(self):
return {
'url': self.url,
'id': self.id,
'num_executors': self.num_executors,
'num_executors_in_use': self.num_executors_in_use(),
'current_build_id': self.current_build_id,
'is_alive': self.is_alive(),
}
def mark_as_idle(self):
"""
Do bookkeeping when this slave becomes idle. Error if the slave cannot be idle.
"""
if self._num_executors_in_use.value() != 0:
raise Exception('Trying to mark slave idle while {} executors still in use.',
self._num_executors_in_use.value())
self.current_build_id = None
def setup(self, build):
"""
Execute a setup command on the slave for the specified build. The setup process executes asynchronously on the
slave and the slave will alert the master when setup is complete and it is ready to start working on subjobs.
:param build: The build to set up this slave to work on
:type build: Build
"""
slave_project_type_params = build.build_request.build_parameters().copy()
slave_project_type_params.update(build.project_type.slave_param_overrides())
setup_url = self._slave_api.url('build', build.build_id(), 'setup')
post_data = {
'project_type_params': slave_project_type_params,
'build_executor_start_index': build.num_executors_allocated,
}
self._network.post_with_digest(setup_url, post_data, Secret.get())
self.current_build_id = build.build_id()
def teardown(self):
"""
Tell the slave to run the build teardown
"""
if self.is_alive():
teardown_url = self._slave_api.url('build', self.current_build_id, 'teardown')
self._network.post(teardown_url)
else:
self._logger.notice('Teardown request to slave {} was not sent since slave is disconnected.', self.url)
def start_subjob(self, subjob):
"""
:type subjob: Subjob
"""
if not self.is_alive():
raise RuntimeError('Tried to start a subjob on a dead slave! ({}, id: {})'.format(self.url, self.id))
SafeThread(target=self._async_start_subjob, args=(subjob,)).start()
def _async_start_subjob(self, subjob):
"""
:type subjob: Subjob
"""
execution_url = self._slave_api.url('build', subjob.build_id(), 'subjob', subjob.subjob_id())
post_data = {
'subjob_artifact_dir': subjob.artifact_dir(),
'atomic_commands': subjob.atomic_commands(),
}
response = self._network.post_with_digest(execution_url, post_data, Secret.get(), error_on_failure=True)
subjob_executor_id = response.json().get('executor_id')
analytics.record_event(analytics.MASTER_TRIGGERED_SUBJOB, executor_id=subjob_executor_id,
build_id=subjob.build_id(), subjob_id=subjob.subjob_id(), slave_id=self.id)
def num_executors_in_use(self):
return self._num_executors_in_use.value()
def claim_executor(self):
new_count = self._num_executors_in_use.increment()
if new_count > self.num_executors:
raise Exception('Cannot claim executor on slave {}. No executors left.'.format(self.url))
#.........這裏部分代碼省略.........