11from app import app , db , imgproxy
22from libforget .auth import require_auth_api , get_viewer
33from flask import jsonify , redirect , make_response , request , Response
4- from model import Account
4+ from model import Account , WorkerCheckin
55import libforget .settings
66import libforget .json
77import random
8+ from datetime import datetime , timedelta
89
910@app .route ('/api/health_check' ) # deprecated 2021-03-12
1011@app .route ('/api/status_check' )
@@ -19,14 +20,23 @@ def api_status_check():
1920 except Exception :
2021 return ('Redis bad' , 500 )
2122
22- if db .session .execute (db .text ("""
23- SELECT 1 FROM accounts
24- WHERE last_delete > now() - '60 minutes'::INTERVAL
25- OR last_fetch > now() - '60 minutes'::INTERVAL
26- OR last_refresh > now() - '60 minutes'::INTERVAL
27- LIMIT 1;
28- """ )).fetchone () is None :
29- return ('Celery stalled' , 500 )
23+ CHECKIN_EVENTS = 5
24+ CHECKIN_PERIOD = timedelta (minutes = 10 )
25+ # sorry about the obtuse variable names, this trips if the frequency is
26+ # lower than events/period
27+ checkin_count = db .session .query (WorkerCheckin )\
28+ .filter (WorkerCheckin .created_at > db .func .now () - CHECKIN_PERIOD )\
29+ .count ()
30+ if checkin_count < events :
31+ return ('Celery slow, {} check-ins in {}' .format (
32+ checkin_count , CHECKIN_PERIOD
33+ ), 500 )
34+
35+ CHECKIN_LATENESS_THRESHOLD = timedelta (minutes = 5 )
36+ checkin = db .session .query (WorkerCheckin .created_at )\
37+ .order_by (db .desc (WorkerCheckin .created_at )).first ()
38+ if checkin + CHECKIN_LATENESS_THRESHOLD < datetime .utcnow ():
39+ return ('Celery late, last check-in was {}' .format (checkin ), 500 )
3040
3141 return 'OK'
3242
0 commit comments