def _do_get_connection(account, conductor, ready, retries_left, backoff): this_ready = defer.Deferred() factory = ImapClientFactory(account, conductor, this_ready) factory.connect() try: conn = yield this_ready # yay - report we are good and tell the real callback we have it. account.reportStatus(brat.EVERYTHING, brat.GOOD) ready.callback(conn) except Exception, exc: fail = Failure() logger.debug("first chance connection error handling: %s\n%s", fail.getErrorMessage(), fail.getBriefTraceback()) retries_left -= 1 if retries_left <= 0: ready.errback(fail) else: status = failure_to_status(fail) account.reportStatus(**status) acct_id = account.details.get("id", "") logger.warning( "Failed to connect to account %r, will retry after %s secs: %s", acct_id, backoff, fail.getErrorMessage(), ) next_backoff = min(backoff * 2, MAX_BACKOFF) # magic number conductor.reactor.callLater( backoff, _do_get_connection, account, conductor, ready, retries_left, next_backoff )
def invoke(self, name, args): """Invoke Exposed Methods @param name (str) - name of method to invoke @param args (tuple) - arguments to pass to invoked method @return (defer.Deferred) """ if name not in self.exposedMethods: return defer.fail( DroneCommandFailed( self.resultContext( "[%(application)s] Unknown method '%(method)s'", method=name, error='unknown method'))) try: #our own form of maybeDeferred d = self.exposedMethods[name](*args) if isinstance(d, defer.Deferred): action = Action(' '.join([str(i) for i in \ (self.action, name) + tuple(args)]), d) return action.deferred elif isinstance(d, DroneCommandFailed): return defer.fail(d) elif isinstance(d, dict): return defer.succeed(d) elif isinstance(d, type(None)): #this just feels dirty return defer.succeed(d) elif isinstance(d, Failure): d.raiseException() #sigh #probably from a triggerred Event callback elif type(d) == types.InstanceType: return defer.succeed(None) return defer.fail(FormatError("Result is not formatted correctly you " + \ "must return self.resultContext or DroneCommandFailed." + \ "\nResult: <%s>" % (str(d),))) except: failure = Failure() if failure.check(DroneCommandFailed): template = "[%(application)s] %(description)s" context = failure.value.resultContext if not 'description' in context: context['description'] = failure.getErrorMessage() else: template = "[%(application)s] " + "%s: %s" % ( getException(failure), failure.getErrorMessage()) context = { 'error': True, 'code': -2, 'stacktrace': failure.getTraceback() } return defer.fail( DroneCommandFailed( self.resultContext(template, None, **context)))
def __unexpectedError(self, failure=None, task=None): self._fireStatusChanged(TranscoderStatusEnum.unexpected_error) if not failure: failure = Failure() self.onJobError(failure.getErrorMessage()) log.notifyFailure(self, failure, "Unexpected error%s", (task and " during %s" % task) or "", cleanTraceback=True) m = messages.Error(T_(failure.getErrorMessage()), debug=log.getFailureMessage(failure)) self.addMessage(m) return failure
def invoke(self, name, args): """Invoke Exposed Methods @param name (str) - name of method to invoke @param args (tuple) - arguments to pass to invoked method @return (defer.Deferred) """ if name not in self.exposedMethods: return defer.fail(DroneCommandFailed(self.resultContext( "[%(application)s] Unknown method '%(method)s'", method=name, error='unknown method')) ) try: #our own form of maybeDeferred d = self.exposedMethods[name](*args) if isinstance(d, defer.Deferred): action = Action(' '.join([str(i) for i in \ (self.action, name) + tuple(args)]), d) return action.deferred elif isinstance(d, DroneCommandFailed): return defer.fail(d) elif isinstance(d, dict): return defer.succeed(d) elif isinstance(d, type(None)): #this just feels dirty return defer.succeed(d) elif isinstance(d, Failure): d.raiseException() #sigh #probably from a triggerred Event callback elif type(d) == types.InstanceType: return defer.succeed(None) return defer.fail(FormatError("Result is not formatted correctly you " + \ "must return self.resultContext or DroneCommandFailed." + \ "\nResult: <%s>" % (str(d),))) except: failure = Failure() if failure.check(DroneCommandFailed): template = "[%(application)s] %(description)s" context = failure.value.resultContext if not 'description' in context: context['description'] = failure.getErrorMessage() else: template = "[%(application)s] " + "%s: %s" % (getException(failure), failure.getErrorMessage()) context = {'error': True, 'code':-2, 'stacktrace': failure.getTraceback()} return defer.fail(DroneCommandFailed(self.resultContext(template, None, **context)) )
def __call__(self, argstr): args = argstr.split() resultContext = None if not args: #return command usage methods = {} for name,args,doc in self.exposedMethodInfo: methods[name] = {'args' : args, 'doc' : doc} resultContext = dict(description=self.__doc__, methods=methods) yield resultContext else: method = args.pop(0) try: wfd = defer.waitForDeferred( self.invoke(method,args) ) yield wfd resultContext = wfd.getResult() except: failure = Failure() if failure.check(DroneCommandFailed): resultContext = failure.value.resultContext else: #be nice and return something to the end user template = "[%(application)s] " template += "%s: %s" % (getException(failure), failure.getErrorMessage()) context = {'error': True, 'code': -2, 'stacktrace': failure.getTraceback()} resultContext = self.resultContext(template, None, **context ) yield resultContext
def _start_stop_common(self, label, action): result = {} try: function = getattr(self.model, action) d = defer.maybeDeferred(function, label) wfd = defer.waitForDeferred(d) yield wfd result = wfd.getResult() #take this time to update the instance if isinstance(result, dict): thisInst = self.model.getInstance(label) thisInst.updateInfo(result) except: failure = Failure() if failure.check(DroneCommandFailed): result = failure.value.resultContext else: #log the error, allowing for debugging self.debugReport() #be nice and return something to the end user template = "%s: %s" % (getException(failure), failure.getErrorMessage()) context = {'error':failure,'code':-2} result = self.resultContext(template, None, **context) #finally wrap the failure into a known type result = Failure(DroneCommandFailed(result)) #AppInstances need a moment to be updated d = defer.Deferred() reactor.callLater(1.0, d.callback, None) wfd = defer.waitForDeferred(d) yield wfd wfd.getResult() yield result
def decorator(*args, **kwargs): try: return func(*args, **kwargs) except: failure = Failure() msg = getException(failure) msg += ': ' + failure.getErrorMessage() return NoResource(msg)
def __call__(self, argstr): args = argstr.split() resultContext = None if not args: #return command usage methods = {} for name, args, doc in self.exposedMethodInfo: methods[name] = {'args': args, 'doc': doc} resultContext = dict(description=self.__doc__, methods=methods) yield resultContext else: method = args.pop(0) try: wfd = defer.waitForDeferred(self.invoke(method, args)) yield wfd resultContext = wfd.getResult() except: failure = Failure() if failure.check(DroneCommandFailed): resultContext = failure.value.resultContext else: #be nice and return something to the end user template = "[%(application)s] " template += "%s: %s" % (getException(failure), failure.getErrorMessage()) context = { 'error': True, 'code': -2, 'stacktrace': failure.getTraceback() } resultContext = self.resultContext(template, None, **context) yield resultContext
def _log_download_errors(self, spider_failure: Failure, download_failure: Failure, request: Request, spider: Spider) -> Union[Failure, None]: """Log and silence errors that come from the engine (typically download errors that got propagated thru here). spider_failure: the value passed into the errback of self.call_spider() download_failure: the value passed into _scrape2() from ExecutionEngine._handle_downloader_output() as "result" """ if not download_failure.check(IgnoreRequest): if download_failure.frames: logkws = self.logformatter.download_error(download_failure, request, spider) logger.log( *logformatter_adapter(logkws), extra={'spider': spider}, exc_info=failure_to_exc_info(download_failure), ) else: errmsg = download_failure.getErrorMessage() if errmsg: logkws = self.logformatter.download_error( download_failure, request, spider, errmsg) logger.log( *logformatter_adapter(logkws), extra={'spider': spider}, ) if spider_failure is not download_failure: return spider_failure return None
def msgBufResponse(self, result): """Format results, encode, and respond""" response, contentType = result try: return { 'response': urllib.quote(blaster.Serialize().execute( contentType, drone.formatResults(response))), 'type': contentType, 'code': 200 } except: failure = Failure() Msg = { 'code': 1, 'description': failure.getErrorMessage(), 'stacktrace': failure.getTraceback(), 'error': True } return { 'response': urllib.quote(blaster.Serialize().execute(contentType, Msg)), 'type': 'text/plain', 'code': 500 }
def deferredDHCPRequestErrback(self, failure: Failure) -> Optional[Failure]: if failure.check(FirstError): # If an error occurred, cancel any other pending requests. # (The error is likely to occur for those requests, too.) # Unfortunately we can't cancel using the DeferredList, since # the DeferredList considers itself "called" the moment the first # errback is invoked. self.cancelAll(self.deferredDHCPRequests) # Suppress further error handling. The original Deferred's errback # has already been called. return None elif failure.check(DHCPProbeException): log.msg("DHCP probe failed. %s" % failure.getErrorMessage()) elif failure.check(CancelledError): # Intentionally cancelled; no need to spam the log. pass else: log.err( failure, "DHCP probe on '%s' failed with an unknown error." % (self.ifname), ) # Make sure the error is propagated to the DeferredList. # We need this so that the DeferredList knows to call us with # FirstError, which is our indicator to cancel the remaining calls. # (It's set to consumeErrors, so it won't spam the log.) return failure
def _start_stop_common(self, label, action): result = {} try: function = getattr(self.model, action) d = defer.maybeDeferred(function, label) wfd = defer.waitForDeferred(d) yield wfd result = wfd.getResult() #take this time to update the instance if isinstance(result, dict): thisInst = self.model.getInstance(label) thisInst.updateInfo(result) except: failure = Failure() if failure.check(DroneCommandFailed): result = failure.value.resultContext else: #log the error, allowing for debugging self.debugReport() #be nice and return something to the end user template = "%s: %s" % (getException(failure), failure.getErrorMessage()) context = {'error': failure, 'code': -2} result = self.resultContext(template, None, **context) #finally wrap the failure into a known type result = Failure(DroneCommandFailed(result)) #AppInstances need a moment to be updated d = defer.Deferred() config.reactor.callLater(1.0, d.callback, None) wfd = defer.waitForDeferred(d) yield wfd wfd.getResult() yield result
async def _purge_history(self, purge_id: str, room_id: str, token: str, delete_local_events: bool) -> None: """Carry out a history purge on a room. Args: purge_id: The ID for this purge. room_id: The room to purge from token: topological token to delete events before delete_local_events: True to delete local events as well as remote ones """ self._purges_in_progress_by_room.add(room_id) try: with await self.pagination_lock.write(room_id): await self.storage.purge_events.purge_history( room_id, token, delete_local_events) logger.info("[purge] complete") self._purges_by_id[purge_id].status = PurgeStatus.STATUS_COMPLETE except Exception: f = Failure() logger.error( "[purge] failed", exc_info=(f.type, f.value, f.getTracebackObject() ) # type: ignore ) self._purges_by_id[purge_id].status = PurgeStatus.STATUS_FAILED self._purges_by_id[purge_id].error = f.getErrorMessage() finally: self._purges_in_progress_by_room.discard(room_id) # remove the purge from the list 24 hours after it completes def clear_purge() -> None: del self._purges_by_id[purge_id] self.hs.get_reactor().callLater( PaginationHandler.CLEAR_PURGE_AFTER_MS / 1000, clear_purge)
def on_disconnect(self, reason: Failure) -> None: """ Executed when the connection is lost. """ if self.is_state(self.PeerState.READY): self.log.info('disconnected', reason=reason.getErrorMessage()) else: self.log.debug('disconnected', reason=reason.getErrorMessage()) if self._idle_timeout_call_later: self._idle_timeout_call_later.cancel() self._idle_timeout_call_later = None self.aborting = True self.update_log_context() if self.state: self.state.on_exit() self.state = None if self.connections: self.connections.on_peer_disconnect(self)
def spider_error(failure: Failure, response: Response, spider: Spider): spider.logger.error("##### HANDLE ERROR #####") spider.logger.error(f'in {spider}') spider.logger.error(failure.type) spider.logger.error(failure.getErrorMessage()) spider.logger.error("###### TRACE BACK ######") spider.logger.error(failure.getTraceback()) spider.logger.error('########################')
def errback(failure: Failure, pdu: EventBase): failure.trap(SynapseError) with PreserveLoggingContext(ctx): logger.warning( "Signature check failed for %s: %s", pdu.event_id, failure.getErrorMessage(), ) return failure
def on_connection_failure(self, failure: Failure, peer: Optional[PeerId], endpoint: IStreamClientEndpoint) -> None: self.log.warn('connection failure', endpoint=endpoint, failure=failure.getErrorMessage()) self.connecting_peers.pop(endpoint) if peer is not None: now = int(self.reactor.seconds()) peer.increment_retry_attempt(now)
def parseError(self, err, query, instMap): err = Failure(err) err.value = 'Received %s from query: %s'%(err.value, query) log.error(err.getErrorMessage()) results = {} for instances in instMap.values(): for tables in instances.values(): for table, props in tables: results[table] = [err,] return results
def home(self, _: EventDispatcher = None, error: Failure = None) -> None: if error is None or type(error.value) == ConnectionDone: Logger.info("Main : Back to Home.") else: Logger.error("Main : Back to Home : " + error.getErrorMessage()) ErrorPopup(type(error.value).__name__, error.getErrorMessage()).open() self.titleBar.switch(TitleBarCtx.HOME) setTitle("Connexion") home = Home() if self.content is not None: self.remove_widget(self.content) self.content = home self.content.bind(on_login=self.login) self.add_widget(self.content)
def _unexpected_error(self, failure=None, task=None): if not failure: failure = Failure() log.notifyFailure(self, failure, "Unexpected error%s", (task and " during %s" % task) or "", cleanTraceback=True) m = messages.Error(_(failure.getErrorMessage()), debug=log.getFailureMessage(failure)) self.addMessage(m) return failure
def newfunc(*args,**kwargs): try: return func(*args,**kwargs) except: failure = Failure() caught_exc = failure.value err_msg = failure.getErrorMessage() if failure.check(exc): raise caught_exc #1 exc_inst = exc(err_msg) exc_inst.inner_exception = caught_exc raise exc_inst
def newfunc(*args, **kwargs): try: return func(*args, **kwargs) except: failure = Failure() caught_exc = failure.value err_msg = failure.getErrorMessage() if failure.check(exc): raise caught_exc #1 exc_inst = exc(err_msg) exc_inst.inner_exception = caught_exc raise exc_inst
def __transcodingError(self, failure=None, task=None): self._fireStatusChanged(TranscoderStatusEnum.error) if not failure: failure = Failure() self.onJobError(failure.getErrorMessage()) log.notifyFailure(self, failure, "Transocding error%s", (task and " during %s" % task) or "", cleanTraceback=True) self.setMood(moods.sad) return failure
def handleDeferreds(labels): """Remember last yield is the return value, don't use return""" results = {} descriptions = [] ret = {} code = 0 for l in labels: try: d = defer.maybeDeferred(func, l, *args[1:], **kwargs) wfd = defer.waitForDeferred(d) yield wfd ret = wfd.getResult() except: failure = Failure() des = "%s: %s" % \ (getException(failure),failure.getErrorMessage()) if failure.check(DroneCommandFailed): result[l] = failure.value.resultContext if 'description' not in result[l]: result[l]['description'] = des result[l]['stacktrace'] = failure.getTraceback() result[l]['error'] = True if 'code' not in result[l]: result[l]['code'] = 1 else: ret = { 'description': des, 'code': 1, 'error': True, 'stacktrace': failure.getTraceback() } if not ret: #NoneType detection ret = {'description': str(ret), 'code': 0} if 'code' in ret: code += abs(ret['code']) results[l] = ret try: descriptions.append(results[l]['description']) except: self.debugReport() results['code'] = code try: results['description'] = '\n'.join(descriptions) except: results['description'] = None if len(labels) == 0: Label = labels[0] else: Label = None ret = self.resultContext('%(description)s', label=Label, **results) yield ret
def parseError(self, err, query, instMap): err = Failure(err) err.value = 'Received %s from query: %s' % (err.value, query) log.error(err.getErrorMessage()) results = {} for instances in instMap.values(): for tables in instances.values(): for table, props in tables: results[table] = [ err, ] return results
def on_disconnect(self, reason: Failure) -> None: """ Executed when the connection is lost. """ remote = self.transport.getPeer() self.log.info('disconnected', remote=remote, reason=reason.getErrorMessage()) self.connected = False if self.state: self.state.on_exit() if self.connections: self.connections.on_peer_disconnect(self)
def handleDeferreds(labels): """Remember last yield is the return value, don't use return""" results = {} descriptions = [] ret = {} code = 0 for l in labels: try: d = defer.maybeDeferred(func, l, *args[1:], **kwargs) wfd = defer.waitForDeferred(d) yield wfd ret = wfd.getResult() except: failure = Failure() des = "%s: %s" % \ (getException(failure),failure.getErrorMessage()) if failure.check(DroneCommandFailed): result[l] = failure.value.resultContext if 'description' not in result[l]: result[l]['description'] = des result[l]['stacktrace'] = failure.getTraceback() result[l]['error'] = True if 'code' not in result[l]: result[l]['code'] = 1 else: ret = { 'description': des, 'code': 1, 'error': True, 'stacktrace': failure.getTraceback() } if not ret: #NoneType detection ret = {'description' : str(ret), 'code' : 0} if 'code' in ret: code += abs(ret['code']) results[l] = ret try: descriptions.append(results[l]['description']) except: self.debugReport() results['code'] = code try: results['description'] = '\n'.join(descriptions) except: results['description'] = None if len(labels) == 0: Label = labels[0] else: Label = None ret = self.resultContext('%(description)s',label=Label,**results) yield ret
def parseError(self, err, query, instMap): if isinstance(err.value, pywbem.cim_http.AuthError): msg = 'AuthError: Please check zWinUser and zWinPassword zProperties' else: msg = 'Received %s from query: %s'%(err.value[1], query) err = Failure(err) err.value = msg log.error(err.getErrorMessage()) results = {} for instances in instMap.values(): for tables in instances.values(): for table, props in tables: results[table] = [err] return results
def parseError(self, err, query, instMap): if isinstance(err.value, pywbem.cim_http.AuthError): msg = 'AuthError: Please check zWinUser and zWinPassword zProperties' else: msg = 'Received %s from query: %s' % (err.value[1], query) err = Failure(err) err.value = msg log.error(err.getErrorMessage()) results = {} for instances in instMap.values(): for tables in instances.values(): for table, props in tables: results[table] = [err] return results
def resultContext(self, template, instance=None, **context): """Creates a dict containg relevant contextual information about a result. You can override this method and tailor it to your liking. We typically use this to pass verbose structured data to a master DroneD controller (not provided with DroneD core) so that it may quickly make decisions based on the result of it's previous command and control activities. IF you set 'error' in the **context this will raise a server error at the remote end. This can be good or bad depending on your outlook on exceptions. Consider this your only warning. return dict """ if 'application' not in context: context['application'] = self.action failure = context.pop('error', False) if isinstance(failure, Failure): if 'description' not in context: context['description'] = '[%s] %s: %s' % \ (self.action, getException(failure), failure.getErrorMessage()) if 'code' not in context: context['code'] = -2 context['error'] = True context['stacktrace'] = failure.getTraceback() self.log('Result context during exception\n%(stacktrace)s' % context) return context #failed so bad we need to shortcut out else: context['error'] = bool(failure) if instance: #this was made for AppManager's if hasattr(instance, 'version'): context['version'] = instance.version if hasattr(instance, 'label'): context['label'] = instance.label if hasattr(instance, 'running'): context['running'] = instance.running try: #fail-safe in case someone is a bonehead context['description'] = template % context except: failure = Failure() context['description'] = '[%s] %s: %s' % \ (self.action, getException(failure), failure.getErrorMessage()) context['stacktrace'] = failure.getTraceback() if 'code' not in context: context['code'] = -2 #be nice to blaster api and the remote client context.update({'code': context.get('code', 0)}) return context
def resultContext(self, template, instance=None, **context): """Creates a dict containg relevant contextual information about a result. You can override this method and tailor it to your liking. We typically use this to pass verbose structured data to a master DroneD controller (not provided with DroneD core) so that it may quickly make decisions based on the result of it's previous command and control activities. IF you set 'error' in the **context this will raise a server error at the remote end. This can be good or bad depending on your outlook on exceptions. Consider this your only warning. return dict """ if 'application' not in context: context['application'] = self.action failure = context.pop('error', False) if isinstance(failure, Failure): if 'description' not in context: context['description'] = '[%s] %s: %s' % \ (self.action, getException(failure), failure.getErrorMessage()) if 'code' not in context: context['code'] = -2 context['error'] = True context['stacktrace'] = failure.getTraceback() self.log('Result context during exception\n%(stacktrace)s' % context) return context #failed so bad we need to shortcut out else: context['error'] = bool(failure) if instance: #this was made for AppManager's if hasattr(instance, 'version'): context['version'] = instance.version if hasattr(instance, 'label'): context['label'] = instance.label if hasattr(instance, 'running'): context['running'] = instance.running try: #fail-safe in case someone is a bonehead context['description'] = template % context except: failure = Failure() context['description'] = '[%s] %s: %s' % \ (self.action, getException(failure), failure.getErrorMessage()) context['stacktrace'] = failure.getTraceback() if 'code' not in context: context['code'] = -2 #be nice to blaster api and the remote client context.update({'code' : context.get('code',0) }) return context
def _unrecoverable_state_errback(self, failure: Failure): failure.trap(UnrecoverableState) import datetime # called after the state raises an UnrecoverableState # log the error and shutdown self._logger.error('Simulation has reach an unrecoverable state. ' 'Aborting.') err_log_path = \ Path(f'./errlog_{datetime.datetime.now():%Y%m%d_%H%M%S.%f}.json') with err_log_path.open('w') as fp: print(failure.getErrorMessage(), file=fp) self._logger.error('Details of variables which failed sanity checks ' f'have been output to {err_log_path.resolve()}') self._reactor.stop()
def _errback(self, result: Failure, replyFilt: dict, tupleName: str, sendResponse: SendVortexMsgResponseCallable): logger.error("TupleActionProcessor:%s Failed to process TupleActon", self._tupleActionProcessorName) vortexLogFailure(result, logger) failureMessage = result.getErrorMessage() payloadEnvelope = PayloadEnvelope(filt=replyFilt, result=failureMessage) vortexMsg = yield payloadEnvelope.toVortexMsgDefer() try: yield sendResponse(vortexMsg) except Exception as e: logger.error("Failed to send TupleAction response for %s\n%s", tupleName, failureMessage) logger.exception(e)
def test_render_deferred_failure(self): "Render function w/ failed deferred return." d = defer.Deferred() def render_GET(request): return d phony_call_router = object() self.request.method = "GET" res = urldispatch.URLMatchJSONResource(self.request, url_matches=self.route_map, call_router=phony_call_router) res.render_GET = render_GET ret_res = res.render(self.request) self.assertEqual(NOT_DONE_YET, ret_res) fail = Failure(Exception("test error")) d.errback(fail=fail) self.assertEqual(str(webapi.UnexpectedServerError(self.request, fail.getErrorMessage())), self.request.content.getvalue())
def test_render_deferred_failure(self): "Render function w/ failed deferred return." d = defer.Deferred() def render_GET(request): return d phony_call_router = object() self.request.method = "GET" res = urldispatch.URLMatchJSONResource(self.request, url_matches=self.route_map, call_router=phony_call_router) res.render_GET = render_GET ret_res = res.render(self.request) self.assertEqual(NOT_DONE_YET, ret_res) fail = Failure(Exception("test error")) d.errback(fail=fail) self.assertEqual( str( webapi.UnexpectedServerError(self.request, fail.getErrorMessage())), self.request.content.getvalue())
def errback(self, failure: Failure): """ https://docs.scrapy.org/en/latest/topics/request-response.html https://docs.scrapy.org/en/latest/topics/request-response.html#topics-request-response-ref-errbacks :param failure: :return: """ request = failure.request url = request.url _id = request.meta.get('_id', '') res = { '_id': _id, 'url': url, 'error_type': f"{failure.type}", 'msg': failure.getErrorMessage(), 'traceback': failure.getBriefTraceback(), } try: status = failure.value.response.status res.update({'status': status}) except: pass self.logger.error(json.dumps(res, ensure_ascii=False))
def msgBufResponse(self, result): """Format results, encode, and respond""" response, contentType = result try: return { 'response' : urllib.quote(blaster.Serialize().execute( contentType, drone.formatResults(response))), 'type' : contentType, 'code' : 200 } except: failure = Failure() Msg = { 'code' : 1, 'description' : failure.getErrorMessage(), 'stacktrace': failure.getTraceback(), 'error' : True } return { 'response' : urllib.quote(blaster.Serialize().execute( contentType, Msg)), 'type' : 'text/plain', 'code' : 500 }
columns = dict(zip(columns, aliases)) cl = SQLClient(device=None) if 0: sp = SQLPlugin({'t': (query, {}, cs, columns)}) cl.plugins.append(sp) cl.run() from twisted.internet import reactor reactor.run() results = Failure('ERROR:zen.SQLClient:No data received.') for plugin, result in cl.getResults(): if plugin != sp: continue results = result break sp = None else: try: results = cl.query({'t':(query, {}, cs, columns)}) except Exception, e: results = Failure(e) cl.close() if isinstance(results, Failure): print results.getErrorMessage() sys.exit(1) results = results.get('t', []) if not results: print 'ERROR:zen.SQLClient:No data received.' sys.exit(1) if not columns: columns = dict(zip(results[0].keys(), results[0].keys())) print "|".join(columns.values()) for row in results: print "|".join([str(row.get(dpname,'')) for dpname in columns.values()])
def startInstance(self, label): """Starts an application instance by label @param label: (string) @fires Event('instance-started') return defer.Deferred() """ template = '[%(application)s,%(label)s] %(description)s' context = { 'description': 'Failed to Start', 'code': 254 } result = {} thisInst = None try: if self.model.getInstance(label).running: context.update(self.model.statusInstance(label)) raise DroneCommandFailed(context) d = self._start_stop_common(label, 'startInstance') wfd = defer.waitForDeferred(d) yield wfd result = wfd.getResult() d = self.statusInstance(label) wfd = defer.waitForDeferred(d) yield wfd result.update(wfd.getResult()) #refresh the instance as it can change thisInst = self.model.getInstance(label) if isinstance(result, dict): context.update(result) elif isinstance(result, DroneCommandFailed): context.update(result.resultContext) if thisInst.running: Event('instance-started').fire(instance=thisInst) context['code'] = 0 raise AssertionError('ignore') raise DroneCommandFailed(context) except AssertionError: #update the instance model wfd = defer.waitForDeferred(self.statusInstance(label)) yield wfd result = wfd.getResult() except: thisInst = self.model.getInstance(label) failure = Failure() if failure.check(DroneCommandFailed): template = '%(description)s' context = failure.value.resultContext else: #log the error, allowing for debugging self.debugReport() #be nice and return something to the end user temp = "%s: %s" % (getException(failure), failure.getErrorMessage()) context = {'error': failure, 'code': 253, 'description': temp} result = self.resultContext(template, thisInst, **context) try: thisInst = self.model.getInstance(label) thisInst.shouldBeRunning = True except: pass yield result
def stopInstance(self, label): """Stops an application instance by label @param label: (string) @fires Event('instance-stopped') return defer.Deferred() """ result = {} template = '[%(application)s,%(label)s] %(description)s' context = {'code': 254} thisInst = None try: thisInst = self.model.getInstance(label) thisInst.shouldBeRunning = False if not thisInst.running: context.update(self.model.statusInstance(label)) raise DroneCommandFailed(context) pid = thisInst.process.pid self.log("Trying to shutdown %d gracefully" % (pid,)) def failed(result): """attempting to be consistant""" self.log("Failed to shutdown process gracefully") return result def success(result): """attempting to be consistant""" self.log("process %d gracefully shutdown" % (pid,)) return result d = self._start_stop_common(label, 'stopInstance') d.addCallback(success) d.addErrback(failed) d.addErrback(self._killInstance, thisInst) wfd = defer.waitForDeferred(d) yield wfd #refresh the instance as it can change thisInst = self.model.getInstance(label) result = wfd.getResult() if isinstance(result, dict): context.update(result) elif isinstance(result, DroneCommandFailed): context.update(result.resultContext) if not thisInst.running: context['code'] = 0 Event('instance-stopped').fire(instance=thisInst) raise AssertionError('ignore me') raise DroneCommandFailed(context) except AssertionError: #update the instance model wfd = defer.waitForDeferred(self.statusInstance(label)) yield wfd result = wfd.getResult() result['code'] = context['code'] except: failure = Failure() if failure.check(DroneCommandFailed): context = failure.value.resultContext template = '%(description)s' else: temp = "%s: %s" % (getException(failure), failure.getErrorMessage()) context = {'error': failure, 'code': 253, 'description': temp} result = self.resultContext(template, thisInst, **context) try: thisInst = self.model.getInstance(label) thisInst.shouldBeRunning = False except: pass yield result
class CommandTestMixin(ConnectionTestMixin): """A subclass of `ConnectionTestMixin` that also sets up a command plugin in addition to a connection and transport.""" #: The command plugin class to test. command_class = None #: Any additional help arguments to test in `test_help`. help_arguments = tuple() def setUp(self): super(CommandTestMixin, self).setUp() self.default_venue = self.connection.nickname name = self.command_class.name self.keyword = name.rsplit('/', 1)[-1].rsplit('.', 1)[-1].lower() self.command = self.connection.settings.enable(name, [self.keyword]) self.reply_buffer = iter([]) self.failure = None def command_message(self, content, **kwargs): action = kwargs.pop('action', 'command') kwargs.setdefault('actor', self.other_users[0]) kwargs.setdefault('venue', self.default_venue) kwargs.setdefault('subaction', self.keyword) return Message(self.connection, False, action, content=content, **kwargs) @inlineCallbacks def send_command(self, content, **kwargs): if isinstance(content, unicode): content = content.encode(DEFAULT_ENCODING) request = self.command_message(content, **kwargs) try: response = yield self.command.respond_to(request) except UserVisibleError: self.failure = Failure() else: if response is not None: self.reply_buffer = ReplyBuffer(response, request) def assert_reply(self, expected): finished = maybeDeferred(next, self.reply_buffer, None) finished.addCallback(self.assertEqual, expected) return finished def assert_no_replies(self): finished = maybeDeferred(next, self.reply_buffer, None) finished.addCallback(self.assertIsNone) return finished def assert_error(self, expected): self.assertIsNotNone(self.failure) self.assertIsNotNone(self.failure.check(UserVisibleError)) self.assertEqual(self.failure.getErrorMessage(), expected) self.failure = None @staticmethod def use_cassette(cassette_name): cassette_path = os.path.join(CASSETTE_LIBRARY, cassette_name + '.json') cassette_agent = CassetteAgent(Agent(reactor), cassette_path) def decorator(func): @wraps(func) def wrapper(self, *args, **kwargs): self.command.agent = IdentifyingAgent(ContentDecoderAgent( RedirectAgent(cassette_agent), [('gzip', GzipDecoder)])) finished = maybeDeferred(func, self, *args, **kwargs) finished.addCallback(cassette_agent.save) return finished return wrapper return decorator def test_help(self): """Ensure that command help doesn't cause errors.""" for content in ('',) + self.help_arguments: self.send_command(content, action='cmdhelp')
def connectionLost(self, reason: Failure): Logger.debug("Connection : Disconnecting : " + reason.getErrorMessage()) self.mode = Mode.DISCONNECTED self.interface.dispatch("on_disconnected", reason)
def on_close(self, error: Failure): Logger.info("Game : Closed : " + error.getErrorMessage())
class TestDirtyReactor(unittest.TestCase): """ The trial script has an option to treat L{DirtyReactorAggregateError}s as warnings, as a migration tool for test authors. It causes a wrapper to be placed around reporters that replaces L{DirtyReactorAggregatErrors} with warnings. """ def setUp(self): self.dirtyError = Failure( util.DirtyReactorAggregateError(['foo'], ['bar'])) self.output = StringIO.StringIO() self.test = TestDirtyReactor('test_errorByDefault') def test_errorByDefault(self): """ C{DirtyReactorAggregateError}s are reported as errors with the default Reporter. """ result = reporter.Reporter(stream=self.output) result.addError(self.test, self.dirtyError) self.assertEqual(len(result.errors), 1) self.assertEqual(result.errors[0][1], self.dirtyError) def test_warningsEnabled(self): """ C{DirtyReactorErrors}s are reported as warnings when using the L{UncleanWarningsReporterWrapper}. """ result = UncleanWarningsReporterWrapper( reporter.Reporter(stream=self.output)) self.assertWarns(UserWarning, self.dirtyError.getErrorMessage(), reporter.__file__, result.addError, self.test, self.dirtyError) def test_warningsMaskErrors(self): """ C{DirtyReactorErrors}s are I{not} reported as errors if the L{UncleanWarningsReporterWrapper} is used. """ result = UncleanWarningsReporterWrapper( reporter.Reporter(stream=self.output)) self.assertWarns(UserWarning, self.dirtyError.getErrorMessage(), reporter.__file__, result.addError, self.test, self.dirtyError) self.assertEquals(result._originalReporter.errors, []) def test_dealsWithThreeTuples(self): """ Some annoying stuff can pass three-tuples to addError instead of Failures (like PyUnit). The wrapper, of course, handles this case, since it is a part of L{twisted.trial.itrial.IReporter}! But it does not convert L{DirtyReactorError} to warnings in this case, because nobody should be passing those in the form of three-tuples. """ result = UncleanWarningsReporterWrapper( reporter.Reporter(stream=self.output)) result.addError(self.test, (self.dirtyError.type, self.dirtyError.value, None)) self.assertEqual(len(result._originalReporter.errors), 1) self.assertEquals(result._originalReporter.errors[0][1].type, self.dirtyError.type) self.assertEquals(result._originalReporter.errors[0][1].value, self.dirtyError.value)
def ioError(self, reason: Failure) -> None: Logger.error("Main : " + reason.getErrorMessage()) ErrorPopup(type(reason.value).__name__, reason.getErrorMessage()).open() self.connection = None
async def _shutdown_and_purge_room( self, delete_id: str, room_id: str, requester_user_id: str, new_room_user_id: Optional[str] = None, new_room_name: Optional[str] = None, message: Optional[str] = None, block: bool = False, purge: bool = True, force_purge: bool = False, ) -> None: """ Shuts down and purges a room. See `RoomShutdownHandler.shutdown_room` for details of creation of the new room Args: delete_id: The ID for this delete. room_id: The ID of the room to shut down. requester_user_id: User who requested the action. Will be recorded as putting the room on the blocking list. new_room_user_id: If set, a new room will be created with this user ID as the creator and admin, and all users in the old room will be moved into that room. If not set, no new room will be created and the users will just be removed from the old room. new_room_name: A string representing the name of the room that new users will be invited to. Defaults to `Content Violation Notification` message: A string containing the first message that will be sent as `new_room_user_id` in the new room. Ideally this will clearly convey why the original room was shut down. Defaults to `Sharing illegal content on this server is not permitted and rooms in violation will be blocked.` block: If set to `true`, this room will be added to a blocking list, preventing future attempts to join the room. Defaults to `false`. purge: If set to `true`, purge the given room from the database. force_purge: If set to `true`, the room will be purged from database also if it fails to remove some users from room. Saves a `RoomShutdownHandler.ShutdownRoomResponse` in `DeleteStatus`: """ self._purges_in_progress_by_room.add(room_id) try: with await self.pagination_lock.write(room_id): self._delete_by_id[ delete_id].status = DeleteStatus.STATUS_SHUTTING_DOWN self._delete_by_id[ delete_id].shutdown_room = await self._room_shutdown_handler.shutdown_room( room_id=room_id, requester_user_id=requester_user_id, new_room_user_id=new_room_user_id, new_room_name=new_room_name, message=message, block=block, ) self._delete_by_id[ delete_id].status = DeleteStatus.STATUS_PURGING if purge: logger.info("starting purge room_id %s", room_id) # first check that we have no users in this room if not force_purge: joined = await self.store.is_host_joined( room_id, self._server_name) if joined: raise SynapseError( 400, "Users are still joined to this room") await self.storage.purge_events.purge_room(room_id) logger.info("complete") self._delete_by_id[delete_id].status = DeleteStatus.STATUS_COMPLETE except Exception: f = Failure() logger.error( "failed", exc_info=(f.type, f.value, f.getTracebackObject()), # type: ignore ) self._delete_by_id[delete_id].status = DeleteStatus.STATUS_FAILED self._delete_by_id[delete_id].error = f.getErrorMessage() finally: self._purges_in_progress_by_room.discard(room_id) # remove the delete from the list 24 hours after it completes def clear_delete() -> None: del self._delete_by_id[delete_id] self._delete_by_room[room_id].remove(delete_id) if not self._delete_by_room[room_id]: del self._delete_by_room[room_id] self.hs.get_reactor().callLater( PaginationHandler.CLEAR_PURGE_AFTER_MS / 1000, clear_delete)
except RuntimeError: pass #tried to set an invalid signal #prevent this from being called again self.suppress_signals = lambda: None ############################################################################### # Parse options and get ready to run ############################################################################### try: drone = Daemon() except SystemExit: raise except: from twisted.python.failure import Failure from kitt.util import getException failure = Failure() #use sys.stderr b/c we may have failed to create the class sys.stderr.write('%s: %s\n' % (sys.argv[0], failure.getErrorMessage())) sys.stderr.write('%s: Try --help for usage details.\n' % (sys.argv[0])) sys.exit(1) drone() #lets daemonize (assuming !nodaemon) and get going drone.reactor.callWhenRunning(drone.log, "DroneD reactor is now running.") import droned.logging if drone.DAEMONIZED: droned.logging.logToDir(drone.LOG_DIR) sys.stdout = droned.logging.StdioKabob(0) sys.stderr = droned.logging.StdioKabob(1) else: droned.logging.logToStdout(timestamp=drone.DEBUG) drone.log('logging subsystem initialized') from twisted.application import service
def _do_consume_connection_queue(q, def_done, retries_left, backoff): """Processes the query queue.""" conn = None try: while True: result = yield q.get() if result is None: logger.debug("queue processor stopping") q.put(None) # tell other consumers to stop def_done.callback(None) break seeder, func, xargs = result # else a real item to process. if conn is None or conn.tags is None: if conn is not None: logger.warn("unexpected IMAP connection failure - reconnecting") # getting the initial connection has its own retry semantics try: conn = yield get_connection(self, conductor) assert conn.tags is not None, "got disconnected connection??" except: # can't get a connection - must re-add the item to the queue # then re-throw the error back out so this queue stops. if seeder: q.put(None) # give up and let all consumers stop. else: q.put(result) # retry - if we wind up failing later, big deal... raise try: args = (conn,) + xargs _ = yield func(*args) # if we got this far we successfully processed an item - report that. self.reportStatus(brat.EVERYTHING, brat.GOOD) # It is possible the server disconnected *after* sending a # response - handle it here so we get a more specific log message. if conn.tags is None: logger.warn("unexpected connection failure after calling %r", func) logger.log(1, "arguments were %s", xargs) self.reportStatus(brat.SERVER, brat.BAD) conn = None except imap4.IMAP4Exception, exc: # put the item back in the queue for later or for another successful # connection. q.put(result) retries_left -= 1 if retries_left <= 0: # We are going to give up on this entire connection... if seeder: # If this is the queue seeder, we must post a stop request. q.put(None) raise fail = Failure() status = failure_to_status(fail) self.reportStatus(**status) logger.warning( "Failed to process queue, will retry after %s secs: %s", backoff, fail.getErrorMessage() ) next_backoff = min(backoff * 2, MAX_BACKOFF) # magic number conductor.reactor.callLater( backoff, consume_connection_queue, q, def_done, retries_left, next_backoff ) return except Exception: if not conductor.reactor.running: break # some other bizarre error - just skip this batch and continue self.reportStatus(**failure_to_status(Failure())) log_exception("failed to process an IMAP query request") # This was the queue seeding request; looping again to fetch a # queue item is likely to hang (as nothing else is in there) # There is no point doing a retry; this doesn't seem to be # connection or server related; so post a stop request and bail. if seeder: q.put(None) raise
class beta_blogger(rend.Page): """ Handles communicating with the Blogger beta service (google) """ def __init__(self, username, app, log): rend.Page.__init__(self) self.username = username self.app = app self.log = log self.error = None self.perm_token = "" def locateChild(self, ctx, segments): if len(segments) > 0 and segments[0]: if segments[0] == "auth": def handle_user_id(result): if result[0] == 0: d2 = self.app.api.publish.get_user_exports(result[1]) d2.addCallback(self.handle_export_list, ctx) return d2 ## Step1. See if this user already has a google/blogger token ## on one of their existing blogs. d = self.app.api.users.get_user_id(self.username) d.addCallback(handle_user_id) return d elif segments[0] == "auth-response": ## Step 2. Google gave us a temp token. request = inevow.IRequest(ctx) if request.args.has_key('token'): self.temp_token = request.args['token'][0] ## ## Now, go get a perm one return self.get_perm_token() else: self.error = Failure(Exception("User didn't authenticate successfully")) return self, [] return self, [] def handle_export_list(self, result, ctx): if result[0] == 0: for export in result[1]: if export['service_id'] == 2: ## ## They already have a google/blogger token ## self.perm_token = export['password'] return self, [] ## ## If we're here, they didn't have a token, or something went wrong. Just get one. ## request = inevow.IRequest(ctx) next = "http://www.%s/publish/beta-blogger/auth-response/?user=%s" % (aztk_config.setup.get('site', "domain"), self.username) scope = "http://www.blogger.com/feeds" api_url = url.URL(scheme="https", netloc="www.google.com", pathsegs=["accounts", "AuthSubRequest"], querysegs=[("next", next), ("scope", scope), ('secure', "0"), ('session', "1")]) return api_url, [] def get_perm_token(self): token_getter = beta_blogger_token_getter(self.temp_token, self.log) d = token_getter.get_token() d.addCallback(self.handle_perm_token) d.addErrback(self.handle_failure) return d def handle_perm_token(self, token): self.error = None self.perm_token = token return self, [] def handle_failure(self, error): self.error = error return self, [] def renderHTTP(self, ctx): auth_successful = 1 if self.error: auth_successful = 0 return self.error.getErrorMessage() doc = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Thanks</title> <script type="text/javascript" src="/js/3.05/third_party/MochiKit/MochiKit.js"></script> <script type="text/javascript"> var blog_token = "%s"; var auth_successful = %s; function closeWindow() { window.opener=top; window.opener.child_window_closed(auth_successful, [blog_token]); window.self.close(); } </script> </head> <body onload="javascript:closeWindow();"> </body> </html> """ % (self.perm_token, auth_successful) return doc
def _logMailSendFailure(failure: Failure) -> Failure: logging.error('Notification sending failed: %s', failure.getErrorMessage()) return failure
sp = SQLPlugin({'t': (query, {}, cs, columns)}) cl.plugins.append(sp) cl.run() from twisted.internet import reactor reactor.run() results = Failure('ERROR:zen.SQLClient:No data received.') for plugin, result in cl.getResults(): if plugin != sp: continue results = result break sp = None else: try: results = cl.query({'t': (query, {}, cs, columns)}) except Exception, e: results = Failure(e) cl.close() if isinstance(results, Failure): print results.getErrorMessage() sys.exit(1) results = results.get('t', []) if not results: print 'ERROR:zen.SQLClient:No data received.' sys.exit(1) if not columns: columns = dict(zip(results[0].keys(), results[0].keys())) print "|".join(columns.values()) for row in results: print "|".join( [str(row.get(dpname, '')) for dpname in columns.values()])