def process(self): """ Processes the request, analyzing the parameters, and feeding them to the _getAnswer() method (implemented by derived classes) """ ContextManager.set('currentRH', self) self._checkParams() self._checkProtection() if self.CHECK_HTML: try: security.Sanitization.sanitizationCheck(self._target, self._params, self._aw, ['requestInfo']) except HtmlForbiddenTag as e: raise HTMLSecurityError('ERR-X0', 'HTML Security problem. {}'.format(e)) if self._doProcess: if Config.getInstance().getProfile(): import profile, pstats, random proffilename = os.path.join(Config.getInstance().getTempDir(), "service%s.prof" % random.random()) result = [None] profile.runctx("result[0] = self._getAnswer()", globals(), locals(), proffilename) answer = result[0] rep = Config.getInstance().getTempDir() stats = pstats.Stats(proffilename) stats.strip_dirs() stats.sort_stats('cumulative', 'time', 'calls') stats.dump_stats(os.path.join(rep, "IndicoServiceRequestProfile.log")) os.remove(proffilename) else: answer = self._getAnswer() self._deleteTempFiles() return answer
def testDeleteWithDel(self): """ Delete works OK """ ContextManager.set('test', 123) del ContextManager.get()['test'] self.assertEqual(ContextManager.get('test', default=None), None)
def testDelete(self): """ Delete works OK """ ContextManager.set('test', 123) ContextManager.delete('test') self.assertEqual(ContextManager.get('test', default=None), None)
def _connect(self): """Connect to the CacheClient. This method must be called before accessing ``self._client``. """ # Maybe we already have a client in this instance if self._client is not None: return # If not, we might have one from another instance self._client = ContextManager.get('GenericCacheClient', None) if self._client is not None: return # If not, create a new one backend = Config.getInstance().getCacheBackend() if backend == 'memcached': self._client = MemcachedCacheClient( Config.getInstance().getMemcachedServers()) elif backend == 'redis': self._client = RedisCacheClient( Config.getInstance().getRedisCacheURL()) elif backend == 'files': self._client = FileCacheClient( Config.getInstance().getXMLCacheDir()) else: self._client = NullCacheClient() ContextManager.set('GenericCacheClient', self._client)
def process(self): """ Processes the request, analyzing the parameters, and feeding them to the _getAnswer() method (implemented by derived classes) """ ContextManager.set('currentRH', self) self._checkParams() self._checkProtection() if self.CHECK_HTML: try: security.Sanitization.sanitizationCheck(self._target, self._params, self._aw, ['requestInfo']) except HtmlForbiddenTag as e: raise HTMLSecurityError('ERR-X0', 'HTML Security problem. {}'.format(e)) if self._doProcess: if Config.getInstance().getProfile(): import profile, pstats, random proffilename = os.path.join(Config.getInstance().getTempDir(), "service%s.prof" % random.random()) result = [None] profile.runctx("result[0] = self._getAnswer()", globals(), locals(), proffilename) answer = result[0] rep = Config.getInstance().getTempDir() stats = pstats.Stats(proffilename) stats.strip_dirs() stats.sort_stats('cumulative', 'time', 'calls') stats.dump_stats(os.path.join(rep, "IndicoServiceRequestProfile.log")) os.remove(proffilename) else: answer = self._getAnswer() self._deleteTempFiles() return answer
def _setParam(self): ContextManager.set("dateChangeNotificationProblems", {}) if self._pTime < self._target.getStartDate(): raise ServiceError("ERR-E3", "Date/time of end cannot " + "be lower than data/time of start") self._target.setDates(self._target.getStartDate(), self._pTime.astimezone(timezone("UTC")), moveEntries=0) dateChangeNotificationProblems = ContextManager.get("dateChangeNotificationProblems") if dateChangeNotificationProblems: warningContent = [] for problemGroup in dateChangeNotificationProblems.itervalues(): warningContent.extend(problemGroup) return Warning( _("Warning"), [ _("The end date of your event was changed correctly."), _("However, there were the following problems:"), warningContent, ], ) else: return None
def _connect(self): """Connect to the CacheClient. This method must be called before accessing ``self._client``. """ # Maybe we already have a client in this instance if self._client is not None: return # If not, we might have one from another instance self._client = ContextManager.get('GenericCacheClient', None) if self._client is not None: return # If not, create a new one backend = Config.getInstance().getCacheBackend() if backend == 'memcached': self._client = MemcachedCacheClient(Config.getInstance().getMemcachedServers()) elif backend == 'redis': self._client = RedisCacheClient(Config.getInstance().getRedisCacheURL()) elif backend == 'files': self._client = FileCacheClient(Config.getInstance().getXMLCacheDir()) else: self._client = NullCacheClient() ContextManager.set('GenericCacheClient', self._client)
def testDeleteWithDel(self): """ Delete works OK """ ContextManager.set('test', 123) del ContextManager.get()['test'] self.assertEqual(ContextManager.get('test', default=None), None)
def testDelete(self): """ Delete works OK """ ContextManager.set('test', 123) ContextManager.delete('test') self.assertEqual(ContextManager.get('test', default=None), None)
def _setParam(self): ContextManager.set('dateChangeNotificationProblems', {}) if (self._pTime < self._target.getStartDate()): raise ServiceError( "ERR-E3", "Date/time of end cannot " + "be lower than data/time of start") self._target.setDates(self._target.getStartDate(), self._pTime.astimezone(timezone("UTC")), moveEntries=0) dateChangeNotificationProblems = ContextManager.get( 'dateChangeNotificationProblems') if dateChangeNotificationProblems: warningContent = [] for problemGroup in dateChangeNotificationProblems.itervalues(): warningContent.extend(problemGroup) return Warning(_('Warning'), [ _('The end date of your event was changed correctly.'), _('However, there were the following problems:'), warningContent ]) else: return None
def _connect(self): # Maybe we already have a client in this instance if self._client is not None: return # If not, we might have one from another instance self._client = ContextManager.get('GenericCacheClient', None) if self._client is not None: return # If not, create a new one backend = Config.getInstance().getCacheBackend() if backend == 'memcached': import memcache self._client = memcache.Client( Config.getInstance().getMemcachedServers()) elif backend == 'redis': self._client = RedisCacheClient( Config.getInstance().getRedisCacheURL()) elif backend == 'files': self._client = FileCacheClient( Config.getInstance().getXMLCacheDir()) else: self._client = NullCacheClient() ContextManager.set('GenericCacheClient', self._client)
def _getAnswer(self): ContextManager.set("dateChangeNotificationProblems", {}) if self._startDate > self._endDate: raise ServiceError("ERR-E3", "Date/time of start cannot " + "be greater than date/time of end") try: self._target.setDates(self._startDate, self._endDate, moveEntries=1) except TimingError, e: raise ServiceError("ERR-E2", e.getMsg())
def _handleSet(self): # Account for possible retries (due to conflicts) # If 'minutesFileId' is defined, there was a previous try fileId = ContextManager.setdefault('minutesFileId', None) minutes = self._target.getMinutes() if not minutes: minutes = self._target.createMinutes() minutes.setText(self._text, forcedFileId=fileId) # save the fileId, in case there is a conflict error # in the worst case scenario the file will be rewritten multiple times res = minutes.getResourceById('minutes') ContextManager.set('minutesFileId', res.getRepositoryId())
def _getAnswer(self): ContextManager.set('dateChangeNotificationProblems', {}) if (self._startDate > self._endDate): raise ServiceError( "ERR-E3", "Date/time of start cannot " + "be greater than date/time of end") try: self._target.setDates(self._startDate, self._endDate, moveEntries=1) except TimingError, e: raise ServiceError("ERR-E2", e.getMsg())
def _handleSet(self): # Account for possible retries (due to conflicts) # If 'minutesFileId' is defined, there was a previous try fileId = ContextManager.setdefault("minutesFileId", None) minutes = self._target.getMinutes() if not minutes: minutes = self._target.createMinutes() minutes.setText(self._text, forcedFileId=fileId) # save the fileId, in case there is a conflict error # in the worst case scenario the file will be rewritten multiple times res = minutes.getResourceById("minutes") ContextManager.set("minutesFileId", res.getRepositoryId())
def _connect(self): # Maybe we already have a client in this instance if self._client is not None: return # If not, we might have one from another instance self._client = ContextManager.get('GenericCacheClient', None) if self._client is not None: return # If not, create a new one backend = Config.getInstance().getCacheBackend() if backend == 'memcached': import memcache self._client = memcache.Client(Config.getInstance().getMemcachedServers()) else: self._client = FileCacheClient(Config.getInstance().getXMLCacheDir()) ContextManager.set('GenericCacheClient', self._client)
def _getAnswer(self): ContextManager.set("dateChangeNotificationProblems", {}) if self._shiftTimes: moveEntries = 1 else: moveEntries = 0 # first sanity check if self._startDate > self._endDate: raise ServiceError("ERR-E3", "Date/time of start cannot " "be greater than date/time of end") # catch TimingErrors that can be returned by the algorithm try: with track_time_changes(): self._target.setDates(self._startDate, self._endDate, moveEntries=moveEntries) except TimingError, e: raise TimingNoReportError(e.getMessage(), title=_("Cannot set event dates"), explanation=e.getExplanation())
def process(self): """ Processes the request, analyzing the parameters, and feeding them to the _getAnswer() method (implemented by derived classes) """ ContextManager.set('currentRH', self) self._setLang() self._checkParams() self._checkProtection() try: security.Sanitization.sanitizationCheck(self._target, self._params, self._aw) except (HtmlScriptError, HtmlForbiddenTag), e: raise HTMLSecurityError('ERR-X0', 'HTML Security problem. %s ' % str(e))
def process(self): """ Processes the request, analyzing the parameters, and feeding them to the _getAnswer() method (implemented by derived classes) """ ContextManager.set('currentRH', self) self._setLang() self._checkParams() self._checkProtection() try: security.Sanitization.sanitizationCheck(self._target, self._params, self._aw) except (HtmlScriptError, HtmlForbiddenTag), e: raise HTMLSecurityError('ERR-X0','HTML Security problem. %s ' % str(e))
def _getAnswer(self): ContextManager.set('dateChangeNotificationProblems', {}) if (self._shiftTimes): moveEntries = 1 else: moveEntries = 0 # first sanity check if (self._startDate > self._endDate): raise ServiceError("ERR-E3", "Date/time of start cannot " "be greater than date/time of end") # catch TimingErrors that can be returned by the algorithm try: with track_time_changes(): self._target.setDates(self._startDate, self._endDate, moveEntries=moveEntries) except TimingError, e: raise TimingNoReportError(e.getMessage(), title=_("Cannot set event dates"), explanation=e.getExplanation())
def setUp( self ): # create a context, for storing autoOps ContextManager.create() ContextManager.set('autoOps', []) a = Avatar() a.setId("creator") self._conf = ConferenceFacade( a ) self._conf.setId('a') self._conf.setTimezone('UTC') self._conf.setDates(datetime(2009, 9, 21, 16, 0 ,0, tzinfo=timezone("UTC")), datetime(2009, 9, 21, 19, 0 ,0, tzinfo=timezone("UTC"))) self._slot1_sDate = datetime(2009, 9, 21, 17, 0, 0, tzinfo=timezone("UTC")) self._slot1_eDate = datetime(2009, 9, 21, 18, 0, 0, tzinfo=timezone("UTC")) self._slot2_sDate = datetime(2009, 9, 21, 18, 0, 0, tzinfo=timezone("UTC")) self._slot2_eDate = datetime(2009, 9, 21, 19, 0, 0, tzinfo=timezone("UTC")) self._slot2_laterDate = datetime(2009, 9, 21, 20, 0, 0, tzinfo=timezone("UTC")) self._session1 = Session() self._session1.setValues({ 'sDate': self._slot1_sDate, 'eDate': self._slot1_eDate }) self._conf.addSession(self._session1) self._slot1 = self._session1.getSlotById(0) self._slot2 = SessionSlot(self._session1) self._slot2.setValues({ 'sDate': self._slot2_sDate, 'eDate': self._slot2_eDate }); self._session1.addSlot(self._slot2)
def testGetWorks(self): ContextManager.set('test', 123) self.assertEquals(ContextManager.get('test'), 123)
def testSetSilent(self): self.assertEquals(ContextManager.set('test','foo').__class__, ContextManager.DummyContext)
def testGetWorks(self): "Getting the value of an existing attribute" ContextManager.set('test', 123) self.assertEquals(ContextManager.get('test'), 123)
def testSetSilent(self): self.assertEquals( ContextManager.set('test', 'foo').__class__, ContextManager.DummyContext)
def process(self, params): if request.method not in self.HTTP_VERBS: # Just to be sure that we don't get some crappy http verb we don't expect raise BadRequest cfg = Config.getInstance() forced_conflicts, max_retries, profile = cfg.getForceConflicts( ), cfg.getMaxRetries(), cfg.getProfile() profile_name, res, textLog = '', '', [] self._startTime = datetime.now() # clear the context ContextManager.destroy() ContextManager.set('currentRH', self) g.rh = self #redirect to https if necessary if self._checkHttpsRedirect(): return self._responseUtil.make_redirect() DBMgr.getInstance().startRequest() textLog.append("%s : Database request started" % (datetime.now() - self._startTime)) Logger.get('requestHandler').info('[pid=%s] Request %s started' % (os.getpid(), request)) try: for i, retry in enumerate(transaction.attempts(max_retries)): with retry: if i > 0: signals.before_retry.send() try: Logger.get('requestHandler').info( '\t[pid=%s] from host %s' % (os.getpid(), request.remote_addr)) profile_name, res = self._process_retry( params, i, profile, forced_conflicts) signals.after_process.send() if i < forced_conflicts: # raise conflict error if enabled to easily handle conflict error case raise ConflictError transaction.commit() DBMgr.getInstance().endRequest(commit=False) break except (ConflictError, POSKeyError): transaction.abort() import traceback # only log conflict if it wasn't forced if i >= forced_conflicts: Logger.get('requestHandler').warning( 'Conflict in Database! (Request %s)\n%s' % (request, traceback.format_exc())) except ClientDisconnected: transaction.abort() Logger.get('requestHandler').warning( 'Client Disconnected! (Request {})'.format( request)) time.sleep(i) self._process_success() except Exception as e: transaction.abort() res = self._getMethodByExceptionName(e)(e) totalTime = (datetime.now() - self._startTime) textLog.append('{} : Request ended'.format(totalTime)) # log request timing if profile and totalTime > timedelta( 0, 1) and os.path.isfile(profile_name): rep = Config.getInstance().getTempDir() stats = pstats.Stats(profile_name) stats.strip_dirs() stats.sort_stats('cumulative', 'time', 'calls') stats.dump_stats(os.path.join(rep, 'IndicoRequestProfile.log')) output = StringIO.StringIO() sys.stdout = output stats.print_stats(100) sys.stdout = sys.__stdout__ s = output.getvalue() f = file(os.path.join(rep, 'IndicoRequest.log'), 'a+') f.write('--------------------------------\n') f.write('URL : {}\n'.format(request.url)) f.write('{} : start request\n'.format(self._startTime)) f.write('params:{}'.format(params)) f.write('\n'.join(textLog)) f.write('\n') f.write('retried : {}\n'.format(10 - retry)) f.write(s) f.write('--------------------------------\n\n') f.close() if profile and profile_name and os.path.exists(profile_name): os.remove(profile_name) if self._responseUtil.call: return self._responseUtil.make_call() # In case of no process needed, we should return empty string to avoid erroneous output # specially with getVars breaking the JS files. if not self._doProcess or res is None: return self._responseUtil.make_empty() return self._responseUtil.make_response(res)
def testGetItemWorks(self): "Getting the value of an existing attribute (__getitem__)" ContextManager.set('test', 123) self.assertEquals(ContextManager.get()['test'], 123)
def testGetWorks(self): "Getting the value of an existing attribute" ContextManager.set('test', 123) self.assertEquals(ContextManager.get('test'), 123)
def testDoubleSetWorks(self): "Two consecutive set() operations over the same attribute" ContextManager.set('test2', 65) ContextManager.set('test2', 66) self.assertEquals(ContextManager.get('test2'), 66)
def requestStarted(self, obj, req): # reset the context manager ContextManager.set('indico.ext.livesync:actions', {}) ContextManager.set('indico.ext.livesync:ids', {})
def setUser( self, newAvatar ): self._currentUser = newAvatar ContextManager.set('currentUser', self._currentUser)
def process(self, params): if request.method not in self.HTTP_VERBS: # Just to be sure that we don't get some crappy http verb we don't expect raise BadRequest cfg = Config.getInstance() forced_conflicts, max_retries, profile = cfg.getForceConflicts(), cfg.getMaxRetries(), cfg.getProfile() profile_name, res, textLog = '', '', [] self._startTime = datetime.now() # clear the context ContextManager.destroy() ContextManager.set('currentRH', self) g.rh = self #redirect to https if necessary if self._checkHttpsRedirect(): return self._responseUtil.make_redirect() DBMgr.getInstance().startRequest() textLog.append("%s : Database request started" % (datetime.now() - self._startTime)) Logger.get('requestHandler').info('[pid=%s] Request %s started' % ( os.getpid(), request)) try: for i, retry in enumerate(transaction.attempts(max_retries)): with retry: if i > 0: signals.before_retry.send() try: Logger.get('requestHandler').info('\t[pid=%s] from host %s' % (os.getpid(), request.remote_addr)) profile_name, res = self._process_retry(params, i, profile, forced_conflicts) signals.after_process.send() if i < forced_conflicts: # raise conflict error if enabled to easily handle conflict error case raise ConflictError transaction.commit() DBMgr.getInstance().endRequest(commit=False) break except (ConflictError, POSKeyError): transaction.abort() import traceback # only log conflict if it wasn't forced if i >= forced_conflicts: Logger.get('requestHandler').warning('Conflict in Database! (Request %s)\n%s' % (request, traceback.format_exc())) except ClientDisconnected: transaction.abort() Logger.get('requestHandler').warning('Client Disconnected! (Request {})'.format(request)) time.sleep(i) self._process_success() except Exception as e: transaction.abort() res = self._getMethodByExceptionName(e)(e) totalTime = (datetime.now() - self._startTime) textLog.append('{} : Request ended'.format(totalTime)) # log request timing if profile and totalTime > timedelta(0, 1) and os.path.isfile(profile_name): rep = Config.getInstance().getTempDir() stats = pstats.Stats(profile_name) stats.strip_dirs() stats.sort_stats('cumulative', 'time', 'calls') stats.dump_stats(os.path.join(rep, 'IndicoRequestProfile.log')) output = StringIO.StringIO() sys.stdout = output stats.print_stats(100) sys.stdout = sys.__stdout__ s = output.getvalue() f = file(os.path.join(rep, 'IndicoRequest.log'), 'a+') f.write('--------------------------------\n') f.write('URL : {}\n'.format(request.url)) f.write('{} : start request\n'.format(self._startTime)) f.write('params:{}'.format(params)) f.write('\n'.join(textLog)) f.write('\n') f.write('retried : {}\n'.format(10-retry)) f.write(s) f.write('--------------------------------\n\n') f.close() if profile and profile_name and os.path.exists(profile_name): os.remove(profile_name) if self._responseUtil.call: return self._responseUtil.make_call() # In case of no process needed, we should return empty string to avoid erroneous output # specially with getVars breaking the JS files. if not self._doProcess or res is None: return self._responseUtil.make_empty() return self._responseUtil.make_response(res)
def process(self, params): if request.method not in HTTP_VERBS: # Just to be sure that we don't get some crappy http verb we don't expect raise BadRequest cfg = Config.getInstance() forced_conflicts, max_retries, profile = cfg.getForceConflicts(), cfg.getMaxRetries(), cfg.getProfile() profile_name, res, textLog = '', '', [] self._startTime = datetime.now() # clear the context ContextManager.destroy() ContextManager.set('currentRH', self) g.rh = self #redirect to https if necessary if self._checkHttpsRedirect(): return self._responseUtil.make_redirect() if self.EVENT_FEATURE is not None: self._check_event_feature() DBMgr.getInstance().startRequest() textLog.append("%s : Database request started" % (datetime.now() - self._startTime)) Logger.get('requestHandler').info(u'Request started: %s %s [IP=%s] [PID=%s]', request.method, request.relative_url, request.remote_addr, os.getpid()) is_error_response = False try: for i, retry in enumerate(transaction.attempts(max_retries)): with retry: if i > 0: signals.before_retry.send() try: profile_name, res = self._process_retry(params, i, profile, forced_conflicts) signals.after_process.send() if i < forced_conflicts: # raise conflict error if enabled to easily handle conflict error case raise ConflictError if self.commit: transaction.commit() else: transaction.abort() DBMgr.getInstance().endRequest(commit=False) break except (ConflictError, POSKeyError): transaction.abort() import traceback # only log conflict if it wasn't forced if i >= forced_conflicts: Logger.get('requestHandler').warning('Database conflict') except ClientDisconnected: transaction.abort() Logger.get('requestHandler').warning('Client disconnected') time.sleep(i) except DatabaseError: handle_sqlalchemy_database_error() break self._process_success() except Exception as e: transaction.abort() res = self._getMethodByExceptionName(e)(e) if isinstance(e, HTTPException) and e.response is not None: res = e.response is_error_response = True totalTime = (datetime.now() - self._startTime) textLog.append('{} : Request ended'.format(totalTime)) # log request timing if profile and os.path.isfile(profile_name): rep = Config.getInstance().getTempDir() stats = pstats.Stats(profile_name) stats.sort_stats('cumulative', 'time', 'calls') stats.dump_stats(os.path.join(rep, 'IndicoRequestProfile.log')) output = StringIO.StringIO() sys.stdout = output stats.print_stats(100) sys.stdout = sys.__stdout__ s = output.getvalue() f = file(os.path.join(rep, 'IndicoRequest.log'), 'a+') f.write('--------------------------------\n') f.write('URL : {}\n'.format(request.url)) f.write('{} : start request\n'.format(self._startTime)) f.write('params:{}'.format(params)) f.write('\n'.join(textLog)) f.write(s) f.write('--------------------------------\n\n') f.close() if profile and profile_name and os.path.exists(profile_name): os.remove(profile_name) if self._responseUtil.call: return self._responseUtil.make_call() if is_error_response and isinstance(res, (current_app.response_class, Response)): # if we went through error handling code, responseUtil._status has been changed # so make_response() would fail return res # In case of no process needed, we should return empty string to avoid erroneous output # specially with getVars breaking the JS files. if not self._doProcess or res is None: return self._responseUtil.make_empty() return self._responseUtil.make_response(res)
def thread1(self): ContextManager.set('samevariable', 'a') time.sleep(2) self.assertEquals(ContextManager.get('samevariable'), 'a')
def requestRetry(self, obj, req, nretry): # reset the context manager ContextManager.set('indico.ext.livesync:actions', {}) ContextManager.set('indico.ext.livesync:ids', {})
def thread2(self): time.sleep(1) ContextManager.set('samevariable', 'b') self.assertEquals(ContextManager.get('samevariable'), 'b')
def requestStarted(self, obj, req): # reset the context manager ContextManager.set('indico.ext.livesync:actions', {}) ContextManager.set('indico.ext.livesync:ids', {})
def process(self, params): """ """ profile = Config.getInstance().getProfile() proffilename = "" res = "" MAX_RETRIES = 10 retry = MAX_RETRIES textLog = [] self._startTime = datetime.now() # clear the context ContextManager.destroy() ContextManager.set('currentRH', self) #redirect to https if necessary if self._checkHttpsRedirect(): return DBMgr.getInstance().startRequest() self._startRequestSpecific2RH( ) # I.e. implemented by Room Booking request handlers textLog.append("%s : Database request started" % (datetime.now() - self._startTime)) Logger.get('requestHandler').info( '[pid=%s] Request %s started (%s)' % (os.getpid(), id(self._req), self._req.unparsed_uri)) # notify components that the request has started self._notify('requestStarted', self._req) forcedConflicts = Config.getInstance().getForceConflicts() try: while retry > 0: if retry < MAX_RETRIES: # notify components that the request is being retried self._notify('requestRetry', self._req, MAX_RETRIES - retry) try: Logger.get('requestHandler').info( '\t[pid=%s] from host %s' % (os.getpid(), self.getHostIP())) try: # clear the fossile cache at the start of each request fossilize.clearCache() # delete all queued emails GenericMailer.flushQueue(False) DBMgr.getInstance().sync() # keep a link to the web session in the access wrapper # this is used for checking access/modification key existence # in the user session self._aw.setIP(self.getHostIP()) self._aw.setSession(self._getSession()) #raise(str(dir(self._websession))) self._setSessionUser() self._setLang(params) if self._getAuth(): if self._getUser(): Logger.get('requestHandler').info( 'Request %s identified with user %s (%s)' % (id(self._req), self._getUser().getFullName(), self._getUser().getId())) if not self._tohttps and Config.getInstance( ).getAuthenticatedEnforceSecure(): self._tohttps = True if self._checkHttpsRedirect(): return #if self._getUser() != None and self._getUser().getId() == "893": # profile = True self._reqParams = copy.copy(params) self._checkParams(self._reqParams) self._checkProtection() security.Sanitization.sanitizationCheck( self._target, self._reqParams, self._aw, self._doNotSanitizeFields) if self._doProcess: if profile: import profile, pstats proffilename = os.path.join( Config.getInstance().getTempDir(), "stone%s.prof" % str(random.random())) result = [None] profile.runctx("result[0] = self._process()", globals(), locals(), proffilename) res = result[0] else: res = self._process() # Save web session, just when needed sm = session.getSessionManager() sm.maintain_session(self._req, self._websession) # notify components that the request has finished self._notify('requestFinished', self._req) # Raise a conflict error if enabled. This allows detecting conflict-related issues easily. if retry > (MAX_RETRIES - forcedConflicts): raise ConflictError self._endRequestSpecific2RH( True ) # I.e. implemented by Room Booking request handlers DBMgr.getInstance().endRequest(True) Logger.get('requestHandler').info( 'Request %s successful' % (id(self._req))) #request succesfull, now, doing tas that must be done only once try: GenericMailer.flushQueue(True) # send emails self._deleteTempFiles() except: Logger.get('mail').exception( 'Mail sending operation failed') pass break except MaKaCError, e: #DBMgr.getInstance().endRequest(False) res = self._processError(e) except (ConflictError, POSKeyError): import traceback # only log conflict if it wasn't forced if retry <= (MAX_RETRIES - forcedConflicts): Logger.get('requestHandler').warning( 'Conflict in Database! (Request %s)\n%s' % (id(self._req), traceback.format_exc())) self._abortSpecific2RH() DBMgr.getInstance().abort() retry -= 1 continue except ClientDisconnected: Logger.get('requestHandler').warning( 'Client Disconnected! (Request %s)' % id(self._req)) self._abortSpecific2RH() DBMgr.getInstance().abort() retry -= 1 time.sleep(10 - retry) continue except KeyAccessError, e: #Key Access error treatment res = self._processKeyAccessError(e) self._endRequestSpecific2RH(False) DBMgr.getInstance().endRequest(False)
def thread2(self): time.sleep(1) ContextManager.set("samevariable", "b") self.assertEquals(ContextManager.get("samevariable"), "b")
def testDoubleSetWorks(self): "Two consecutive set() operations over the same attribute" ContextManager.set('test2', 65) ContextManager.set('test2', 66) self.assertEquals(ContextManager.get('test2'), 66)
def setUp(self): # create a context, for storing autoOps ContextManager.create() ContextManager.set('autoOps', []) a = Avatar() a.setId("creator") self._conf = ConferenceFacade(a) self._conf.setId('a') self._conf.setTimezone('UTC') self._conf.setDates( datetime(2009, 9, 21, 16, 0, 0, tzinfo=timezone("UTC")), datetime(2009, 9, 21, 19, 0, 0, tzinfo=timezone("UTC"))) self._slot1_sDate = datetime(2009, 9, 21, 17, 0, 0, tzinfo=timezone("UTC")) self._slot1_eDate = datetime(2009, 9, 21, 18, 0, 0, tzinfo=timezone("UTC")) self._slot2_sDate = datetime(2009, 9, 21, 18, 0, 0, tzinfo=timezone("UTC")) self._slot2_eDate = datetime(2009, 9, 21, 19, 0, 0, tzinfo=timezone("UTC")) self._slot2_laterDate = datetime(2009, 9, 21, 20, 0, 0, tzinfo=timezone("UTC")) self._session1 = Session() self._session1.setValues({ 'sDate': self._slot1_sDate, 'eDate': self._slot1_eDate }) self._conf.addSession(self._session1) self._slot1 = self._session1.getSlotById(0) self._slot2 = SessionSlot(self._session1) self._slot2.setValues({ 'sDate': self._slot2_sDate, 'eDate': self._slot2_eDate }) self._session1.addSlot(self._slot2)
def testGetWorks(self): ContextManager.set('test', 123) self.assertEquals(ContextManager.get('test'), 123)
def testGetItemWorks(self): "Getting the value of an existing attribute (__getitem__)" ContextManager.set('test', 123) self.assertEquals(ContextManager.get()['test'], 123)
def testDoubleSetWorks(self): ContextManager.set('test2', 65) ContextManager.set('test2', 66) self.assertEquals(ContextManager.get('test2'), 66)
def thread1(self): ContextManager.set('samevariable', 'a') time.sleep(2) self.assertEquals(ContextManager.get('samevariable'), 'a')
def thread2(self): ContextManager.create() time.sleep(1) ContextManager.set('samevariable', 'b') self.assertEquals(ContextManager.get('samevariable'), 'b')
def setUser( self, newAvatar ): self._currentUser = newAvatar ContextManager.set('currentUser', self._currentUser)
def testDoubleSetWorks(self): ContextManager.set('test2', 65) ContextManager.set('test2', 66) self.assertEquals(ContextManager.get('test2'), 66)
def requestRetry(self, obj, req, nretry): # reset the context manager ContextManager.set('indico.ext.livesync:actions', {}) ContextManager.set('indico.ext.livesync:ids', {})
def process(self, params): if request.method not in HTTP_VERBS: # Just to be sure that we don't get some crappy http verb we don't expect raise BadRequest cfg = Config.getInstance() forced_conflicts, max_retries, profile = cfg.getForceConflicts( ), cfg.getMaxRetries(), cfg.getProfile() profile_name, res, textLog = '', '', [] self._startTime = datetime.now() # clear the context ContextManager.destroy() ContextManager.set('currentRH', self) g.rh = self #redirect to https if necessary if self._checkHttpsRedirect(): return self._responseUtil.make_redirect() if self.EVENT_FEATURE is not None: self._check_event_feature() DBMgr.getInstance().startRequest() textLog.append("%s : Database request started" % (datetime.now() - self._startTime)) Logger.get('requestHandler').info( u'Request started: %s %s [IP=%s] [PID=%s]', request.method, request.relative_url, request.remote_addr, os.getpid()) is_error_response = False try: for i, retry in enumerate(transaction.attempts(max_retries)): with retry: if i > 0: signals.before_retry.send() try: profile_name, res = self._process_retry( params, i, profile, forced_conflicts) signals.after_process.send() if i < forced_conflicts: # raise conflict error if enabled to easily handle conflict error case raise ConflictError if self.commit: transaction.commit() else: transaction.abort() DBMgr.getInstance().endRequest(commit=False) break except (ConflictError, POSKeyError): transaction.abort() import traceback # only log conflict if it wasn't forced if i >= forced_conflicts: Logger.get('requestHandler').warning( 'Database conflict') except ClientDisconnected: transaction.abort() Logger.get('requestHandler').warning( 'Client disconnected') time.sleep(i) except DatabaseError: handle_sqlalchemy_database_error() break self._process_success() except Exception as e: transaction.abort() res = self._getMethodByExceptionName(e)(e) if isinstance(e, HTTPException) and e.response is not None: res = e.response is_error_response = True totalTime = (datetime.now() - self._startTime) textLog.append('{} : Request ended'.format(totalTime)) # log request timing if profile and os.path.isfile(profile_name): rep = Config.getInstance().getTempDir() stats = pstats.Stats(profile_name) stats.strip_dirs() stats.sort_stats('cumulative', 'time', 'calls') stats.dump_stats(os.path.join(rep, 'IndicoRequestProfile.log')) output = StringIO.StringIO() sys.stdout = output stats.print_stats(100) sys.stdout = sys.__stdout__ s = output.getvalue() f = file(os.path.join(rep, 'IndicoRequest.log'), 'a+') f.write('--------------------------------\n') f.write('URL : {}\n'.format(request.url)) f.write('{} : start request\n'.format(self._startTime)) f.write('params:{}'.format(params)) f.write('\n'.join(textLog)) f.write(s) f.write('--------------------------------\n\n') f.close() if profile and profile_name and os.path.exists(profile_name): os.remove(profile_name) if self._responseUtil.call: return self._responseUtil.make_call() if is_error_response and isinstance( res, (current_app.response_class, Response)): # if we went through error handling code, responseUtil._status has been changed # so make_response() would fail return res # In case of no process needed, we should return empty string to avoid erroneous output # specially with getVars breaking the JS files. if not self._doProcess or res is None: return self._responseUtil.make_empty() return self._responseUtil.make_response(res)
def process( self, params ): """ """ profile = Config.getInstance().getProfile() proffilename = "" res = "" MAX_RETRIES = 10 retry = MAX_RETRIES textLog = [] self._startTime = datetime.now() # clear the context ContextManager.destroy() ContextManager.set('currentRH', self) #redirect to https if necessary if self._checkHttpsRedirect(): return DBMgr.getInstance().startRequest() self._startRequestSpecific2RH() # I.e. implemented by Room Booking request handlers textLog.append("%s : Database request started"%(datetime.now() - self._startTime)) Logger.get('requestHandler').info('[pid=%s] Request %s started (%s)' % (os.getpid(),id(self._req), self._req.unparsed_uri)) # notify components that the request has started self._notify('requestStarted', self._req) forcedConflicts = Config.getInstance().getForceConflicts() try: while retry>0: if retry < MAX_RETRIES: # notify components that the request is being retried self._notify('requestRetry', self._req, MAX_RETRIES - retry) try: Logger.get('requestHandler').info('\t[pid=%s] from host %s' % (os.getpid(), self.getHostIP())) try: # clear the fossile cache at the start of each request fossilize.clearCache() # delete all queued emails GenericMailer.flushQueue(False) DBMgr.getInstance().sync() # keep a link to the web session in the access wrapper # this is used for checking access/modification key existence # in the user session self._aw.setIP( self.getHostIP() ) self._aw.setSession(self._getSession()) #raise(str(dir(self._websession))) self._setSessionUser() self._setLang(params) if self._getAuth(): if self._getUser(): Logger.get('requestHandler').info('Request %s identified with user %s (%s)' % (id(self._req), self._getUser().getFullName(), self._getUser().getId())) if not self._tohttps and Config.getInstance().getAuthenticatedEnforceSecure(): self._tohttps = True if self._checkHttpsRedirect(): return #if self._getUser() != None and self._getUser().getId() == "893": # profile = True self._reqParams = copy.copy( params ) self._checkParams( self._reqParams ) self._checkProtection() security.Sanitization.sanitizationCheck(self._target, self._reqParams, self._aw, self._doNotSanitizeFields) if self._doProcess: if profile: import profile, pstats proffilename = os.path.join(Config.getInstance().getTempDir(), "stone%s.prof" % str(random.random())) result = [None] profile.runctx("result[0] = self._process()", globals(), locals(), proffilename) res = result[0] else: res = self._process() # Save web session, just when needed sm = session.getSessionManager() sm.maintain_session(self._req, self._websession) # notify components that the request has finished self._notify('requestFinished', self._req) # Raise a conflict error if enabled. This allows detecting conflict-related issues easily. if retry > (MAX_RETRIES - forcedConflicts): raise ConflictError self._endRequestSpecific2RH( True ) # I.e. implemented by Room Booking request handlers DBMgr.getInstance().endRequest( True ) Logger.get('requestHandler').info('Request %s successful' % (id(self._req))) #request succesfull, now, doing tas that must be done only once try: GenericMailer.flushQueue(True) # send emails self._deleteTempFiles() except: Logger.get('mail').exception('Mail sending operation failed') pass break except MaKaCError, e: #DBMgr.getInstance().endRequest(False) res = self._processError(e) except (ConflictError, POSKeyError): import traceback # only log conflict if it wasn't forced if retry <= (MAX_RETRIES - forcedConflicts): Logger.get('requestHandler').warning('Conflict in Database! (Request %s)\n%s' % (id(self._req), traceback.format_exc())) self._abortSpecific2RH() DBMgr.getInstance().abort() retry -= 1 continue except ClientDisconnected: Logger.get('requestHandler').warning('Client Disconnected! (Request %s)' % id(self._req) ) self._abortSpecific2RH() DBMgr.getInstance().abort() retry -= 1 time.sleep(10-retry) continue except KeyAccessError, e: #Key Access error treatment res = self._processKeyAccessError( e ) self._endRequestSpecific2RH( False ) DBMgr.getInstance().endRequest(False)
def thread1(self): ContextManager.set("samevariable", "a") time.sleep(2) self.assertEquals(ContextManager.get("samevariable"), "a")