From 8df3b4dec64b1a121d43531c509af33c98244b3d Mon Sep 17 00:00:00 2001 From: Michael Romashov Date: Wed, 10 Mar 2021 15:25:24 -0500 Subject: [PATCH] Fixed 'database is locked' error. --- apps/tmu/jobs.py | 18 +++++++++--------- zhu_core/scheduler.py | 16 ++++++++-------- zhu_core/settings.py | 3 +++ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/apps/tmu/jobs.py b/apps/tmu/jobs.py index bdf9ea2..dec6839 100644 --- a/apps/tmu/jobs.py +++ b/apps/tmu/jobs.py @@ -32,14 +32,14 @@ def fetch_metars(): for airport in airports_icao: url = 'https://avwx.rest/api/metar/' + airport + '?format=json&onfail=cache' resp = requests.get(url, headers=headers) - data = resp.json() - METAR.objects.update_or_create( - station=data.get('station'), - defaults={ - 'raw': data.get('sanitized'), - 'flight_rules': data.get('flight_rules'), - 'timestamp': pytz.utc.localize(datetime.fromisoformat(data.get('time').get('dt')[:-1])), - } - ) + query = METAR.objects.filter(station=airport) + if query.exists(): + query.delete() + + METAR( + raw=data.get('sanitized'), + flight_rules=data.get('flight_rules'), + timestamp=pytz.utc.localize(datetime.fromisoformat(data.get('time').get('dt')[:-1])), + ).save() diff --git a/zhu_core/scheduler.py b/zhu_core/scheduler.py index d223131..d15db1e 100644 --- a/zhu_core/scheduler.py +++ b/zhu_core/scheduler.py @@ -19,7 +19,7 @@ def start_scheduler(): scheduler.add_job( users.sync_vatusa_roster, - trigger=CronTrigger(minute=0), # Top of every hour + trigger=CronTrigger(minute=1), # Top of every hour + 1 id='sync_vatusa_roster', max_instances=1, replace_existing=True, @@ -27,24 +27,24 @@ def start_scheduler(): scheduler.add_job( users.update_user_ratings, - trigger=CronTrigger(minute=0), # Top of every hour + trigger=CronTrigger(minute=2), # Top of every hour + 2 id='update_user_ratings', max_instances=1, replace_existing=True, ) scheduler.add_job( - tmu.delete_inactive_atis, - trigger=CronTrigger(minute=0), # Top of every hour - id='delete_inactive_atis', + tmu.fetch_metars, + trigger=CronTrigger(minute=3), # Top of every hour + 3 + id='fetch_metars', max_instances=1, replace_existing=True, ) scheduler.add_job( - tmu.fetch_metars, - trigger=CronTrigger(minute=0), # Top of every hour - id='fetch_metars', + tmu.delete_inactive_atis, + trigger=CronTrigger(minute=4), # Top of every hour + 4 + id='delete_inactive_atis', max_instances=1, replace_existing=True, ) diff --git a/zhu_core/settings.py b/zhu_core/settings.py index 0512032..67ab90e 100644 --- a/zhu_core/settings.py +++ b/zhu_core/settings.py @@ -151,6 +151,9 @@ 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', + 'OPTIONS': { + 'timeout': 20, + } } }