Repository navigation
Expand file tree
/
Copy pathsyncDatabase.py
More file actions
193 lines (178 loc) · 7.05 KB
/
Copy pathsyncDatabase.py
File metadata and controls
193 lines (178 loc) · 7.05 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#-*- encoding: utf-8
from setting.Databasethomson import Database
from setting.File import File, getLog
from setting import config as osDb
import os
import sys
import threading
from Queue import Queue
import time
import logging, logging.config
from setting.utils import *
from thomson_api import Job, JobDetail, Workflow, Node
threadLimiter = threading.BoundedSemaphore(10)
# strQuery = ""
jobp_Q = Queue()
main_Q = Queue()
def thread_sql(jobDetail=None):
jobp_Q.put(jobDetail.parse_xml_2_query(jobDetail.get_param()))
# time.sleep(10)
jobp_Q.task_done()
#################################
#---------create table----------#
#################################
def create_tbJob():
sql = "create table job(jid int unsigned, host nvarchar(20), state char(10), status char(10), prog int unsigned, ver int unsigned, startdate int unsigned, enddate int unsigned);"
command = command_sql(sql)
try:
os.system(command)
print "create table Job\n#####success#####"
except Exception as e:
raise e
def create_tbParam():
sql = "create table job_param(jid int unsigned, host nvarchar(20), name nvarchar(50), wid nvarchar(50), backup nvarchar(5));"
command = command_sql(sql)
try:
os.system(command)
print "create table Param Job\n#####success#####"
except Exception as e:
print e
def create_tbWorkflow():
sql= """create table workflow(wid varchar(50) CHARACTER SET utf8, name varchar(50) CHARACTER SET utf8, host nvarchar(20), pubver int unsigned, priver int unsigned);"""
command = command_sql(sql)
print "create table Workflow\n#####success#####"
try:
os.system(command)
except Exception as e:
print e
def create_tbNode():
sql = "create table node(nid int unsigned, host nvarchar(20), cpu int unsigned, alloccpu int unsigned, mem int unsigned, allocmem int unsigned, status char(10), state char(10), uncreachable char(5));"
command = command_sql(sql)
print "create table Node\n#####success#####"
try:
os.system(command)
except Exception as e:
raise e
def create_tbNodeDetail():
sql = "create table node_detail(nid int unsigned, host nvarchar(20), jid int unsigned);"
command = command_sql(sql)
print "create table Node Detail\n#####success#####"
try:
os.system(command)
except Exception as e:
raise e
#################################
#---------insert table----------#
#################################
#insert job table
def insert_job(session=None, host=None):
logger = getLog('Sync_Data')
logger.setLevel(logging.INFO)
db = Database()
session = db.connect()
start = time.time()
try:
dataJob = get_job(host)
logger.info('Get query insert Job %s completed %s and len %s'%(host['host'], time.time() - start, len(dataJob)))
truncate_table(session, ["delete from job where host = '{0}';".format(host['host'])])
# truncate_table(session, ["delete from job_param where host = '{0}';".format(host['host'])])
db = Database()
db.many_insert(session, 'job', dataJob, 'jid', 'host', 'state', 'status', 'prog', 'ver', 'startdate', 'enddate')
#db.many_insert(session, 'job_param', dataPara, 'jid', 'host', 'name', 'wid', 'backup')
except Exception as e:
logerr = getLog('Error_Sync_Data')
logerr.error('Get Log {0} and len jobs {1}'.format(e,len(dataJob)))
finally:
db.close_connect(session)
print(session)
return 0
#insert workflow table
def insert_workflow(session = None, host=None):
logger = getLog('Sync_Data')
logger.setLevel(logging.INFO)
db = Database()
session = db.connect()
start = time.time()
try:
data = get_workflow(host)
logger.info('Get query insert Workflow %s Completed in %s and len %s.' %(host['host'], time.time() - start,len(data)))
#logger.info('Get query insert Workflow %s data is %s.' %(host['host'],data))
truncate_table(session, ["delete from workflow where host = '{0}';".format(host['host'])])
db = Database()
db.many_insert(session, 'workflow',data, 'wid', 'host', 'name')
except Exception as e:
logerr = getLog('Error_Sync_Data')
logerr.error('Get workflow error %s.'%(e))
raise
finally:
db.close_connect(session)
print(session)
return 0
#insert node table
def insert_node(session = None, host=None):
db = Database()
session = db.connect()
logger = getLog('Sync_Data')
logger.setLevel(logging.INFO)
start = time.time()
try:
data_node = get_node(host)
logger.info(' Get query insert Node %s Completed in %s.' %(host['host'] ,time.time() - start))
truncate_table(session,["delete from node where host = '{0}';".format(host['host'])])
db.many_insert(session, 'node', data_node, 'nid', 'host', 'cpu', 'alloccpu', 'mem', 'allocmem', 'status', 'state', 'uncreachable')
data_nodedetail = get_node_detail(host)
truncate_table(session,["delete from node_detail where host = '{0}';".format(host['host'])])
db.many_insert(session, 'node_detail', data_nodedetail, 'nid','host','jid')
except Exception as e:
logerr = getLog('Error_Sync_Data')
logerr.error('Get Node %s' %(e))
raise
finally:
db.close_connect(session)
print(session)
return 0
def truncate_table(session, argsql):
db = Database()
flag = True
count = 0
while flag and count <= 3 :
try:
for ite in argsql:
time.sleep(1)
db.execute_nonquery(session, ite)
session.commit()
flag = False
count +=1
return 1
except Exception as e:
logerr = getLog('Error_Sync_Data')
logerr.error('Truncate table %s'%(e))
flag = True
count += 1
finally:
logger = getLog('Sync_Data')
logger.info('Final truncate table')
return 0
def main():
list_Jobs = []
#sqlTruncate =[ "truncate workflow;", "truncate node;", "truncate node_detail;","alter table workflow auto_increment = 1;", "alter table node auto_increment = 1;", " alter table node_detail auto_increment = 1;"]
for host in osDb.THOMSON_HOST:
thread_job = threading.Thread(target=insert_job, kwargs={'host':host})
thread_job.start()
list_Jobs.append(thread_job)
thread_workflow = threading.Thread(target=insert_workflow, kwargs={'host':host})
thread_workflow.start()
list_Jobs.append(thread_workflow)
thread_node = threading.Thread(target=insert_node, kwargs={'host':host})
thread_node.start()
list_Jobs.append(thread_node)
# deamon Queue
#for job in list_Jobs:
#job.daemon = False
#job.start()
# job.join()
# main_Q.join()
#time.sleep(5)
if __name__ == '__main__':
main()
time.sleep(20)