lilspikey / django-background-task

A database-backed work queue for Django
BSD 3-Clause "New" or "Revised" License
107 stars 200 forks source link

handling an exception during error #34

Open McMoe89 opened 3 years ago

McMoe89 commented 3 years ago

Hi i am handling an exception in a background_task. Running the code alone works fine, but when i want to run it as a background_task there is always an error message. It seems that handling an exception is not possible through a background_task. I want to set a status when the background_tasks failed, so i tried it with an exception. In this case i try to load something into a teradata table that does not exist to force an error and handle teh exception.

Here is my Code:

@background(schedule=0) def forced_load_schedule(id, user):

Identify user and send message

#user = User.objects.get(pk=user_id)
#user.email_user('Here is a notification', 'You have been notified')
queryconfig = "SELECT * FROM ETL_config"
queryloads = "SELECT * FROM ETL_load WHERE id ='" + str(id) + "'"
teradatahost = '195.233.30.21'
udaExec = teradata.UdaExec (appName="Toolbox2.0_ETL", version="1.0", logConsole=False)
conn = sqlite3.connect('C:/inetpub/wwwroot/db.sqlite3')
conn2 = sqlite3.connect('C:/inetpub/wwwroot/db.sqlite3')

#Get teradata user and etl configuration from SQLite database
df = pd.read_sql_query(queryconfig, conn)

for row in df.iterrows():
    pos, d = row
    teradatauser = d["Teradata_User"]
    teradatapassword = d["Teradata_Password"]
    etltimer = d["ETL_Timer"]
    etlstatus = d["ETL_Status"]
    runningindicator = d["ETL_Running_Indicator"]

df = pd.read_sql_query(queryloads, conn)

for row in df.iterrows():
    pos, d = row
    loadsql = d["Load_SQL"]
    loadname = d["Name"]
    loadid = d["id"]
    etlgroup = d["ETL_Group"]

    #Connect to teradata
    with udaExec.connect(method="odbc",system=teradatahost, username=teradatauser,
                        password=teradatapassword, driver="Teradata") as connect:
                        now = str(datetime.now())[0:19]
                        #Execute load SQL
                        try:
                            curs = conn.cursor()
                            curs.execute("UPDATE ETL_load SET Trigger_Status = '1' WHERE id ='" + str(loadid) + "'")
                            conn.commit()
                            curs = connect.cursor()
                            curs.execute(loadsql)
                            curs = conn.cursor()
                            curs.execute("UPDATE ETL_load SET Load_Status = '1' WHERE id ='" + str(id) + "'")
                            curs.execute("UPDATE ETL_load SET Last_Load = '" + str(datetime.now()) + "' WHERE id ='" + str(id) + "'")
                            curs.execute("INSERT INTO log_log (Appname, Log_Title, Log_Message, Timestamp, Username) SELECT 'ETL_process' AS Appname, 'Info' AS Log_Title, 'Loadprocess " + loadname + " from ETL-Group " + etlgroup + " has been forced' AS Log_Message, '" + now + "' AS Timestamp, '" + user + "' AS Username")
                            conn.commit()
                        except Exception as e:
                            curs2 = conn2.cursor()
                            curs2.execute("UPDATE ETL_load SET Load_Status = '2' WHERE id ='" + str(id) + "'")
                            conn2.commit()

#Set ETL config Status back to 'active'
curs = conn.cursor()
curs.execute("UPDATE ETL_config SET ETL_Status = 'active'")
conn.commit()
conn.close()

My error message is this in the tasks table: Traceback (most recent call last): File "C:\inetpub\wwwroot\ETL\tasks.py", line 49, in forced_load_schedule curs = connect.cursor() File "C:\Python38-32\lib\site-packages\teradata\udaexec.py", line 745, in execute self._execute(self.cursor.execute, query, params, kwargs) File "C:\Python38-32\lib\site-packages\teradata\udaexec.py", line 790, in _execute func(query, params, kwargs) File "C:\Python38-32\lib\site-packages\teradata\tdodbc.py", line 614, in execute checkStatus(rc, hStmt=self.hStmt, method="SQLExecDirectW") File "C:\Python38-32\lib\site-packages\teradata\tdodbc.py", line 231, in checkStatus raise DatabaseError(i[2], u"[{}] {}".format(i[0], msg), i[0]) teradata.api.DatabaseError: (3807, "[42S02] [Teradata][ODBC Teradata Driver][Teradata Database] Object 'AVU_NL.TASHKKXAIADAD' does not exist. ")

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Python38-32\lib\site-packages\background_task\tasks.py", line 43, in bg_runner func(*args, **kwargs) File "C:\inetpub\wwwroot\ETL\tasks.py", line 60, in forced_load_schedule sqlite3.OperationalError: near ".": syntax error