diff --git a/extras/config.example b/extras/config.example index df98ac593175f27d2f54ab1d991a966b768f583c..e77c0b21bfe700649866f0d0cc460bc2a0dbc27c 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 0000000000000000000000000000000000000000..7b4512b757dd1c387f1fd6cbbfcef2ff974fa469 --- /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 fbd6c91becaebde96e20ada12f189b9c71967595..2e23fbcab718fe83c5c8d3c4ee4f35f1f9f3a808 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 ae7f8cb0d6520485bf185b0fe07a00dc11c08d58..77254bf243bd588fee0e529f22646cdeab87153c 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',