Issues (1588)

myems-cleaning/clean_analog_value.py (1 issue)

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