def application(environ, start_response): if environ['PATH_INFO'] == '/whattimeisit': uwsgi.add_var('X-SSE-OFFLOAD', 'clock') return [] elif environ['PATH_INFO'] == '/whattimeisit2': uwsgi.add_var('X-SSE-OFFLOAD', 'clock') start_response('200 OK', [('Content-Type', 'event/stream'), ('Cache-Control', 'no-cache'), ('Foo', 'Bar')]) return [] else: start_response('200 OK', [('Content-Type', 'text/plain')]) return ['Hello World']
def run_codebox_script(self, request, socket, endpoint, metadata, path, **kwargs): skip_payload = request._empty_data # Skip payload if we're dealing with small text/plain requests # (for backwards compatibility when depending on META.user, META.admin) uwsgi.add_var('PAYLOAD_PARSED', '0' if skip_payload else '1') script = Munch(config={ 'allow_full_access': True, 'timeout': kwargs.get('timeout', settings.SOCKETS_DEFAULT_TIMEOUT), 'async': kwargs.get('async', settings.SOCKETS_DEFAULT_ASYNC), 'mcpu': kwargs.get('mcpu', settings.SOCKETS_DEFAULT_MCPU), }, runtime_name=kwargs.get('runtime', LATEST_NODEJS_RUNTIME), source='') if path in socket.file_list: # Prepare spec. script_files = socket.get_files() entrypoint = socket.get_local_path(path) spec = { 'files': script_files, 'source_hash': socket.get_hash(), 'entrypoint': entrypoint, 'output_limit': settings.SOCKETS_MAX_RESULT_SIZE, 'name': endpoint.name, 'cache': kwargs.get('cache', 0), } # Add environment if socket.environment_id: environment = Cached(SocketEnvironment, kwargs={'pk': socket.environment_id}).get() if not environment.is_ready: if environment.status == SocketEnvironment.STATUSES.ERROR: raise SocketEnvironmentFailure() raise SocketEnvironmentNotReady() spec['environment'] = environment.get_hash() spec['environment_url'] = environment.get_url() return self.run_view(request, obj=endpoint, script=script, metadata=metadata, endpoint=endpoint, spec=spec, skip_payload=skip_payload, flat_args=True, uwsgi_handler=self.get_codebox_handler)
def zip_file(self, request, *args, **kwargs): socket = self.get_object() real_file_list = { f_key: request.build_absolute_uri(Socket.get_storage().url(f_val['file'])) for f_key, f_val in socket.file_list.items() if not f_key.startswith('<') } # File list with full urls can get quite big so we pass it through tempfile with tempfile.NamedTemporaryFile(delete=False, suffix='.file_list', mode="w") as list_file: json.dump(real_file_list, list_file) try: propagate_uwsgi_params(get_tracing_attrs()) uwsgi.add_var('OFFLOAD_HANDLER', 'apps.sockets.handlers.SocketZipHandler') uwsgi.add_var('LIST_FILE', list_file.name) uwsgi.add_var( 'FILE_NAME', get_valid_filename('{}_{}'.format(socket.name, socket.version))) except ValueError: os.unlink(list_file.name) raise UwsgiValueError() return HttpResponse()
def status_offload(request): if not uwsgi or not settings.STATUS_OFFLOAD_SOCKET: return JsonResponse( { "error": "Status unavailable in this environment.", }, status=404, ) if not request.user.is_authenticated(): return JsonResponse({ "error": "Unauthenticated", }, status=401) redis = get_lock_redis() taskstore = TaskStore.get_for_user(request.user) pickle_id = str(uuid.uuid4()) pickled_data = pickle.dumps({ "taskstore": taskstore, "username": request.user.username }) redis.set(f"pickle_{pickle_id}", pickled_data, ex=60) hostname, port = settings.STATUS_OFFLOAD_SOCKET.split(":") offload_ip = socket.gethostbyname(hostname) uwsgi.add_var("PICKLE_ID", str(pickle_id)) uwsgi.add_var("OFFLOAD_TO_SSE", "y") uwsgi.add_var("OFFLOAD_SERVER", ":".join([offload_ip, port])) return HttpResponse()
def subscribe(): try: import uwsgi except ImportError: # If we're not running inside uWSGI, we make use of the fact that # the response of a Flask view can also be another WSGI application, # and while not being particularly scalable, we can run the SSE app # within the Flask dev server. import sseapp def _app(environ, start_response): environ['OFFLOAD_USER_IP'] = request.remote_addr return sseapp.application(environ, start_response) return _app # If we are running inside uWSGI, ask it to offload the request to the # gevent-based instance which only runs the SSE app. uwsgi.add_var('OFFLOAD_TO_SSE', 'y') uwsgi.add_var('OFFLOAD_SERVER', '/tmp/uwsgi-offload.sock') uwsgi.add_var('OFFLOAD_USER_IP', request.remote_addr) return app.response_class('', mimetype='text/event-stream')
def status_offload(request): if not uwsgi: return JsonResponse( { 'error': 'Status unavailable in this environment.', }, status=404, ) if not request.user.is_authenticated(): return JsonResponse( { 'error': 'Unauthenticated', }, status=401 ) redis = get_lock_redis() taskstore = TaskStore.get_for_user(request.user) pickle_id = str(uuid.uuid4()) pickled_data = pickle.dumps({ 'taskstore': taskstore, 'username': request.user.username }) redis.set( 'pickle_{}'.format(pickle_id), pickled_data, ex=60 ) uwsgi.add_var("PICKLE_ID", str(pickle_id)) uwsgi.add_var("OFFLOAD_TO_SSE", "y") uwsgi.add_var("OFFLOAD_SERVER", "/tmp/inthe_am_status.sock") return HttpResponse()
def subscribe(request): uwsgi.add_var("CC_USER", str(request.user)) uwsgi.route("uwsgi", settings.UWSGI_WEBSOCKETS_SOCKET) return HttpResponse("output that shouldn't appear")
def create_uwsgi_response(self, request, channel, last_id, room, transport='poll'): try: propagate_uwsgi_params(get_current_span_propagation()) if transport == 'poll': uwsgi.add_var('OFFLOAD_HANDLER', 'apps.channels.handlers.ChannelPollHandler') else: uwsgi.add_var('OFFLOAD_HANDLER', 'apps.channels.handlers.ChannelWSHandler') uwsgi.add_var('CHANNEL_PK', str(channel.pk)) uwsgi.add_var('INSTANCE_PK', str(request.instance.pk)) uwsgi.add_var('STREAM_CHANNEL', channel.get_stream_channel_name(room)) if room is not None: uwsgi.add_var('CHANNEL_ROOM', room) if last_id is not None: uwsgi.add_var('LAST_ID', str(last_id)) except ValueError: raise UwsgiValueError() return HttpResponse()
def create_uwsgi_response(self, request, obj, instance, trace, payload_key, meta_key=None, script=None, offload_handler_class=None, uwsgi_handler=None): try: propagate_uwsgi_params(get_current_span_propagation()) if uwsgi_handler is not None: uwsgi.add_var('UWSGI_HANDLER', uwsgi_handler) else: uwsgi.add_var('OFFLOAD_HANDLER', offload_handler_class or self.offload_handler_class) uwsgi.add_var('TASK_CLASS', self.script_task_class) uwsgi.add_var('OBJECT_PK', str(obj.pk)) uwsgi.add_var('INSTANCE_PK', str(instance.pk)) uwsgi.add_var('TRACE_PK', str(trace.pk)) uwsgi.add_var('PAYLOAD_KEY', payload_key) response_template = getattr(request, 'response_template', None) if response_template: uwsgi.add_var('TEMPLATE', response_template.name) if meta_key is not None: uwsgi.add_var('META_KEY', str(meta_key)) if script: uwsgi.add_var('SCRIPT_PK', str(script.pk)) except ValueError: raise UwsgiValueError() return HttpResponse()
def propagate_uwsgi_params(zipkin_attrs): if zipkin_attrs is not None and zipkin_attrs.is_sampled and uwsgi is not None: uwsgi.add_var('X-B3-SAMPLED', '1') uwsgi.add_var('X-B3-TRACEID', zipkin_attrs.trace_id) uwsgi.add_var('X-B3-PARENTSPANID', zipkin_attrs.span_id) uwsgi.add_var('X-B3-FLAGS', zipkin_attrs.flags)
def create_uwsgi(): new_id = Data.new() uwsgi.add_var("OFFLOAD_TO_POLLER", "y") uwsgi.add_var("POLLER_ID", str(new_id)) return make_response('id: {}'.format(new_id), 200, {})
def propagate_uwsgi_params(data): if uwsgi is None: return for k, v in data.items(): uwsgi.add_var(k, v)