-
Notifications
You must be signed in to change notification settings - Fork 25
/
sys_info.py
161 lines (140 loc) · 4.12 KB
/
sys_info.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
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
'''
Author: Guanyu Gao
Email: guanyugao@gmail.com
Description: the interface for accessing the Mysql database.
'''
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import time
import config
import MySQLdb
ip = config.mysql_ip
passwd = config.mysql_password
user_name = config.mysql_user_name
db_name = config.mysql_db_name
def init_db():
try:
con = MySQLdb.connect(ip, user_name, passwd, db_name)
cur = con.cursor()
cur.execute("create table if not exists task_info(id VARCHAR(100), submit_time REAL, start_time REAL, \
finish_time REAL, service_type INTEGER, trans_time REAL, task_ongoing INTEGER)")
con.commit()
cur.execute("create table if not exists server_info(id VARCHAR(100) NOT NULL PRIMARY KEY, \
last_time REAL, state INTEGER)")
con.commit()
con.close()
return 0
except Exception, e:
print str(e)
#con.rollback()
return -1
def db_insert_task_info(task_id, service_type):
try:
con = MySQLdb.connect(ip, user_name, passwd, db_name)
cur = con.cursor()
cur_time = time.time()
sql_cmd = "INSERT INTO task_info VALUES('%s', %f, -1, -1, %d, -1, 1)" \
% (task_id, cur_time, service_type)
cur.execute(sql_cmd)
con.commit()
con.close()
return 0
except:
#con.rollback()
return -1
def db_update_finish_time(task_id, result):
try:
con = MySQLdb.connect(ip, user_name, passwd, db_name)
cur = con.cursor()
cur_time = time.time()
sql_cmd = "UPDATE task_info SET finish_time = %f, task_ongoing = %d WHERE id = '%s'" \
% (cur_time, result, task_id)
print sql_cmd
cur.execute(sql_cmd)
con.commit()
con.close()
return 0
except:
#con.rollback()
return -1
def db_update_start_time(task_id):
try:
con = MySQLdb.connect(ip, user_name, passwd, db_name)
cur = con.cursor()
cur_time = time.time()
sql_cmd = "UPDATE task_info SET start_time = %f WHERE id = '%s'" \
% (cur_time, task_id)
print sql_cmd
cur.execute(sql_cmd)
con.commit()
con.close()
return 0
except:
#con.rollback()
return -1
def db_update_trans_time(task_id, trans_time):
try:
con = MySQLdb.connect(ip, user_name, passwd, db_name)
cur = con.cursor()
sql_cmd = "UPDATE task_info SET trans_time = %f WHERE id = '%s'" \
% (trans_time, task_id)
print sql_cmd
cur.execute(sql_cmd)
con.commit()
con.close()
return 0
except:
#con.rollback()
return -1
def get_task_progress():
pass
'''
add worker information in MySQL
'''
def db_add_worker_info(host_name):
try:
con = MySQLdb.connect(ip, user_name, passwd, db_name)
cur = con.cursor()
sql_cmd = "INSERT INTO server_info VALUES('%s', 0, 1) ON DUPLICATE KEY UPDATE \
last_time = 0 and state = 1" % host_name
cur.execute(sql_cmd)
con.commit()
con.close()
return 0
except Exception, e:
#con.rollback()
print str(e)
return -1
'''
get worker state
'''
def db_get_worker_state(host_name):
try:
con = MySQLdb.connect(ip, user_name, passwd, db_name)
cur = con.cursor()
sql_cmd = "SELECT state FROM server_info where id = '%s'" % host_name
cur.execute(sql_cmd)
rows = cur.fetchall()
ret = rows[0][0]
con.close()
return ret
except Exception, e:
print str(e)
return -1
'''
update the last access time for a worker
'''
def db_update_last_access(host_name):
try:
con = MySQLdb.connect(ip, user_name, passwd, db_name)
cur = con.cursor()
cur_time = time.time()
sql_cmd = "update server_info set last_time = %f where id = '%s'" % (cur_time, host_name)
cur.execute(sql_cmd)
con.commit()
con.close()
return 0
except Exception, e:
print str(e)
return -1