44 lines
		
	
	
		
			903 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			903 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| from threading import Thread
 | |
| from mysql.connector import connect, Error
 | |
| from time import sleep, time
 | |
| from macros import *
 | |
| import os
 | |
| 
 | |
| 
 | |
| class NThread(Thread):
 | |
| 	conn = None
 | |
| 
 | |
| 	def __init__(self, event):
 | |
| 		super(NThread, self).__init__()
 | |
| 		self.event = event
 | |
| 
 | |
| 
 | |
| 	def clean(self):
 | |
| 		with self.conn.cursor() as c:
 | |
| 			c.execute(f"DELETE FROM `asiclogs` WHERE `time` < UNIX_TIMESTAMP() - %s", (CONF.get('db-log-days'),))
 | |
| 			c.execute(f"DELETE FROM `laststate` WHERE `time` < UNIX_TIMESTAMP() - 30")
 | |
| 
 | |
| 			self.conn.commit()
 | |
| 
 | |
| 
 | |
| 	def run(self):
 | |
| 		try:
 | |
| 			SUCC("Cleaner started!")
 | |
| 
 | |
| 			self.conn = connect(
 | |
| 				host=CONF.get('db', 'host'),
 | |
| 				user=CONF.get('db', 'user'),
 | |
| 				password=CONF.get('db', 'password'),
 | |
| 				database=CONF.get('db', 'name'))
 | |
| 
 | |
| 			while 1:
 | |
| 				if self.event.is_set():
 | |
| 					SUCC('Cleaner stopped!')
 | |
| 					break
 | |
| 
 | |
| 				self.clean()
 | |
| 				sleep(5)
 | |
| 		except Exception as e:
 | |
| 			CRIT(str(e))
 | |
| 			os._exit(1)
 |