clean_digital_value.job()   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 28
Code Lines 24

Duplication

Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
eloc 24
nop 1
dl 28
loc 28
rs 7.904
c 0
b 0
f 0
1
import mysql.connector
2
import config
3
import time
4
from datetime import datetime, timedelta
5
import schedule
6
7
8 View Code Duplication
def job(logger):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
9
10
    cnx = None
11
    cursor = None
12
    try:
13
        cnx = mysql.connector.connect(**config.myems_historical_db)
14
        cursor = cnx.cursor()
15
    except Exception as e:
16
        logger.error("Error in clean digital value process " + str(e))
17
        if cursor:
18
            cursor.close()
19
        if cnx:
20
            cnx.close()
21
        return
22
23
    expired_utc = datetime.utcnow() - timedelta(days=config.live_in_days)
24
    try:
25
        cursor.execute(" DELETE FROM tbl_digital_value WHERE utc_date_time < %s ", (expired_utc,))
26
        cnx.commit()
27
    except Exception as e:
28
        logger.error("Error in delete_expired_trend process " + str(e))
29
    finally:
30
        if cursor:
31
            cursor.close()
32
        if cnx:
33
            cnx.close()
34
35
    logger.info("Deleted trend before date time in UTC: " + expired_utc.isoformat()[0:19])
36
37
38
def process(logger):
39
40
    if config.is_debug:
41
        # run the job immediately
42
        job(logger)
43
        return
44
45
    schedule.every(8).hours.do(job, logger)
46
47
    while True:
48
        schedule.run_pending()
49
        time.sleep(60)
50
51