| Total Complexity | 10 |
| Total Lines | 50 |
| Duplicated Lines | 56 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 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): |
|
|
|
|||
| 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 analog 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_analog_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 |