diff --git a/components/db.py b/components/db.py index d23e5609..e3fb2a2f 100644 --- a/components/db.py +++ b/components/db.py @@ -4,7 +4,7 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -from components.utilities import Struct +from components.utilities import Struct, retry from components.logging import logEntryExit from components.providerbase import BaseProvider, INeedsLoggingProvider from components.logging import LogLevel @@ -138,6 +138,19 @@ # ================================================================================== +# On Taskcluster, run.py starts cloud_sql_proxy just before Updatebot, and it +# isn't always accepting connections yet, so retry for about half a minute. +@retry(times=6, sleep_s=1, exp=2, exceptions=(pymysql.err.OperationalError,)) +def _connect(database_config): + return pymysql.connect( + host=database_config['host'], + user=database_config['user'], + password=database_config['password'], + charset='utf8', + cursorclass=pymysql.cursors.DictCursor, + autocommit=True) + + class MySQLDatabase(BaseProvider, INeedsLoggingProvider): def __init__(self, database_config): pymysql.converters.encoders[JOBSTATUS] = lambda x, y=None: pymysql.converters.escape_int(x.value) @@ -148,13 +161,7 @@ def __init__(self, database_config): self._successfully_created_tmp_db = False self.database_config = database_config - self.connection = pymysql.connect( - host=database_config['host'], - user=database_config['user'], - password=database_config['password'], - charset='utf8', - cursorclass=pymysql.cursors.DictCursor, - autocommit=True) + self.connection = _connect(database_config) if database_config.get('use_tmp_db', False): with self.connection.cursor() as cursor: diff --git a/tests/database.py b/tests/database.py index 0fcff619..882d0d3e 100755 --- a/tests/database.py +++ b/tests/database.py @@ -9,6 +9,9 @@ import copy import random import unittest +from unittest import mock + +import pymysql sys.path.append(".") sys.path.append("..") @@ -80,5 +83,27 @@ def testJobs(self): self.db.delete_job(job_id=newJob.id) +class TestDatabaseConnection(unittest.TestCase): + def testRetriesWhileTheServerIsNotUpYet(self): + # Like cloud_sql_proxy not listening yet: the first attempts are refused. + real_connect = pymysql.connect + calls = [] + + def connect(*args, **kwargs): + calls.append(1) + if len(calls) < 3: + raise pymysql.err.OperationalError(2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)") + return real_connect(*args, **kwargs) + + db_config = transform_db_config_to_tmp_db(localconfig['Database']) + with mock.patch("components.db.pymysql.connect", side_effect=connect), mock.patch("components.utilities.time.sleep"): + db = DatabaseProvider(db_config) + db.update_config(SimpleLoggerConfig) + self.assertEqual(len(calls), 3) + db.check_database() + self.assertTrue(db.updatebot_is_enabled()) + del db + + if __name__ == '__main__': unittest.main(verbosity=0)