2017-07-27 20:20:59 +02:00
|
|
|
from celery import Celery
|
|
|
|
|
|
|
|
from app import app as flaskapp
|
|
|
|
from app import db
|
2017-07-28 00:08:20 +02:00
|
|
|
from model import Session, Account
|
2017-07-28 12:48:00 +02:00
|
|
|
import lib.twitter
|
|
|
|
from twitter import TwitterError
|
2017-07-28 01:07:51 +02:00
|
|
|
from datetime import timedelta, datetime
|
2017-07-27 20:20:59 +02:00
|
|
|
|
|
|
|
app = Celery('tasks', broker=flaskapp.config['CELERY_BROKER'], task_serializer='pickle')
|
|
|
|
|
|
|
|
@app.task
|
|
|
|
def remove_old_sessions():
|
2017-07-28 00:08:20 +02:00
|
|
|
Session.query.filter(Session.updated_at < (db.func.now() - timedelta(hours=12))).\
|
2017-07-27 20:20:59 +02:00
|
|
|
delete(synchronize_session=False)
|
|
|
|
db.session.commit()
|
|
|
|
|
2017-07-28 12:48:00 +02:00
|
|
|
@app.task(autoretry_for=(TwitterError,))
|
|
|
|
def fetch_acc(remote_id):
|
|
|
|
lib.twitter.fetch_acc(Account.query.get(remote_id), **flaskapp.config.get_namespace("TWITTER_"))
|
2017-07-28 00:08:20 +02:00
|
|
|
|
|
|
|
@app.task
|
2017-07-28 12:48:00 +02:00
|
|
|
def queue_fetch_for_most_stale_accounts(min_staleness=timedelta(minutes=1), limit=20):
|
|
|
|
accs = Account.query\
|
|
|
|
.filter(Account.last_fetch < db.func.now() - min_staleness)\
|
|
|
|
.order_by(db.asc(Account.last_fetch))\
|
|
|
|
.limit(limit)
|
|
|
|
for acc in accs:
|
|
|
|
fetch_acc.s(acc.remote_id).delay()
|
|
|
|
acc.last_fetch = db.func.now()
|
|
|
|
db.session.commit()
|
2017-07-27 20:20:59 +02:00
|
|
|
|
2017-07-28 01:07:51 +02:00
|
|
|
app.add_periodic_task(10*60, remove_old_sessions)
|
2017-07-28 12:48:00 +02:00
|
|
|
app.add_periodic_task(60, queue_fetch_for_most_stale_accounts)
|
2017-07-27 20:20:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.worker_main()
|