Attempted fix for schema update issues

Closes database connection while pollers are running and reopens at the end.
This commit is contained in:
Paul Gear
2016-01-21 16:52:46 +10:00
parent c11cf53658
commit b8ad489ff0

View File

@ -95,6 +95,18 @@ else:
db_dbname = config['db_name'] db_dbname = config['db_name']
def db_open():
try:
if db_port == 0:
db = MySQLdb.connect(host=db_server, user=db_username, passwd=db_password, db=db_dbname)
else:
db = MySQLdb.connect(host=db_server, port=db_port, user=db_username, passwd=db_password, db=db_dbname)
return db
except:
print "ERROR: Could not connect to MySQL database!"
sys.exit(2)
# (c) 2015, GPLv3, Daniel Preussker <f0o@devilcode.org> <<<EOC1 # (c) 2015, GPLv3, Daniel Preussker <f0o@devilcode.org> <<<EOC1
if 'distributed_poller_group' in config: if 'distributed_poller_group' in config:
poller_group = str(config['distributed_poller_group']) poller_group = str(config['distributed_poller_group'])
@ -180,16 +192,6 @@ except:
devices_list = [] devices_list = []
try:
if db_port == 0:
db = MySQLdb.connect(host=db_server, user=db_username, passwd=db_password, db=db_dbname)
else:
db = MySQLdb.connect(host=db_server, port=db_port, user=db_username, passwd=db_password, db=db_dbname)
cursor = db.cursor()
except:
print "ERROR: Could not connect to MySQL database!"
sys.exit(2)
""" """
This query specificly orders the results depending on the last_polled_timetaken variable This query specificly orders the results depending on the last_polled_timetaken variable
Because this way, we put the devices likely to be slow, in the top of the queue Because this way, we put the devices likely to be slow, in the top of the queue
@ -203,6 +205,9 @@ else:
query = "select device_id from devices where disabled = 0 order by last_polled_timetaken desc" query = "select device_id from devices where disabled = 0 order by last_polled_timetaken desc"
# EOC2 # EOC2
db = db_open()
cursor = db.cursor()
cursor.execute(query) cursor.execute(query)
devices = cursor.fetchall() devices = cursor.fetchall()
for row in devices: for row in devices:
@ -215,6 +220,7 @@ if distpoll and not IsNode:
maxlocks = devices[0][0] maxlocks = devices[0][0]
minlocks = devices[0][1] minlocks = devices[0][1]
# EOC3 # EOC3
db.close()
""" """
A seperate queue and a single worker for printing information to the screen prevents A seperate queue and a single worker for printing information to the screen prevents
@ -358,6 +364,8 @@ if distpoll or memc_alive():
show_stopper = False show_stopper = False
db = db_open()
cursor = db.cursor()
query = "update pollers set last_polled=NOW(), devices='%d', time_taken='%d' where poller_name='%s'" % (polled_devices, query = "update pollers set last_polled=NOW(), devices='%d', time_taken='%d' where poller_name='%s'" % (polled_devices,
total_time, config['distributed_poller_name']) total_time, config['distributed_poller_name'])
response = cursor.execute(query) response = cursor.execute(query)