From d0c7524154f8ecfe9f5b4c9d196de8f645cb9d5f Mon Sep 17 00:00:00 2001 From: Aevann Date: Sun, 23 Jul 2023 00:31:44 +0300 Subject: [PATCH] put cron_fn in gevent to make it stop blocking --- files/helpers/cron.py | 53 ++++++++++++++++++++-------------------- files/routes/__init__.py | 3 +-- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/files/helpers/cron.py b/files/helpers/cron.py index 6432905c4..af25e8d89 100644 --- a/files/helpers/cron.py +++ b/files/helpers/cron.py @@ -27,42 +27,43 @@ from files.cli import app, db_session, g CRON_CACHE_TIMEOUT = 172800 def cron_fn(every_5m, every_1d): - g.db = db_session() - g.v = None + with app.app_context(): + g.db = db_session() + g.v = None - try: - if every_5m: - kind = 'every_5m' - if FEATURES['GAMBLING']: - check_if_end_lottery_task() + try: + if every_5m: + kind = 'every_5m' + if FEATURES['GAMBLING']: + check_if_end_lottery_task() - spin_roulette_wheel() - #offsitementions.offsite_mentions_task(cache) - _award_timers_task() + spin_roulette_wheel() + #offsitementions.offsite_mentions_task(cache) + _award_timers_task() - if every_1d: - kind = 'every_1d' - stats.generate_charts_task(SITE) + if every_1d: + kind = 'every_1d' + 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() - g.db.commit() - except: - print(traceback.format_exc(), flush=True) - g.db.rollback() + _leaderboard_task() + g.db.commit() + except: + print(traceback.format_exc(), flush=True) + g.db.rollback() - g.db.close() - del g.db + g.db.close() + del g.db - now = datetime.datetime.now().time() + now = datetime.datetime.now().time() - print(f'Finished {kind} at {now}', flush=True) - stdout.flush() + print(f'Finished {kind} at {now}', flush=True) + stdout.flush() @app.cli.command('cron', help='Run scheduled tasks.') @click.option('--every-5m', is_flag=True, help='Call every 5 minutes.') diff --git a/files/routes/__init__.py b/files/routes/__init__.py index 1cf41c517..825a7c813 100644 --- a/files/routes/__init__.py +++ b/files/routes/__init__.py @@ -52,5 +52,4 @@ if FEATURES['PING_GROUPS']: if IS_LOCALHOST: from files.helpers.cron import cron_fn - with app.app_context(): - cron_fn(True, False) + gevent.spawn(cron_fn, True, False)