From 61245829e1bac31bdc1a9a7fd69ba0493d240fcf Mon Sep 17 00:00:00 2001 From: Ameya Shenoy Date: Thu, 11 Feb 2021 18:06:12 +0530 Subject: [PATCH] feat: schedule job in case of failure Signed-off-by: Ameya Shenoy --- backend/app/apps.py | 5 --- backend/app/utils.py | 95 ++++++++++++++++++++++++-------------------- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/backend/app/apps.py b/backend/app/apps.py index 9e78197..8c69bf8 100644 --- a/backend/app/apps.py +++ b/backend/app/apps.py @@ -3,15 +3,10 @@ """Initializer and scheduling done here.""" -# standard imports -import datetime -import logging - # third-party imports import django_rq from django.apps import AppConfig -from django_rq.management.commands import rqscheduler class AppConfig(AppConfig): diff --git a/backend/app/utils.py b/backend/app/utils.py index 486b883..c3b7b49 100644 --- a/backend/app/utils.py +++ b/backend/app/utils.py @@ -11,6 +11,7 @@ from io import BytesIO, TextIOWrapper from zipfile import ZipFile # third-party imports +import django_rq import requests from django_redis import get_redis_connection @@ -49,49 +50,57 @@ def fetch_bhav_copy_equity_data(curr_date=None): @transaction.atomic def populate_bhav_copy_data(): """Populate DB with Bhav Copy data.""" - pipe = cache.pipeline() - data = fetch_bhav_copy_equity_data() - del data[0] # delete title row + try: + pipe = cache.pipeline() + data = fetch_bhav_copy_equity_data() + del data[0] # delete title row - cache.delete("stocks") - for stock in data: - # prevent creation of duplicate entries - pipe.rpush("stocks", stock[0]) - pipe.hset( - f"stock:{stock[0]}", - mapping={ - "sc_code": stock[0], - "sc_name": stock[1], - "sc_group": stock[2], - "sc_type": stock[3], - "open_price": float(stock[4]), - "high_price": float(stock[5]), - "low_price": float(stock[6]), - "close_price": float(stock[7]), - "last_price": float(stock[8]), - "prevclose_price": float(stock[9]), - "no_trades": int(stock[10]), - "no_of_shrs": int(stock[11]), - "net_turnov": float(stock[12]), - "tdcloindi": stock[13], - } - ) - pipe.execute() - BhavCopyEquity.objects.get_or_create( - sc_code=int(stock[0]), - sc_name=stock[1], - sc_group=stock[2], - sc_type=stock[3], - open_price=float(stock[4]), - high_price=float(stock[5]), - low_price=float(stock[6]), - close_price=float(stock[7]), - last_price=float(stock[8]), - prevclose_price=float(stock[9]), - no_trades=int(stock[10]), - no_of_shrs=int(stock[11]), - net_turnov=float(stock[12]), - tdcloindi=stock[13], + cache.delete("stocks") + for stock in data: + # prevent creation of duplicate entries + pipe.rpush("stocks", stock[0]) + pipe.hset( + f"stock:{stock[0]}", + mapping={ + "sc_code": stock[0], + "sc_name": stock[1], + "sc_group": stock[2], + "sc_type": stock[3], + "open_price": float(stock[4]), + "high_price": float(stock[5]), + "low_price": float(stock[6]), + "close_price": float(stock[7]), + "last_price": float(stock[8]), + "prevclose_price": float(stock[9]), + "no_trades": int(stock[10]), + "no_of_shrs": int(stock[11]), + "net_turnov": float(stock[12]), + "tdcloindi": stock[13], + } + ) + pipe.execute() + BhavCopyEquity.objects.get_or_create( + sc_code=int(stock[0]), + sc_name=stock[1], + sc_group=stock[2], + sc_type=stock[3], + open_price=float(stock[4]), + high_price=float(stock[5]), + low_price=float(stock[6]), + close_price=float(stock[7]), + last_price=float(stock[8]), + prevclose_price=float(stock[9]), + no_trades=int(stock[10]), + no_of_shrs=int(stock[11]), + net_turnov=float(stock[12]), + tdcloindi=stock[13], + ) + except: + # potential code for alerting if needed goes here + # Repeat job after 10 mins if fails at 6 pm + scheduler = django_rq.get_scheduler('default') + scheduler.schedule( + scheduled_time=datetime.datetime.now()+datetime.timedelta(minutes=10), + func=populate_bhav_copy_data, ) -