Skip to content

Commit 31d90fc

Browse files
committed
add sigterm sigint handling
1 parent c931361 commit 31d90fc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

middleware/consumer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import signal
34
import time
45
import sys
56
import boto3
@@ -135,6 +136,26 @@ def go():
135136
log.info('Spinning up messages generator')
136137
msgs = get_msgs(sqs, Q_URL)
137138

139+
receipt_handle = None
140+
141+
# Orderly clean-up logic if process is suddenly shut down.
142+
def clean_up(signum, frm):
143+
'''Attempt to cleanly reset the visiblity of the current in-flight
144+
message before exiting. (It's possible we are exiting after
145+
a message was deleted but the next has not yet been retrieved; that's ok'''
146+
nonlocal sqs
147+
nonlocal receipt_handle
148+
log.info('***************************')
149+
log.info('SIGINT or SIGTERM received.')
150+
log.info('***************************')
151+
try:
152+
make_msg_visible(sqs, Q_URL, receipt_handle)
153+
except Exception as ex:
154+
log.error(ex)
155+
sys.exit(0)
156+
signal.signal(signal.SIGINT, clean_up)
157+
signal.signal(signal.SIGTERM, clean_up)
158+
138159
# Senzing init tasks.
139160
sz_eng = None
140161
try:

0 commit comments

Comments
 (0)