Reputation: 105
I have a python script running inside a container, sometime it fails. I would like to know if it is possible to create a healthcheck for that script and in case of failure delete this container and raise a new one.
Upvotes: 3
Views: 3000
Reputation: 105
I eventually modified this part
if not self.setOutputSyslogHandler:
syslogger.addHandler(syslog)
self.setOutputSyslogHandler = True
I modified it to remove the handler and add it every time it uses it again.
syslogger.addHandler(syslog)
for msg in decrypted_file.splitlines():
if msg != '':
try:
syslogger.info(msg)
except:
self.logger.error('Error sending log file to syslog server %s on port %s via protocol %s', self.config.SYSLOG_ADDRESS, self.config.SYSLOG_PORT, self.config.SYSLOG_PROTO)
self.running = False
sys.exit(1)
while syslogger.hasHandlers():
syslogger.removeHandler(syslogger.handlers[0])
Upvotes: 1
Reputation: 819
I would suggest editing the log downloader to ensure it exists,
... # line 255 ( https://github.com/imperva/incapsula-logs-downloader/blob/master/script/LogsDownloader.py )
"""
Saves the decrypted file content to a log file in the filesystem
"""
def handle_log_decrypted_content(self, filename, decrypted_file):
decrypted_file = decrypted_file.decode('utf-8')
if self.config.SYSLOG_ENABLE == 'YES':
syslogger = logging.getLogger("syslog")
syslogger.setLevel(logging.INFO)
if self.config.SYSLOG_PROTO == 'TCP':
self.logger.info('Syslog enabled, using TCP')
syslog = logging.handlers.SysLogHandler(address=(self.config.SYSLOG_ADDRESS, int(self.config.SYSLOG_PORT)), socktype=socket.SOCK_STREAM)
else:
self.logger.info('Syslog enabled, using UDP')
syslog = logging.handlers.SysLogHandler(address=(self.config.SYSLOG_ADDRESS, int(self.config.SYSLOG_PORT)))
### Add by Maytee Sittipornchaisakul
if not self.setOutputSyslogHandler:
syslogger.addHandler(syslog)
self.setOutputSyslogHandler = True
for msg in decrypted_file.splitlines():
if msg != '':
try:
syslogger.info(msg)
except:
self.logger.error('Error sending log file to syslog server %s on port %s via protocol %s', self.config.SYSLOG_ADDRESS, self.config.SYSLOG_PORT, self.config.SYSLOG_PROTO)
self.running = False
sys.exit(1)
if self.config.SAVE_LOCALLY == "YES":
local_file = open(self.config.PROCESS_DIR + filename, "a+")
local_file.writelines(decrypted_file)
...
This will hopefully cause the script to exit when syslog errors, you can then handle the exited container with some kind of watchdog such as the --restart option discussed in chat.
Upvotes: 1