Commit 4d933089b50791a3d3c32eba0d995ded39e58d58
1 parent
b55aab93
Exists in
devel
[TranslationServer] Adiciona metódo para reconexão do BD
Showing
1 changed file
with
82 additions
and
37 deletions
Show diff stats
src/TranslationServer.py
@@ -35,14 +35,24 @@ def dict_mode(): | @@ -35,14 +35,24 @@ def dict_mode(): | ||
35 | WEBGL_SIGNS_PATH=os.path.join(SIGNS_PATH, "WEBGL") | 35 | WEBGL_SIGNS_PATH=os.path.join(SIGNS_PATH, "WEBGL") |
36 | BUNDLES_PATH={"IOS":IOS_SIGNS_PATH, "ANDROID":ANDROID_SIGNS_PATH, "STANDALONE":STANDALONE_SIGNS_PATH, "WEBGL":WEBGL_SIGNS_PATH} | 36 | BUNDLES_PATH={"IOS":IOS_SIGNS_PATH, "ANDROID":ANDROID_SIGNS_PATH, "STANDALONE":STANDALONE_SIGNS_PATH, "WEBGL":WEBGL_SIGNS_PATH} |
37 | 37 | ||
38 | -def full_mode(): | ||
39 | - dict_mode() | 38 | +def connect_database(): |
40 | import MySQLdb, warnings | 39 | import MySQLdb, warnings |
41 | global conn | 40 | global conn |
42 | warnings.filterwarnings('ignore', category=MySQLdb.Warning) | 41 | warnings.filterwarnings('ignore', category=MySQLdb.Warning) |
43 | - conn = MySQLdb.connect(user="root", db="signsdb") | 42 | + while True: |
43 | + try: | ||
44 | + conn = MySQLdb.connect(user="root", db="signsdb") | ||
45 | + except: | ||
46 | + print "Trying to connect to the database...\n" | ||
47 | + sleep(5) | ||
48 | + continue | ||
49 | + break | ||
44 | check_database() | 50 | check_database() |
45 | 51 | ||
52 | +def full_mode(): | ||
53 | + dict_mode() | ||
54 | + connect_database() | ||
55 | + | ||
46 | def logger(): | 56 | def logger(): |
47 | global app | 57 | global app |
48 | logfile = os.path.join(os.environ['HOME'], "translate.log") | 58 | logfile = os.path.join(os.environ['HOME'], "translate.log") |
@@ -95,54 +105,89 @@ def check_database(): | @@ -95,54 +105,89 @@ def check_database(): | ||
95 | cursor.close() | 105 | cursor.close() |
96 | 106 | ||
97 | def insert_sign_db(sign_name, value, has): | 107 | def insert_sign_db(sign_name, value, has): |
98 | - cursor = conn.cursor() | ||
99 | - query_string = "INSERT INTO signs (sign_name, amount, has) VALUES (%s, %s, %s)" | ||
100 | - cursor.execute(query_string, (sign_name, value, has)); | ||
101 | - conn.commit() | 108 | + try: |
109 | + cursor = conn.cursor() | ||
110 | + query_string = "INSERT INTO signs (sign_name, amount, has) VALUES (%s, %s, %s)" | ||
111 | + cursor.execute(query_string, (sign_name, value, has)); | ||
112 | + conn.commit() | ||
113 | + except MySQLdb.OperationalError: | ||
114 | + connect_database() | ||
115 | + print "Reconnecting..." | ||
116 | + insert_sign_db(sign_name, value, has) | ||
102 | 117 | ||
103 | def update_sign_db(sign_name, amount, has): | 118 | def update_sign_db(sign_name, amount, has): |
104 | - cursor = conn.cursor() | ||
105 | - query_string = "UPDATE signs SET amount=%s, has=%s WHERE sign_name=%s" | ||
106 | - cursor.execute(query_string, (amount, has, sign_name)); | ||
107 | - conn.commit() | 119 | + try: |
120 | + cursor = conn.cursor() | ||
121 | + query_string = "UPDATE signs SET amount=%s, has=%s WHERE sign_name=%s" | ||
122 | + cursor.execute(query_string, (amount, has, sign_name)); | ||
123 | + conn.commit() | ||
124 | + except MySQLdb.OperationalError: | ||
125 | + connect_database() | ||
126 | + print "Reconnecting..." | ||
127 | + update_sign_db(sign_name, amount, has) | ||
108 | 128 | ||
109 | def select_sign_db(sign_name): | 129 | def select_sign_db(sign_name): |
110 | - cursor = conn.cursor() | ||
111 | - query_string = "SELECT amount FROM signs WHERE sign_name=%s" | ||
112 | - cursor.execute(query_string, (sign_name)); | ||
113 | try: | 130 | try: |
114 | - amount = cursor.fetchone()[0] | ||
115 | - except TypeError: | ||
116 | - return None | ||
117 | - return amount | 131 | + cursor = conn.cursor() |
132 | + query_string = "SELECT amount FROM signs WHERE sign_name=%s" | ||
133 | + cursor.execute(query_string, (sign_name)); | ||
134 | + try: | ||
135 | + amount = cursor.fetchone()[0] | ||
136 | + except TypeError: | ||
137 | + return None | ||
138 | + return amount | ||
139 | + except MySQLdb.OperationalError: | ||
140 | + connect_database() | ||
141 | + print "Reconnecting..." | ||
142 | + select_sign_db(sign_name) | ||
118 | 143 | ||
119 | def insert_translation_db(text, gloss): | 144 | def insert_translation_db(text, gloss): |
120 | - cursor = conn.cursor() | ||
121 | - query_string = "INSERT INTO translations (text, gloss, amount) VALUES (%s, %s, %s)" | ||
122 | - cursor.execute(query_string, (text, gloss, 1)); | ||
123 | - conn.commit() | 145 | + try: |
146 | + cursor = conn.cursor() | ||
147 | + query_string = "INSERT INTO translations (text, gloss, amount) VALUES (%s, %s, %s)" | ||
148 | + cursor.execute(query_string, (text, gloss, 1)); | ||
149 | + conn.commit() | ||
150 | + except MySQLdb.OperationalError: | ||
151 | + connect_database() | ||
152 | + print "Reconnecting..." | ||
153 | + insert_translation_db(text, gloss) | ||
124 | 154 | ||
125 | def update_translation_db(text, gloss): | 155 | def update_translation_db(text, gloss): |
126 | - cursor = conn.cursor() | ||
127 | - query_string = "UPDATE translations SET amount=amount+1 WHERE text=%s AND gloss=%s" | ||
128 | - cursor.execute(query_string, (text, gloss)); | ||
129 | - conn.commit() | 156 | + try: |
157 | + cursor = conn.cursor() | ||
158 | + query_string = "UPDATE translations SET amount=amount+1 WHERE text=%s AND gloss=%s" | ||
159 | + cursor.execute(query_string, (text, gloss)); | ||
160 | + conn.commit() | ||
161 | + except MySQLdb.OperationalError: | ||
162 | + connect_database() | ||
163 | + print "Reconnecting..." | ||
164 | + update_translation_db(text, gloss) | ||
130 | 165 | ||
131 | def select_translation_db(text, gloss): | 166 | def select_translation_db(text, gloss): |
132 | - cursor = conn.cursor() | ||
133 | - query_string = "SELECT amount FROM translations WHERE text=%s AND gloss=%s" | ||
134 | - cursor.execute(query_string, (text, gloss)); | ||
135 | try: | 167 | try: |
136 | - amount = cursor.fetchone()[0] | ||
137 | - except TypeError: | ||
138 | - return None | ||
139 | - return amount | 168 | + cursor = conn.cursor() |
169 | + query_string = "SELECT amount FROM translations WHERE text=%s AND gloss=%s" | ||
170 | + cursor.execute(query_string, (text, gloss)); | ||
171 | + try: | ||
172 | + amount = cursor.fetchone()[0] | ||
173 | + except TypeError: | ||
174 | + return None | ||
175 | + return amount | ||
176 | + except MySQLdb.OperationalError: | ||
177 | + connect_database() | ||
178 | + print "Reconnecting..." | ||
179 | + select_translation_db(text, gloss) | ||
140 | 180 | ||
141 | def update_platform_db(platform_name): | 181 | def update_platform_db(platform_name): |
142 | - cursor = conn.cursor() | ||
143 | - query_string = "UPDATE platforms SET amount=amount+1 WHERE platform_name=%s" | ||
144 | - cursor.execute(query_string, (platform_name)); | ||
145 | - conn.commit() | 182 | + try: |
183 | + cursor = conn.cursor() | ||
184 | + query_string = "UPDATE platforms SET amount=amount+1 WHERE platform_name=%s" | ||
185 | + cursor.execute(query_string, (platform_name)); | ||
186 | + conn.commit() | ||
187 | + except MySQLdb.OperationalError: | ||
188 | + connect_database() | ||
189 | + print "Reconnecting..." | ||
190 | + update_platform_db(platform_name) | ||
146 | 191 | ||
147 | def update_database_statistic(sign_name, platform_name, file_exists): | 192 | def update_database_statistic(sign_name, platform_name, file_exists): |
148 | if RUN_MODE == "full": | 193 | if RUN_MODE == "full": |