def before_wiki(): """ Setup environment for wiki requests, start timers. """ logging.debug("running before_wiki") flaskg.clock = Clock() flaskg.clock.start('total') flaskg.clock.start('init') try: set_umask() # do it once per request because maybe some server # software sets own umask flaskg.unprotected_storage = app.unprotected_storage try: flaskg.user = setup_user() except HTTPException, e: # this makes stuff like abort(redirect(...)) work return app.handle_http_exception(e) flaskg.dicts = app.cfg.dicts() flaskg.groups = app.cfg.groups() flaskg.content_lang = app.cfg.language_default flaskg.current_lang = app.cfg.language_default flaskg.storage = app.storage setup_jinja_env()
def task_call (flask_app, environ) : global tasks with flask_app.request_context(environ) : try : tasks[task_id]["return_value"] = wrapped_function(*args, **kwargs) except HTTPException as e : tasks[task_id]["return_value"] = current_app.handle_http_exception(e) except Exception as err : tasks[task_id]["return_value"] = handle_exception(err) finally : now = datetime.timestamp(datetime.utcnow()) tasks[task_id]["completion_timestamp"] = now
def handle_permission_denied(error): if g.identity and g.identity.id is not None: code = 403 else: code = 401 # XXX since the custom exception error handler is done after the # default http ones, we work around this limitation. try: abort(code) except HTTPException as e: return current_app.handle_http_exception(e)
def task(app, environ): # Create a request context similar to that of the original request # so that the task can have access to flask.g, flask.request, etc. with app.request_context(environ): node_id = request.form['node'] try: logging.debug('Dismiss node %s' % node_id) try: stop_thread_for_node(node_id) except NoTaskException: logging.debug('Thread for node is not found, continue') except NoThreadException: logging.debug('Node is not found by task, continue') # Run the route function and record the response logging.debug("Executing target function") tasks[task_id]['node'] = node_id tasks[task_id]['os'] = request.form['os'] tasks[task_id]['thread_name'] = threading.Thread.getName( threading.current_thread()) tasks[task_id]['log_file'] = threaded_logging. \ get_log_name(tasks[task_id]['thread_name']) tasks[task_id]['log_handler'] = threaded_logging.start() tasks[task_id]['starttime'] = timestamp() tasks[task_id]['endtime'] = -1 tasks[task_id]['started'] = True tasks[task_id]['stopped'] = False tasks[task_id]['rv'] = target_func(*args, **kwargs) except HTTPException as e: logging.debug("Caught http exception:" + str(e)) tasks[task_id]['rv'] = \ current_app.handle_http_exception(e) traceback.print_exc(file=sys.stdout) except Exception as e: logging.error("Caught Exception:%s" % sys.exc_info()[1]) # The function raised an exception, so we set a 500 error tasks[task_id]['rv'] = InternalServerError() traceback.print_exc(file=sys.stdout) except: tasks[task_id]['rv'] = InternalServerError() msg = "Oops in flask_task! %s occured." % sys.exc_info()[1] logging.error(msg) finally: # We record the time of the response, to help in garbage # collecting old tasks tasks[task_id]['status'] = 'done' tasks[task_id]['endtime'] = timestamp() threaded_logging.stop(tasks[task_id]['log_handler']) logging.debug("target function completed")
def task_call(flask_app, environ): # Create a request context similar to that of the original request # so that the task can have access to flask.g, flask.request, etc. with flask_app.request_context(environ): try: tasks[task_id]['return_value'] = wrapped_function(*args, **kwargs) except HTTPException as e: tasks[task_id]['return_value'] = current_app.handle_http_exception(e) except Exception as e: # The function raised an exception, so we set a 500 error tasks[task_id]['return_value'] = InternalServerError() if current_app.debug: # We want to find out if something happened so reraise raise finally: # We record the time of the response, to help in garbage # collecting old tasks tasks[task_id]['completion_timestamp'] = datetime.timestamp(datetime.utcnow())
def task(app, environ, **task_kwargs): with app.request_context(environ): try: # Pass response_url and context data. tasks[uid]['result'] = f(*args, **kwargs) app.logger.info('Success!') except HTTPException as e: app.logger.info('Http Exception') tasks[uid]['result'] = current_app.handle_http_exception(e) except Exception as e: print(repr(e)) # The function raised an exception, so we set a 500 error tasks[uid]['result'] = InternalServerError() if current_app.debug: # We want to find out if something happened so reraise raise finally: # We record the time of the response, to help in garbage # collecting old tasks tasks[uid]['ended_at'] = time.time()
def task(app: Any, rq: _RequestProxy) -> None: """ Run the decorated route function in a new thread """ this_task = _tasks[task_id] # Pretty ugly hack, but no better solution is apparent: # Create a fresh Flask RequestContext object, wrapping our # custom _RequestProxy object that can be safely passed between threads with RequestContext(app, rq.environ, request=rq): try: # Run the original route function and record # the response (return value) rq.set_progress_func(progress) this_task["rv"] = f(*args, **kwargs) except HTTPException as e: this_task["rv"] = current_app.handle_http_exception(e) except Exception as e: # The function raised an exception, so we set a 500 error this_task["rv"] = InternalServerError() if current_app.debug: # We want to find out if something happened, so reraise raise finally: # We record the time of the response, to help in garbage # collecting old tasks this_task["t"] = utcnow()
def handle_exception(self, exc_type, exc_value, exc_tb): if isinstance(exc_value, HTTPException): return current_app.handle_http_exception(exc_value) else: raise exc_type, exc_value, exc_tb