diff --git a/monasca_notification/common/repositories/mysql/mysql_repo.py b/monasca_notification/common/repositories/mysql/mysql_repo.py index d9e857e..7a29d94 100644 --- a/monasca_notification/common/repositories/mysql/mysql_repo.py +++ b/monasca_notification/common/repositories/mysql/mysql_repo.py @@ -108,7 +108,17 @@ class MysqlRepo(BaseRepo): cur = self._mysql.cursor() cur.executemany(self._insert_notification_types_sql, notification_types) + except pymysql.IntegrityError as ignoredException: + # If multiple instances of the notification engine tries to write the + # same content at the same time, only one of them will succeed and others will + # get duplicate primary key, integrity error. We can safely ignore this error. + # This may happen only during the first start when the tables are empty. + code, mesg = ignoredException.args + if code == pymysql.constants.ER.DUP_ENTRY: + log.debug("Notification type exists in DB. Ignoring the exception {}".format(mesg)) + else: + raise exc.DatabaseException(ignoredException) except pymysql.Error as e: self._mysql = None log.exception("Couldn't insert notification types %s", e) - raise exc.DatabaseException(e) \ No newline at end of file + raise exc.DatabaseException(e)