-
Notifications
You must be signed in to change notification settings - Fork 1
/
updatingWorker.py
86 lines (71 loc) · 2.09 KB
/
updatingWorker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import threading
import json
import os
import mysql.connector
# Functions
# 2 set time interval to be called
def setInterval(func, time):
e = threading.Event()
while not e.wait(time):
func()
# 3 reads the dump file and execute queries
def readExecuteDumps():
global files_list
global files_runned
# 3.1 list of files already read
log_file = open('dump_logs.json', 'r')
data = json.load(log_file)
# 3.2 list of all files
files_list = listFilesInDirectory()
# 3.3 list remaining files to run
remainingFiles(data)
# 3.4 insert into database new updates
saveLog()
files_list = []
files_runned = []
data = ""
# 3.2 list all the dump files
def listFilesInDirectory():
files = []
path = "./dumps"
for diretorio, subpastas, arquivos in os.walk(path):
for arquivo in arquivos:
files.append(arquivo)
return files
# 3.3 remove from the list the already runned files
def remainingFiles(data):
global files_list
for file in files_list[:]:
for file2 in data['logs'][:]:
if file == file2:
files_list.remove(file)
files_runned.append(file)
# 3.4 save in the log and log file the dumps already updated
def saveLog():
workerdb = mysql.connector.connect(
host="localhost",
user="root",
password="123456",
database="library_worker"
)
mycursor = workerdb.cursor()
while len(files_list):
dump = open('./dumps/' + files_list[0], 'r')
for line in dump:
sql = line
print('query = ' + sql)
mycursor.execute(sql)
files_runned.append(files_list[0])
files_list.remove(files_list[0])
workerdb.commit()
updateLogFile()
# 4 update the log file (dump_logs.json)
def updateLogFile():
data = { "logs" : files_runned }
with open('dump_logs.json', 'w', encoding='utf8') as f:
json.dump(data, f, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ':'))
# 1 global var for files_list
files_list = []
files_runned = []
# 2 start
setInterval(readExecuteDumps, 15)