def test_context(self): context.foo = 1 def test(): foo = random.random() context.foo = foo time.sleep(0.5 + random.random()) print(context) self.assertEqual(context.foo, foo) context._threadclear() self.assertFalse(hasattr(context, 'foo')) for x in range(1, 10): six.moves._thread.start_new_thread(test, ()) time.sleep(0.5) for i in range(10): time.sleep(0.2 + random.random()) self.assertEqual(context.foo, 1) context.foo = 2 context.bar = 3 self.assertEqual(context.foo, 2) self.assertEqual(context.bar, 3) context._threadclear() self.assertFalse(hasattr(context, 'foo')) self.assertFalse(hasattr(context, 'bar'))
def test(): foo = random.random() context.foo = foo time.sleep(0.5 + random.random()) print(context) self.assertEqual(context.foo, foo) context._threadclear() self.assertFalse(hasattr(context, 'foo'))
def application(environ, start_response): global firstcall if firstcall: server_setup(environ) firstcall = False # XMLRPC uses POST only. Reject anything else if environ['REQUEST_METHOD'] != 'POST': headers = [ ('Allow', 'POST'), ] start_response('405 Method Not Allowed', headers) response = "Method Not Allowed\nThis is an XML-RPC server. Only POST requests are accepted." headers = [ ('Content-Length', str(len(response))), ('Content-Type', "text/plain"), ] return [response] if opts.get('ServerOffline'): return offline_reply(start_response, msg=opts.get("OfflineMessage", None)) # XXX check request length # XXX most of this should be moved elsewhere if 1: try: start = time.time() memory_usage_at_start = get_memory_usage() context._threadclear() context.commit_pending = False context.opts = opts context.handlers = HandlerAccess(registry) context.environ = environ context.policy = policy try: context.cnx = koji.db.connect() except Exception: return offline_reply(start_response, msg="database outage") h = ModXMLRPCRequestHandler(registry) if environ.get('CONTENT_TYPE') == 'application/octet-stream': response = h._wrap_handler(h.handle_upload, environ) else: response = h._wrap_handler(h.handle_rpc, environ) headers = [ ('Content-Length', str(len(response))), ('Content-Type', "text/xml"), ] start_response('200 OK', headers) if h.traceback: #rollback context.cnx.rollback() elif context.commit_pending: # Currently there is not much data we can provide to the # pre/postCommit callbacks. The handler can access context at # least koji.plugin.run_callbacks('preCommit') context.cnx.commit() koji.plugin.run_callbacks('postCommit') memory_usage_at_end = get_memory_usage() if memory_usage_at_end - memory_usage_at_start > opts[ 'MemoryWarnThreshold']: paramstr = repr(getattr(context, 'params', 'UNKNOWN')) if len(paramstr) > 120: paramstr = paramstr[:117] + "..." h.logger.warning( "Memory usage of process %d grew from %d KiB to %d KiB (+%d KiB) processing request %s with args %s" % (os.getpid(), memory_usage_at_start, memory_usage_at_end, memory_usage_at_end - memory_usage_at_start, context.method, paramstr)) h.logger.debug("Returning %d bytes after %f seconds", len(response), time.time() - start) finally: #make sure context gets cleaned up if hasattr(context, 'cnx'): try: context.cnx.close() except Exception: pass context._threadclear() return [response] #XXX
def handler(req, profiling=False): global firstcall, ready, registry, opts, plugins, policy if firstcall: firstcall = False opts = load_config(req) setup_logging(opts) plugins = load_plugins(opts) registry = get_registry(opts, plugins) policy = get_policy(opts, plugins) ready = True if not ready: #this will happen on subsequent passes if an error happens in the firstcall code opts['ServerOffline'] = True opts['OfflineMessage'] = 'server startup error' if profiling: import profile, pstats, StringIO, tempfile global _profiling_req _profiling_req = req temp = tempfile.NamedTemporaryFile() profile.run( "import kojixmlrpc; kojixmlrpc.handler(kojixmlrpc._profiling_req, False)", temp.name) stats = pstats.Stats(temp.name) strstream = StringIO.StringIO() sys.stdout = strstream stats.sort_stats("time") stats.print_stats() req.write("<pre>" + strstream.getvalue() + "</pre>") _profiling_req = None else: try: if opts.get('ServerOffline'): offline_reply(req, msg=opts.get("OfflineMessage", None)) return apache.OK context._threadclear() context.commit_pending = False context.opts = opts context.handlers = HandlerAccess(registry) context.req = req context.policy = policy koji.db.provideDBopts(database=opts["DBName"], user=opts["DBUser"], password=opts.get("DBPass", None), host=opts.get("DBHost", None)) try: context.cnx = koji.db.connect() except Exception: offline_reply(req, msg="database outage") return apache.OK h = ModXMLRPCRequestHandler(registry) h.handle_request(req) if h.traceback: #rollback context.cnx.rollback() elif context.commit_pending: context.cnx.commit() finally: #make sure context gets cleaned up if hasattr(context, 'cnx'): try: context.cnx.close() except Exception: pass context._threadclear() return apache.OK
def handler(req, profiling=False): global firstcall, ready, registry, opts, plugins, policy if firstcall: firstcall = False opts = load_config(req) setup_logging(opts) plugins = load_plugins(opts) registry = get_registry(opts, plugins) policy = get_policy(opts, plugins) ready = True if not ready: #this will happen on subsequent passes if an error happens in the firstcall code opts['ServerOffline'] = True opts['OfflineMessage'] = 'server startup error' if profiling: import profile, pstats, StringIO, tempfile global _profiling_req _profiling_req = req temp = tempfile.NamedTemporaryFile() profile.run("import kojixmlrpc; kojixmlrpc.handler(kojixmlrpc._profiling_req, False)", temp.name) stats = pstats.Stats(temp.name) strstream = StringIO.StringIO() sys.stdout = strstream stats.sort_stats("time") stats.print_stats() req.write("<pre>" + strstream.getvalue() + "</pre>") _profiling_req = None else: try: if opts.get('ServerOffline'): offline_reply(req, msg=opts.get("OfflineMessage", None)) return apache.OK context._threadclear() context.commit_pending = False context.opts = opts context.handlers = HandlerAccess(registry) context.req = req context.policy = policy koji.db.provideDBopts(database = opts["DBName"], user = opts["DBUser"], password = opts.get("DBPass",None), host = opts.get("DBHost", None)) try: context.cnx = koji.db.connect() except Exception: offline_reply(req, msg="database outage") return apache.OK h = ModXMLRPCRequestHandler(registry) h.handle_request(req) if h.traceback: #rollback context.cnx.rollback() elif context.commit_pending: context.cnx.commit() finally: #make sure context gets cleaned up if hasattr(context,'cnx'): try: context.cnx.close() except Exception: pass context._threadclear() return apache.OK
def application(environ, start_response): global firstcall if firstcall: server_setup(environ) firstcall = False # XMLRPC uses POST only. Reject anything else if environ['REQUEST_METHOD'] != 'POST': headers = [ ('Allow', 'POST'), ] start_response('405 Method Not Allowed', headers) response = "Method Not Allowed\nThis is an XML-RPC server. Only POST requests are accepted." headers = [ ('Content-Length', str(len(response))), ('Content-Type', "text/plain"), ] return [response] if opts.get('ServerOffline'): return offline_reply(start_response, msg=opts.get("OfflineMessage", None)) # XXX check request length # XXX most of this should be moved elsewhere if 1: try: start = time.time() memory_usage_at_start = get_memory_usage() context._threadclear() context.commit_pending = False context.opts = opts context.handlers = HandlerAccess(registry) context.environ = environ context.policy = policy try: context.cnx = koji.db.connect() except Exception: return offline_reply(start_response, msg="database outage") h = ModXMLRPCRequestHandler(registry) if environ['CONTENT_TYPE'] == 'application/octet-stream': response = h._wrap_handler(h.handle_upload, environ) else: response = h._wrap_handler(h.handle_rpc, environ) headers = [ ('Content-Length', str(len(response))), ('Content-Type', "text/xml"), ] start_response('200 OK', headers) if h.traceback: #rollback context.cnx.rollback() elif context.commit_pending: context.cnx.commit() memory_usage_at_end = get_memory_usage() if memory_usage_at_end - memory_usage_at_start > opts['MemoryWarnThreshold']: paramstr = repr(getattr(context, 'params', 'UNKNOWN')) if len(paramstr) > 120: paramstr = paramstr[:117] + "..." h.logger.warning("Memory usage of process %d grew from %d KiB to %d KiB (+%d KiB) processing request %s with args %s" % (os.getpid(), memory_usage_at_start, memory_usage_at_end, memory_usage_at_end - memory_usage_at_start, context.method, paramstr)) h.logger.debug("Returning %d bytes after %f seconds", len(response), time.time() - start) finally: #make sure context gets cleaned up if hasattr(context,'cnx'): try: context.cnx.close() except Exception: pass context._threadclear() return [response] #XXX