def _iterate_plugins(self, plugins):
"""Iterates plugins."""
plugins = iter(plugins)
def log_plugin_failure(failure, plugin_instance):
if failure.check(TimeoutError, defer.TimeoutError):
self._logger.debug("Plugin %s reported a timeout",
plugin_instance.alias, exc_info=True)
raise AbortedJobError(
"Plugin %s reported a timeout" % plugin_instance.alias)
elif failure.check(SuggestedReschedule):
self._logger.debug("Plugin %s suggested a reschedule in "
"%d seconds",
plugin_instance, failure.value.delay)
elif failure.check(db.ResetDBConnectionError):
pass
else:
log_unhandled_failure(self._logger,
failure,
"Plugin %s reported an unhandled failure",
plugin_instance)
return failure
def next_plugin(result=None):
self._raise_if_cancelled()
try:
plugin_instance = next(plugins)
except StopIteration:
return result
self._logger.debug("Now calling plugin: %s", plugin_instance)
self._start_plugin_timer(plugin_instance)
df = defer.maybeDeferred(plugin_instance.handle)
df.addErrback(self._stop_plugin_timer)
df.addErrback(log_plugin_failure, plugin_instance)
df.addCallback(self._stop_plugin_timer)
df.addCallback(next_plugin)
return df
return next_plugin()
python类TimeoutError()的实例源码
def _do_check(self):
self._logger.debug("checking SNMP%s availability",
self.agent.snmpVersion)
try:
result = yield self.agent.walk(SYSTEM_OID)
except (defer.TimeoutError, error.TimeoutError):
self._logger.debug("SNMP%s timed out", self.agent.snmpVersion)
returnValue(False)
self._logger.debug("SNMP response: %r", result)
returnValue(bool(result))
def _handle_failure(self, failure, ip=None):
"""Logs DNS failures, but does not stop the job from running."""
failtype = failure.trap(error.TimeoutError, defer.TimeoutError,
DomainError)
if failtype in (error.TimeoutError, defer.TimeoutError):
self._logger.warning("DNS lookup timed out")
elif failtype == DomainError:
self._logger.warning("DNS lookup error for %s: %s",
ip, failure.type.__name__)
def _ignore_timeout(self, failure, result=None):
"""Ignores a TimeoutError in an errback chain.
The result argument will be returned, and there injected into the
regular callback chain.
"""
failure.trap(error.TimeoutError, defer.TimeoutError)
self._logger.debug("request timed out, ignoring and moving on...")
return result
def _collect_bandwidth(self, netboxes):
for mib in self._mibs_for_me(BANDWIDTH_MIBS):
try:
metrics = yield self._collect_bandwidth_from_mib(mib, netboxes)
except (TimeoutError, defer.TimeoutError):
self._logger.debug("collect_bandwidth: ignoring timeout in %s",
mib.mib['moduleName'])
else:
if metrics:
defer.returnValue(metrics)
defer.returnValue([])
def __timeout_handler(self, failure, descr):
"""Handles timeouts while processing alternate MIB instances.
Under the premise that we may have an incorrect community string for a
MIB instance, we don't want to derail the entire process of collecting
from all instances, so we ignore timeouts for anything but the primary
(base) instance.
"""
if self.agent_proxy is not self._base_agent:
failure.trap(TimeoutError, defer.TimeoutError)
self._logger.debug("ignoring timeout from %r", descr)
return None
return failure
def connect(self):
"""Start connection to remote server."""
if self.state != "disconnected":
raise RuntimeError("can't connect in this state")
self.state = "connecting"
if not self.factoryStarted:
self.factory.doStart()
self.factoryStarted = 1
self.transport = transport = self._makeTransport()
if self.timeout is not None:
self.timeoutID = self.reactor.callLater(self.timeout, transport.failIfNotConnected, error.TimeoutError())
self.factory.startedConnecting(self)
def testCallBeforeStartupUnexecuted(self):
progname = self.mktemp()
with open(progname, 'w') as progfile:
progfile.write(_callBeforeStartupProgram % {'reactor': reactor.__module__})
def programFinished(result):
(out, err, reason) = result
if reason.check(error.ProcessTerminated):
self.fail("Process did not exit cleanly (out: %s err: %s)" % (out, err))
if err:
log.msg("Unexpected output on standard error: %s" % (err,))
self.assertFalse(
out,
"Expected no output, instead received:\n%s" % (out,))
def programTimeout(err):
err.trap(error.TimeoutError)
proto.signalProcess('KILL')
return err
env = os.environ.copy()
env['PYTHONPATH'] = os.pathsep.join(sys.path)
d = defer.Deferred().addCallbacks(programFinished, programTimeout)
proto = ThreadStartupProcessProtocol(d)
reactor.spawnProcess(proto, sys.executable, ('python', progname), env)
return d
def queryUDP(self, queries, timeout = None):
"""
Make a number of DNS queries via UDP.
@type queries: A C{list} of C{dns.Query} instances
@param queries: The queries to make.
@type timeout: Sequence of C{int}
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@rtype: C{Deferred}
@raise C{twisted.internet.defer.TimeoutError}: When the query times
out.
"""
if timeout is None:
timeout = self.timeout
addresses = self.servers + list(self.dynServers)
if not addresses:
return defer.fail(IOError("No domain name servers available"))
# Make sure we go through servers in the list in the order they were
# specified.
addresses.reverse()
used = addresses.pop()
d = self._query(used, queries, timeout[0])
d.addErrback(self._reissue, addresses, [used], queries, timeout)
return d
def _reissue(self, reason, addressesLeft, addressesUsed, query, timeout):
reason.trap(dns.DNSQueryTimeoutError)
# If there are no servers left to be tried, adjust the timeout
# to the next longest timeout period and move all the
# "used" addresses back to the list of addresses to try.
if not addressesLeft:
addressesLeft = addressesUsed
addressesLeft.reverse()
addressesUsed = []
timeout = timeout[1:]
# If all timeout values have been used this query has failed. Tell the
# protocol we're giving up on it and return a terminal timeout failure
# to our caller.
if not timeout:
return failure.Failure(defer.TimeoutError(query))
# Get an address to try. Take it out of the list of addresses
# to try and put it ino the list of already tried addresses.
address = addressesLeft.pop()
addressesUsed.append(address)
# Issue a query to a server. Use the current timeout. Add this
# function as a timeout errback in case another retry is required.
d = self._query(address, query, timeout[0], reason.value.id)
d.addErrback(self._reissue, addressesLeft, addressesUsed, query, timeout)
return d
def _timeoutZone(self, d, controller, connector, seconds):
connector.disconnect()
controller.timeoutCall = None
controller.deferred = None
d.errback(error.TimeoutError("Zone lookup timed out after %d seconds" % (seconds,)))
def testTimeout(self):
def login():
d = self.client.login('test', 'twisted')
d.addCallback(loggedIn)
d.addErrback(timedOut)
return d
def loggedIn(result):
self.fail("Successfully logged in!? Impossible!")
def timedOut(failure):
failure.trap(error.TimeoutError)
self._cbStopClient(None)
def quit():
return self.client.quit()
self.client.timeout = 0.01
# Tell the server to not return a response to client. This
# will trigger a timeout.
pop3testserver.TIMEOUT_RESPONSE = True
methods = [login, quit]
map(self.connected.addCallback, map(strip, methods))
self.connected.addCallback(self._cbStopClient)
self.connected.addErrback(self._ebGeneral)
return self.loopback()
def connectionLost(self, reason):
"""
Clean up when the connection has been lost.
When the loss of connection was initiated by the client due to a
timeout, the L{_timedOut} flag will be set. When it was initiated by
the client due to an error in the server greeting, L{_greetingError}
will be set to the server response minus the status indicator.
@type reason: L{Failure <twisted.python.failure.Failure>}
@param reason: The reason the connection was terminated.
"""
if self.timeout > 0:
self.setTimeout(None)
if self._timedOut:
reason = error.TimeoutError()
elif self._greetingError:
reason = ServerErrorResponse(self._greetingError)
d = []
if self._waiting is not None:
d.append(self._waiting)
self._waiting = None
if self._blockedQueue is not None:
d.extend([deferred for (deferred, f, a) in self._blockedQueue])
self._blockedQueue = None
for w in d:
w.errback(reason)
def get_initial_wmiprvse_stats(config):
initial_wmiprvse_stats = {}
good_conn_infos = []
for conn_info in config.conn_infos:
try:
client = create_winrm_client(conn_info)
initial_wmiprvse_stats[conn_info.hostname] = \
yield get_remote_process_stats(client)
good_conn_infos.append(conn_info)
except UnauthorizedError:
continue
except TimeoutError:
continue
defer.returnValue((initial_wmiprvse_stats, good_conn_infos))
def receive(self):
try:
receive_elem = yield self._sender.send_request(
'receive',
shell_id=self._shell_id,
command_id=self._command_id)
except TimeoutError:
yield self._sender.close_connections()
raise
stdout_parts = _find_stream(receive_elem, self._command_id, 'stdout')
stderr_parts = _find_stream(receive_elem, self._command_id, 'stderr')
self._exit_code = _find_exit_code(receive_elem, self._command_id)
stdout = _stripped_lines(stdout_parts)
stderr = _stripped_lines(stderr_parts)
defer.returnValue((stdout, stderr))
def onReceiveFail(self, failure):
e = failure.value
if isinstance(e, defer.CancelledError):
return
retry, level, msg = (False, None, None) # NOT USED.
self._errorMsgCheck(e.message)
# Handle errors on which we should retry the receive.
if 'OperationTimeout' in e.message:
retry, level, msg = (
True,
logging.DEBUG,
"OperationTimeout on {}"
.format(self.config.id))
elif isinstance(e, ConnectError):
retry, level, msg = (
isinstance(e, TimeoutError),
logging.WARN,
"network error on {}: {}"
.format(self.config.id, e.message or 'timeout'))
if isinstance(e, TimeoutError):
self.network_failures += 1
# Handle errors on which we should start over.
else:
retry, level, msg = (
False,
logging.WARN,
"receive failure on {}: {}"
.format(self.config.id, e))
if self.data_deferred and not self.data_deferred.called:
self.data_deferred.errback(failure)
LOG.log(level, msg)
if self.network_failures >= MAX_NETWORK_FAILURES:
yield self.stop()
self.reset()
if retry:
self.receive()
else:
yield self.restart()
defer.returnValue(None)