def create_application(): # Adjust path sys.path.append(os.path.dirname(__file__)) # Import app try: from hello_redis_tasks import app except Exception: log('Could not load app:\n' + str(format_exc())) return None # Run worker try: from worker import TaskWorker worker = TaskWorker(app, debug=app.debug) worker.reset() worker.start() except Exception: log('Could not load worker:\n' + str(format_exc())) return None # Handle request def application(environ, start_response): log('Application called') try: results = app(environ, start_response) except Exception: log('*** ERROR ***\n' + str(format_exc()) + '\n*************') return results return application
# Errors @app.errorhandler(404) def page_not_found(e): return render_template('error.html', message='Not Found', description='The requested URL was not found on the server.'), 404 @app.errorhandler(ConnectionError) def connection_error(e): debug_description = "<strong>redis-server</strong> is" production_description = "both <strong>redis-server</strong> and <strong>worker.py</strong> are" description = "Check to make sure that %s running." % (debug_description if app.debug else production_description) return render_template('error.html', message='Coult not connect to the task queue', description=description), 500 # Run dev server if __name__ == '__main__': # Run both the task queue # TODO: When Flask version 0.8, refactor using the new # app.before_first_request() debug = app.config.get('DEBUG', True) use_reloader = app.config.get('DEBUG', True) if os.environ.get('WERKZEUG_RUN_MAIN') == 'true' or not use_reloader: from worker import TaskWorker worker = TaskWorker(app, debug=debug) worker.reset() worker.start() app.run(host=app.config.get('HOST'), port=app.config.get('PORT'), debug=debug, use_reloader=use_reloader)