From 84d5de3cc9930342b57edaf06d47dcd1afc56e99 Mon Sep 17 00:00:00 2001 From: Martijn Vermaat <martijn@vermaat.name> Date: Tue, 8 May 2012 12:30:55 +0000 Subject: [PATCH] Make auto-reconnect to MySQL a configuration setting. Starting from Debian Wheezy, MySQLdb does no longer accept the auto_reconnect keyword argument. Therefore it must be possible to disable this in the configuration file. See Trac ticket #91. git-svn-id: https://humgenprojects.lumc.nl/svn/mutalyzer/trunk@520 eb6bd6ab-9ccd-42b9-aceb-e2899b4a52f1 --- extras/config.example | 4 +-- .../006-config-add-autoreconnect.migration | 32 +++++++++++++++++++ mutalyzer/Db.py | 28 ++++++++-------- mutalyzer/config.py | 5 +-- 4 files changed, 52 insertions(+), 17 deletions(-) create mode 100755 extras/migrations/006-config-add-autoreconnect.migration diff --git a/extras/config.example b/extras/config.example index df98ac59..e77c0b21 100644 --- a/extras/config.example +++ b/extras/config.example @@ -42,8 +42,8 @@ LocalMySQLuser = "mutalyzer" # Host name for the local databases. LocalMySQLhost = "localhost" - - +# Automatically reconnect to MySQL server (see Trac issue #91). +autoReconnect = yes # diff --git a/extras/migrations/006-config-add-autoreconnect.migration b/extras/migrations/006-config-add-autoreconnect.migration new file mode 100755 index 00000000..7b4512b7 --- /dev/null +++ b/extras/migrations/006-config-add-autoreconnect.migration @@ -0,0 +1,32 @@ +#!/bin/bash + +# Add 'auto reconnect to MySQL server' setting to the configuration file. +# +# Usage: +# ./006-config-add-autoreconnect.migration [migrate] + +COLOR_INFO='\033[32m' +COLOR_WARNING='\033[33m' +COLOR_ERROR='\033[31m' +COLOR_END='\033[0m' + +if [ -e /etc/mutalyzer/config ] && ! $(grep -q 'autoReconnect' /etc/mutalyzer/config); then + echo -e "${COLOR_WARNING}This migration is needed.${COLOR_END}" + if [ "$1" = 'migrate' ]; then + echo 'Performing migration.' + echo -e "${COLOR_INFO}Copying /etc/mutalyzer/config to /etc/mutalyzer/config.backup${COLOR_END}" + cp /etc/mutalyzer/config /etc/mutalyzer/config.backup + # Insert after LocalMySQLhost line + if $(grep -q '^LocalMySQLhost' /etc/mutalyzer/config && sed -i '/^LocalMySQLhost/ a\ +\ +# Automatically reconnect to MySQL server (see Trac issue #91).\ +autoReconnect = yes' /etc/mutalyzer/config); then + echo -e "${COLOR_INFO}Added autoReconnect = yes to /etc/mutalyzer/config${COLOR_END}" + echo 'Performed migration.' + else + echo -e "${COLOR_ERROR}Could not perform migration.${COLOR_END}" + fi + fi +else + echo -e "${COLOR_INFO}This migration is not needed.${COLOR_END}" +fi diff --git a/mutalyzer/Db.py b/mutalyzer/Db.py index fbd6c91b..2e23fbca 100644 --- a/mutalyzer/Db.py +++ b/mutalyzer/Db.py @@ -28,15 +28,6 @@ from mutalyzer import util from mutalyzer import config -# Automatically reconnect to the MySQL server. This is expecially useful for -# long-running processes such as the batch deamon, which would otherwise loose -# their connection on an event such as restarting the MySQL server. Also see -# Trac ticket #91. -# Be alarmed though, that this messes up transactions. Fortunately, Mutalyzer -# doesn't use transactions at the moment. -AUTO_RECONNECT = True - - # # Note that compound queries are split into single queries because of a bug # in MySQLdb. The functions load_Update(), merge_cdsUpdates() and @@ -57,7 +48,6 @@ class Db() : Public methods: - query(statement) ; General query function. """ - def __init__(self, dbName, mySqlUser, mySqlHost) : """ Log in to the database. @@ -71,10 +61,22 @@ class Db() : @type mySqlUser: string @arg mySqlHost: Host name for the database @type mySqlHost: string - """ - self.__db = MySQLdb.connect(user=mySqlUser, db=dbName, host=mySqlHost, - reconnect=AUTO_RECONNECT) + Note: Depending on a configuration setting, we automatically reconnect + to the MySQL server. This is expecially useful for long-running + processes such as the batch deamon, which would otherwise loose + their connection on an event such as restarting the MySQL server. + Also see Trac ticket #91. + Be alarmed though, that this messes up transactions. Fortunately, + Mutalyzer doesn't use transactions at the moment. + Additionally, this feature has been removed from the MySQLdb + library starting from Debian Wheezy. Again, see #91. + """ + kwargs = dict(user=mySqlUser, db=dbName, host=mySqlHost) + if config.get('autoReconnect'): + kwargs.update(reconnect=True) + + self.__db = MySQLdb.connect(**kwargs) #__init__ def query(self, statement) : diff --git a/mutalyzer/config.py b/mutalyzer/config.py index ae7f8cb0..77254bf2 100644 --- a/mutalyzer/config.py +++ b/mutalyzer/config.py @@ -113,8 +113,9 @@ class _Config(): # incorrect values upon instantiation. # A few 'special' values. - self._values = {'debug': config.as_bool('debug'), - 'threshold': float(config['threshold'])} + self._values = {'autoReconnect': config.as_bool('autoReconnect'), + 'debug': config.as_bool('debug'), + 'threshold': float(config['threshold'])} # Simple string values. for name in ('email', 'cache', 'lrgurl', 'internalDb', 'dbNames', -- GitLab