Skip to content

Commit b2e086d

Browse files
author
Erik Öqvist
committed
Add default values and catch InterfaceError exception in kafka_producer
1 parent a15a690 commit b2e086d

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

nipap/nipap/kafka_producer.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ def _create_kafka_producer(cfg):
6161
return None
6262

6363
brokers = None
64-
try:
65-
if cfg.has_option('kafka', 'brokers'):
66-
brokers = cfg.get('kafka', 'brokers')
67-
except Exception:
68-
brokers = None
64+
if cfg.has_option('kafka', 'brokers'):
65+
brokers = cfg.get('kafka', 'brokers')
6966

7067
if not brokers:
7168
LOG.error("No 'kafka.brokers' configured, kafka producer disabled")
@@ -213,21 +210,8 @@ def run(config_path=None):
213210

214211
LOG.info("Starting kafka producer (config: %s)", config_path)
215212

216-
# poll interval seconds
217-
poll_interval = 2
218-
try:
219-
if cfg.has_option('kafka', 'poll_interval'):
220-
poll_interval = int(cfg.get('kafka', 'poll_interval'))
221-
except Exception:
222-
poll_interval = 2
223-
224-
# topic prefix default
225-
topic_prefix = "nipap."
226-
try:
227-
if cfg.has_option('kafka', 'topic_prefix'):
228-
topic_prefix = cfg.get('kafka', 'topic_prefix')
229-
except Exception:
230-
topic_prefix = "nipap."
213+
poll_interval = int(cfg.get('kafka', 'poll_interval'))
214+
topic_prefix = cfg.get('kafka', 'topic_prefix')
231215

232216
conn = _connect_db(cfg)
233217
cur = conn.cursor()
@@ -331,6 +315,17 @@ def run(config_path=None):
331315
else:
332316
conn.rollback()
333317

318+
except psycopg2.InterfaceError as e:
319+
LOG.error("Database interface error in kafka_producer loop: %s. Reconnecting to database.", e)
320+
try:
321+
conn.close()
322+
except Exception:
323+
pass
324+
# reconnect to database and update cursor
325+
conn = _connect_db(cfg)
326+
cur = conn.cursor()
327+
# backoff before next attempt
328+
time.sleep(max(1, poll_interval))
334329
except Exception as e:
335330
LOG.exception("Unexpected error in kafka_producer loop: %s", e)
336331
try:

nipap/nipap/nipapconfig.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'db_sslmode': 'require',
2020
'auth_cache_timeout': '3600',
2121
'user': '',
22-
'group': ''
22+
'group': '',
2323
}
2424

2525

@@ -50,6 +50,8 @@ def __init__(self, cfg_path=None):
5050

5151
self.read_config_file()
5252

53+
self.set_default_values_in_sections()
54+
5355
def read_config_file(self):
5456
""" Read the configuration file
5557
"""
@@ -64,6 +66,11 @@ def read_config_file(self):
6466
except IOError as exc:
6567
raise NipapConfigError(str(exc))
6668

69+
def set_default_values_in_sections(self):
70+
if 'kafka' in self:
71+
self['kafka'].setdefault('poll_interval', '2')
72+
self['kafka'].setdefault('topic_prefix', 'nipap.')
73+
6774

6875
class NipapConfigError(Exception):
6976
pass

0 commit comments

Comments
 (0)