put cron_fn in gevent to make it stop blocking

pull/173/head
Aevann 2023-07-23 00:31:44 +03:00
parent 8bd0429124
commit d0c7524154
2 changed files with 28 additions and 28 deletions

View File

@ -27,42 +27,43 @@ from files.cli import app, db_session, g
CRON_CACHE_TIMEOUT = 172800 CRON_CACHE_TIMEOUT = 172800
def cron_fn(every_5m, every_1d): def cron_fn(every_5m, every_1d):
g.db = db_session() with app.app_context():
g.v = None g.db = db_session()
g.v = None
try: try:
if every_5m: if every_5m:
kind = 'every_5m' kind = 'every_5m'
if FEATURES['GAMBLING']: if FEATURES['GAMBLING']:
check_if_end_lottery_task() check_if_end_lottery_task()
spin_roulette_wheel() spin_roulette_wheel()
#offsitementions.offsite_mentions_task(cache) #offsitementions.offsite_mentions_task(cache)
_award_timers_task() _award_timers_task()
if every_1d: if every_1d:
kind = 'every_1d' kind = 'every_1d'
stats.generate_charts_task(SITE) stats.generate_charts_task(SITE)
_sub_inactive_purge_task() _sub_inactive_purge_task()
cache.set('stats', stats.stats(), timeout=CRON_CACHE_TIMEOUT) cache.set('stats', stats.stats(), timeout=CRON_CACHE_TIMEOUT)
_generate_emojis_zip() _generate_emojis_zip()
_leaderboard_task() _leaderboard_task()
g.db.commit() g.db.commit()
except: except:
print(traceback.format_exc(), flush=True) print(traceback.format_exc(), flush=True)
g.db.rollback() g.db.rollback()
g.db.close() g.db.close()
del g.db del g.db
now = datetime.datetime.now().time() now = datetime.datetime.now().time()
print(f'Finished {kind} at {now}', flush=True) print(f'Finished {kind} at {now}', flush=True)
stdout.flush() stdout.flush()
@app.cli.command('cron', help='Run scheduled tasks.') @app.cli.command('cron', help='Run scheduled tasks.')
@click.option('--every-5m', is_flag=True, help='Call every 5 minutes.') @click.option('--every-5m', is_flag=True, help='Call every 5 minutes.')

View File

@ -52,5 +52,4 @@ if FEATURES['PING_GROUPS']:
if IS_LOCALHOST: if IS_LOCALHOST:
from files.helpers.cron import cron_fn from files.helpers.cron import cron_fn
with app.app_context(): gevent.spawn(cron_fn, True, False)
cron_fn(True, False)