Skip to content

Commit

Permalink
Merge pull request #4 from a-thomas-22/sigterm-handler
Browse files Browse the repository at this point in the history
Improve SIGTERM handling
  • Loading branch information
thedatabaseme authored May 17, 2023
2 parents c59f8d5 + c4ac072 commit ea740a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import subprocess
import json
import datetime
import signal

from prometheus_client import start_http_server, Gauge
from psycopg2.extras import DictCursor
Expand All @@ -20,6 +21,13 @@
else:
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)

terminate = False;

def signal_handler(sig, frame):
global terminate
logging.info('SIGTERM received, preparing to shut down...')
terminate = True

# Class definition
class Exporter():
def __init__(self):
Expand Down Expand Up @@ -207,6 +215,9 @@ def get_archive_status(self):
logging.info("Startup...")
logging.info('My PID is: %s', os.getpid())

# Register the signal handler for SIGTERM
signal.signal(signal.SIGTERM, signal_handler)

logging.info("Reading environment configuration")

# Read the configuration
Expand All @@ -232,6 +243,11 @@ def get_archive_status(self):
# Check if this is a primary instance
# with while True and try catch this is how reconnect already should work.
while True:

if terminate:
logging.info("Received SIGTERM, shutting down...")
break

try:
with psycopg2.connect(
host = pg_host,
Expand Down Expand Up @@ -283,6 +299,9 @@ def get_archive_status(self):
raise Exception(
"Unable to execute SELECT NOT pg_is_in_recovery()" + str(e))
except Exception as e:
if terminate:
logging.info("Received SIGTERM during exception, shutting down...")
break
logging.error(
"Error occured, retrying in 60sec..." + str(e))
time.sleep(60)
2 changes: 1 addition & 1 deletion scripts/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash

sleep 5
envdir /run/etc/wal-e.d/env wal-g-prometheus-exporter
exec envdir /run/etc/wal-e.d/env wal-g-prometheus-exporter

0 comments on commit ea740a9

Please sign in to comment.