本文整理汇总了Python中app.util.unhandled_exception_handler.UnhandledExceptionHandler类的典型用法代码示例。如果您正苦于以下问题:Python UnhandledExceptionHandler类的具体用法?Python UnhandledExceptionHandler怎么用?Python UnhandledExceptionHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UnhandledExceptionHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, port, host, num_executors=10):
"""
:param port: The port number the slave service is running on
:type port: int
:param host: The hostname at which the slave is reachable
:type host: str
:param num_executors: The number of executors this slave should operate with -- this determines how many
concurrent subjobs the slave can execute.
:type num_executors: int
"""
self.port = port
self.host = host
self._slave_id = None
self._num_executors = num_executors
self._logger = log.get_logger(__name__)
self._idle_executors = Queue(maxsize=num_executors)
self.executors = {}
for executor_id in range(num_executors):
executor = SubjobExecutor(executor_id)
self._idle_executors.put(executor)
self.executors[executor_id] = executor
self._setup_complete_event = Event()
self._master_url = None
self._network = Network(min_connection_poolsize=num_executors)
self._master_api = None # wait until we connect to a master first
self._project_type = None # this will be instantiated during build setup
self._current_build_id = None
UnhandledExceptionHandler.singleton().add_teardown_callback(self._async_teardown_build,
should_disconnect_from_master=True)
示例2: setUp
def setUp(self):
super().setUp()
self._set_up_safe_guards()
# Reset singletons so that they get recreated for every test that uses them.
Configuration.reset_singleton()
UnhandledExceptionHandler.reset_singleton()
# Explicitly initialize UnhandledExceptionHandler singleton here (on the main thread) since it sets up signal
# handlers that must execute on the main thread.
UnhandledExceptionHandler.singleton()
MasterConfigLoader().configure_defaults(Configuration.singleton())
MasterConfigLoader().configure_postload(Configuration.singleton())
self.patch('app.util.conf.master_config_loader.MasterConfigLoader.load_from_config_file')
# Configure logging to go to stdout. This makes debugging easier by allowing us to see logs for failed tests.
log.configure_logging('DEBUG')
# Then stub out configure_logging so we don't end up logging to real files during testing.
self.patch('app.util.log.configure_logging')
# Set up TestHandler. This allows asserting on log messages in tests.
self.log_handler = logbook.TestHandler(bubble=True)
self.log_handler.push_application()
self._base_setup_called = True
示例3: test_handles_platform_does_not_support_SIGINFO
def test_handles_platform_does_not_support_SIGINFO(self):
UnhandledExceptionHandler.reset_singleton()
mock_signal = self.patch('app.util.unhandled_exception_handler.signal')
def register_signal_handler(sig, _):
if sig == process_utils.SIGINFO:
raise ValueError
mock_signal.signal.side_effect = register_signal_handler
UnhandledExceptionHandler.singleton()
示例4: initialize_unhandled_exception_handler
def initialize_unhandled_exception_handler():
# Note: Unfortunately we can't use `self.assertRaises` here since this executes on a different thread.
# todo: After exceptions in test threads are being caught, simplify this test to use self.assertRaises.
UnhandledExceptionHandler.reset_singleton()
try:
UnhandledExceptionHandler.singleton()
except Exception:
nonlocal exception_raised
exception_raised = True
示例5: _write_pid_file
def _write_pid_file(self, filename):
fs.write_file(str(os.getpid()), filename)
def remove_pid_file():
try:
os.remove(filename)
except OSError:
pass
UnhandledExceptionHandler.singleton().add_teardown_callback(remove_pid_file)
示例6: test_calling_kill_subprocesses_will_break_out_of_command_execution_wait_loop
def test_calling_kill_subprocesses_will_break_out_of_command_execution_wait_loop(self):
def fake_communicate(timeout=None):
# The fake implementation is that communicate() times out forever until os.killpg is called.
if mock_killpg.call_count == 0 and timeout is not None:
raise TimeoutExpired(None, timeout)
elif mock_killpg.call_count > 0:
return b'fake output', b'fake error'
self.fail('Popen.communicate() should not be called without a timeout before os.killpg has been called.')
mock_killpg = self.patch('os.killpg')
self.mock_popen.communicate.side_effect = fake_communicate
self.mock_popen.returncode = 1
self.mock_popen.pid = 55555
project_type = ProjectType()
command_thread = SafeThread(target=project_type.execute_command_in_project, args=('echo The power is yours!',))
# This calls execute_command_in_project() on one thread, and calls kill_subprocesses() on another. The
# kill_subprocesses() call should cause the first thread to exit.
command_thread.start()
project_type.kill_subprocesses()
# This *should* join immediately, but we specify a timeout just in case something goes wrong so that the test
# doesn't hang. A successful join implies success. We also use the UnhandledExceptionHandler so that exceptions
# propagate from the child thread to the test thread and fail the test.
with UnhandledExceptionHandler.singleton():
command_thread.join(timeout=10)
if command_thread.is_alive():
mock_killpg() # Calling killpg() causes the command thread to end.
self.fail('project_type.kill_subprocesses should cause the command execution wait loop to exit.')
mock_killpg.assert_called_once_with(pgid=55555, sig=ANY)
示例7: _start_application
def _start_application(self, application, port):
# Note: No significant application logic should be executed before this point. The call to application.listen()
# will raise an exception if another process is using the same port. We rely on this exception to force us to
# exit if there are any port conflicts.
try:
application.listen(port, '0.0.0.0')
except OSError:
self._logger.error('Could not start application on port {}. Is port already in use?'.format(port))
sys.exit(1)
ioloop = tornado.ioloop.IOLoop.instance()
# add a teardown callback that will stop the tornado server
stop_tornado_ioloop = functools.partial(ioloop.add_callback, callback=ioloop.stop)
UnhandledExceptionHandler.singleton().add_teardown_callback(stop_tornado_ioloop)
return ioloop
示例8: test_executing_build_teardown_multiple_times_will_raise_exception
def test_executing_build_teardown_multiple_times_will_raise_exception(self):
self.mock_network.post().status_code = http.client.OK
slave = self._create_cluster_slave()
project_type_mock = self.patch('app.slave.cluster_slave.util.create_project_type').return_value
# This test uses setup_complete_event to detect when the async fetch_project() has executed.
setup_complete_event = Event()
project_type_mock.fetch_project.side_effect = self.no_args_side_effect(setup_complete_event.set)
# This test uses teardown_event to cause a thread to block on the teardown_build() call.
teardown_event = Event()
project_type_mock.teardown_build.side_effect = self.no_args_side_effect(teardown_event.wait)
slave.connect_to_master(self._FAKE_MASTER_URL)
slave.setup_build(build_id=123, project_type_params={'type': 'Fake'}, build_executor_start_index=0)
self.assertTrue(setup_complete_event.wait(timeout=5), 'Build setup should complete very quickly.')
# Start the first thread that does build teardown. This thread will block on teardown_build().
first_thread = SafeThread(target=slave._do_build_teardown_and_reset)
first_thread.start()
# Call build teardown() again and it should raise an exception.
with self.assertRaises(BuildTeardownError):
slave._do_build_teardown_and_reset()
# Cleanup: Unblock the first thread and let it finish. We use the unhandled exception handler just in case any
# exceptions occurred on the thread (so that they'd be passed back to the main thread and fail the test).
teardown_event.set()
with UnhandledExceptionHandler.singleton():
first_thread.join()
示例9: trigger_graceful_app_shutdown
def trigger_graceful_app_shutdown(self):
"""
Helper method to easily trigger graceful shutdown. The side effect of this is that all teardown handlers that
are registered with UnhandledExceptionHandler will be executed. This method will raise a SystemExit if an
exception is raised in any of the teardown handlers.
"""
with UnhandledExceptionHandler.singleton():
raise _GracefulShutdownTrigger
示例10: _start_build
def _start_build(self):
"""
Send the build request to the master for execution.
"""
build_url = self._master_api.url('build')
# todo: catch connection error
response = self._network.post_with_digest(build_url, self._request_params, self._secret, error_on_failure=True)
response_data = response.json()
if 'error' in response_data:
error_message = response_data['error']
raise _BuildRunnerError('Error starting build: ' + error_message)
self._build_id = response_data['build_id']
UnhandledExceptionHandler.singleton().add_teardown_callback(self._cancel_build)
self._logger.info('Build is running. (Build id: {})', self._build_id)
示例11: setUp
def setUp(self):
super().setUp()
self.addCleanup(patch.stopall)
self._patched_items = {}
self._blacklist_methods_not_allowed_in_unit_tests()
# Stub out a few library dependencies that launch subprocesses.
self.patch('app.util.autoversioning.get_version').return_value = '0.0.0'
self.patch('app.util.conf.base_config_loader.platform.node').return_value = self._fake_hostname
if self._do_network_mocks:
# requests.Session() also makes some subprocess calls on instantiation.
self.patch('app.util.network.requests.Session')
# Stub out Network.are_hosts_same() call with a simple string comparison.
self.patch('app.util.network.Network.are_hosts_same', new=lambda host_a, host_b: host_a == host_b)
# Reset singletons so that they get recreated for every test that uses them.
Configuration.reset_singleton()
UnhandledExceptionHandler.reset_singleton()
SlaveRegistry.reset_singleton()
# Explicitly initialize UnhandledExceptionHandler singleton here (on the main thread) since it sets up signal
# handlers that must execute on the main thread.
UnhandledExceptionHandler.singleton()
MasterConfigLoader().configure_defaults(Configuration.singleton())
MasterConfigLoader().configure_postload(Configuration.singleton())
self.patch('app.util.conf.master_config_loader.MasterConfigLoader.load_from_config_file')
# Reset counters
Slave._slave_id_counter = Counter()
Build._build_id_counter = Counter()
analytics._event_id_generator = Counter()
# Configure logging to go to stdout. This makes debugging easier by allowing us to see logs for failed tests.
log.configure_logging('DEBUG')
# Then stub out configure_logging so we don't end up logging to real files during testing.
self.patch('app.util.log.configure_logging')
# Set up TestHandler. This allows asserting on log messages in tests.
self.log_handler = logbook.TestHandler(bubble=True)
self.log_handler.push_application()
self._base_setup_called = True
示例12: _start_application
def _start_application(self, application, port):
# Note: No significant application logic should be executed before this point. The call to application.listen()
# will raise an exception if another process is using the same port. We rely on this exception to force us to
# exit if there are any port conflicts.
try:
# If SSL cert and key files are provided in configuration, ClusterRunner wil start with HTTPS protocol.
# Otherwise ClusterRunner will start with HTTP protocol.
server = HTTPServer(application, ssl_options=self._get_https_options())
server.listen(port, '0.0.0.0')
except OSError:
self._logger.error('Could not start application on port {}. Is port already in use?'.format(port))
sys.exit(1)
ioloop = tornado.ioloop.IOLoop.instance()
# add a teardown callback that will stop the tornado server
stop_tornado_ioloop = functools.partial(ioloop.add_callback, callback=ioloop.stop)
UnhandledExceptionHandler.singleton().add_teardown_callback(stop_tornado_ioloop)
return ioloop
示例13: connect_to_master
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 | None
"""
self.is_alive = True
self._master_url = master_url or "localhost:43000"
self._master_api = UrlBuilder(self._master_url)
connect_url = self._master_api.url("slave")
data = {"slave": "{}:{}".format(self.host, self.port), "num_executors": self._num_executors}
response = self._network.post(connect_url, data=data)
self._slave_id = int(response.json().get("slave_id"))
self._logger.info("Slave {}:{} connected to master on {}.", self.host, self.port, self._master_url)
# We disconnect from the master before build_teardown so that the master stops sending subjobs. (Teardown
# callbacks are executed in the reverse order that they're added, so we add the build_teardown callback first.)
UnhandledExceptionHandler.singleton().add_teardown_callback(self._do_build_teardown_and_reset, timeout=30)
UnhandledExceptionHandler.singleton().add_teardown_callback(self._disconnect_from_master)
示例14: test_exception_on_safe_thread_calls_teardown_callbacks
def test_exception_on_safe_thread_calls_teardown_callbacks(self):
my_awesome_teardown_callback = MagicMock()
unhandled_exception_handler = UnhandledExceptionHandler.singleton()
unhandled_exception_handler.add_teardown_callback(my_awesome_teardown_callback, 'fake arg', fake_kwarg='boop')
def my_terrible_method():
raise Exception('Sic semper tyrannis!')
thread = SafeThread(target=my_terrible_method)
thread.start()
thread.join()
my_awesome_teardown_callback.assert_called_once_with('fake arg', fake_kwarg='boop')
示例15: test_normal_execution_on_safe_thread_does_not_call_teardown_callbacks
def test_normal_execution_on_safe_thread_does_not_call_teardown_callbacks(self):
my_lonely_teardown_callback = MagicMock()
unhandled_exception_handler = UnhandledExceptionHandler.singleton()
unhandled_exception_handler.add_teardown_callback(my_lonely_teardown_callback)
def my_fantastic_method():
print('Veritas vos liberabit!')
thread = SafeThread(target=my_fantastic_method)
thread.start()
thread.join()
self.assertFalse(my_lonely_teardown_callback.called,
'The teardown callback should not be called unless an exception is raised.')