Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 8917c35

Browse files
committed
Gracefully handle SIGTERM signals
Add checking for termination from file
1 parent 151b6e4 commit 8917c35

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

tap_postgres/sync_strategies/logical_replication.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616
import json
1717
import re
1818
import time
19+
import signal
20+
from os.path import exists
1921

2022
LOGGER = singer.get_logger()
21-
2223
UPDATE_BOOKMARK_PERIOD = 1000
23-
2424
COUNTER_PRINT_PERIOD = 10000
25+
COUNTER = {'U': 0, 'D': 0, 'I': 0, 'json_load': 0, 'read_message': 0, 'send_message': 0}
26+
SIGTERM_RECEIVED = False
27+
28+
29+
def on_sigterm_received():
30+
SIGTERM_RECEIVED = True
31+
32+
signal.signal(signal.SIGTERM, on_sigterm_received)
2533

26-
COUNTER={'U': 0, 'D': 0, 'I': 0, 'json_load': 0, 'read_message': 0, 'send_message': 0}
2734

2835
def get_pg_version(cur):
2936
cur.execute("SELECT version()")
@@ -464,6 +471,14 @@ def sync_tables(conn_info, logical_streams, state, end_lsn):
464471
rows_saved = 0
465472
idle_count = 0
466473
while True:
474+
if exists('/tmp/terminating_pod'):
475+
LOGGER.info("SIGTERM received from file '/tmp/terminating_pod'. Exiting")
476+
break
477+
478+
if SIGTERM_RECEIVED:
479+
LOGGER.info("SIGTERM received from parent process. Exiting")
480+
break
481+
467482
poll_duration = (datetime.datetime.now() - begin_ts).total_seconds()
468483
if poll_duration > poll_total_seconds:
469484
LOGGER.info("breaking after %s seconds of polling with no data", poll_duration)

0 commit comments

Comments
 (0)