Skip to content

Commit 5249d0e

Browse files
bug: sql mixin: reconnect on connection issues
separate the sql mixin initialization from the __init__ to be able to call it from inside the bot for re-connecting. calling self.__init__ would require at least the bot-id calling self._connect would require preparing all the arguments fixes #2200
1 parent 5980e05 commit 5249d0e

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

intelmq/lib/mixins/sql.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
""" SQLMixin for IntelMQ
22
3-
SPDX-FileCopyrightText: 2021 Birger Schacht
3+
SPDX-FileCopyrightText: 2021 Birger Schacht, 2022 Intevation GmbH
44
SPDX-License-Identifier: AGPL-3.0-or-later
55
66
Based on the former SQLBot base class
@@ -25,6 +25,11 @@ class SQLMixin:
2525
message_jsondict_as_string = True
2626

2727
def __init__(self, *args, **kwargs):
28+
self._init_sql()
29+
30+
super().__init__(*args, **kwargs)
31+
32+
def _init_sql(self):
2833
self.logger.debug("Running SQL Mixin initialization.")
2934
self._engine_name = getattr(self, 'engine', self._default_engine).lower()
3035
engines = {SQLMixin.POSTGRESQL: (self._init_postgresql, "%s"),
@@ -97,14 +102,14 @@ def execute(self, query: str, values: tuple, rollback=False):
97102
except self._engine.OperationalError:
98103
self.logger.exception('Executed rollback command '
99104
'after failed query execution.')
100-
self.init()
105+
self._init_sql()
101106
except Exception:
102107
self.logger.exception('Cursor has been closed, connecting '
103108
'again.')
104-
self.init()
109+
self._init_sql()
105110
else:
106111
self.logger.exception('Database connection problem, connecting again.')
107-
self.init()
112+
self._init_sql()
108113
else:
109114
return True
110115
return False

0 commit comments

Comments
 (0)