Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions components/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions tests/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import copy
import random
import unittest
from unittest import mock

import pymysql

sys.path.append(".")
sys.path.append("..")
Expand Down Expand Up @@ -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)
Loading