close sqlalchemy session after a worker's work. closes #4

that was far easier than i anticipated...
This commit is contained in:
codl 2017-08-11 00:31:02 +02:00
parent 3dea73b0bd
commit 5a02d75f7c
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
1 changed files with 11 additions and 1 deletions

View File

@ -1,4 +1,4 @@
from celery import Celery from celery import Celery, Task
from app import app as flaskapp from app import app as flaskapp
from app import db from app import db
@ -31,6 +31,16 @@ if 'SENTRY_DSN' in flaskapp.config:
register_logger_signal(sentry) register_logger_signal(sentry)
register_signal(sentry) register_signal(sentry)
class DBTask(Task):
def __call__(self, *args, **kwargs):
try:
super().__call__(*args, **kwargs)
finally:
db.session.close()
app.Task = DBTask
@app.task(autoretry_for=(TwitterError, URLError)) @app.task(autoretry_for=(TwitterError, URLError))
def fetch_acc(id, cursor=None): def fetch_acc(id, cursor=None):
acc = Account.query.get(id) acc = Account.query.get(id)