-
Notifications
You must be signed in to change notification settings - Fork 52
/
flask_background.py
48 lines (35 loc) · 1.39 KB
/
flask_background.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Bridgy background flask app, mostly task queue handlers: poll, propagate, etc."""
import logging
from flask import Flask, g
from oauth_dropins.webutil import flask_util
from oauth_dropins.webutil.appengine_config import ndb_client
from werkzeug.exceptions import HTTPException
import granary
import appengine_config # *after* import granary to override set_user_agent()
import util
logger = logging.getLogger(__name__)
# Flask app
app = Flask(__name__)
app.config.from_pyfile('config.py')
app.wsgi_app = flask_util.ndb_context_middleware(app.wsgi_app, client=ndb_client)
@app.errorhandler(Exception)
def background_handle_exception(e):
"""Common exception handler for background tasks.
Catches failed outbound HTTP requests and returns HTTP 304.
"""
if isinstance(e, HTTPException):
# raised by this app itself, pass it through
return str(e), e.code
transients = getattr(g, 'TRANSIENT_ERROR_HTTP_CODES', ())
source = getattr(g, 'source', None)
if source:
transients += source.RATE_LIMIT_HTTP_CODES + source.TRANSIENT_ERROR_HTTP_CODES
code, body = util.interpret_http_exception(e)
if ((code and int(code) // 100 == 5) or code in transients or
util.is_connection_failure(e)):
logger.error(f'Marking as error and finishing. {code}: {body}\n{e}')
return '', util.ERROR_HTTP_RETURN_CODE
raise e
@app.route('/_ah/<any(start, stop, warmup):_>')
def noop(_):
return 'OK'