-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Give more specific messages if a job was killed due to SIGTERM or SIGKILL signals #12435
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5046f02
Reap jobs on dispatcher startup to increase clarity
AlanCoding 863d6d4
Exit jobs if receiving SIGTERM signal
AlanCoding 62b0ebf
Fix unwanted reaping on shutdown, let subprocess close out
AlanCoding e4a5e4a
Back out the exception on receiving signals
AlanCoding 01d948c
Replace existing reaping logic with the new startup reaper
AlanCoding 673c5fa
Add some sanity tests for signal module
AlanCoding 84c62d5
Only log reaped jobs when jobs were reaped
AlanCoding cc58c51
Add a log for an unhandled dispatcher error
AlanCoding 54265fd
Refine wording of error messages
AlanCoding 9ec335d
Fix syntax bug
AlanCoding 14a676e
Change wording for reaping of jobs on startup
AlanCoding 638b468
Remove a word from the error message
AlanCoding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,8 +169,9 @@ def run(self, *args, **kwargs): | |
logger.exception(f"Error consuming new events from postgres, will retry for {self.pg_max_wait} s") | ||
self.pg_down_time = time.time() | ||
self.pg_is_down = True | ||
if time.time() - self.pg_down_time > self.pg_max_wait: | ||
logger.warning(f"Postgres event consumer has not recovered in {self.pg_max_wait} s, exiting") | ||
current_downtime = time.time() - self.pg_down_time | ||
if current_downtime > self.pg_max_wait: | ||
logger.exception(f"Postgres event consumer has not recovered in {current_downtime} s, exiting") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise | ||
# Wait for a second before next attempt, but still listen for any shutdown signals | ||
for i in range(10): | ||
|
@@ -179,6 +180,10 @@ def run(self, *args, **kwargs): | |
time.sleep(0.1) | ||
for conn in db.connections.all(): | ||
conn.close_if_unusable_or_obsolete() | ||
except Exception: | ||
# Log unanticipated exception in addition to writing to stderr to get timestamps and other metadata | ||
logger.exception('Encountered unhandled error in dispatcher main loop') | ||
raise | ||
|
||
|
||
class BaseWorker(object): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import signal | ||
import functools | ||
import logging | ||
|
||
|
||
logger = logging.getLogger('awx.main.tasks.signals') | ||
|
||
|
||
__all__ = ['with_signal_handling', 'signal_callback'] | ||
|
||
|
||
class SignalState: | ||
def reset(self): | ||
self.sigterm_flag = False | ||
self.is_active = False | ||
self.original_sigterm = None | ||
self.original_sigint = None | ||
|
||
def __init__(self): | ||
self.reset() | ||
|
||
def set_flag(self, *args): | ||
"""Method to pass into the python signal.signal method to receive signals""" | ||
self.sigterm_flag = True | ||
|
||
def connect_signals(self): | ||
self.original_sigterm = signal.getsignal(signal.SIGTERM) | ||
self.original_sigint = signal.getsignal(signal.SIGINT) | ||
signal.signal(signal.SIGTERM, self.set_flag) | ||
signal.signal(signal.SIGINT, self.set_flag) | ||
self.is_active = True | ||
|
||
def restore_signals(self): | ||
signal.signal(signal.SIGTERM, self.original_sigterm) | ||
signal.signal(signal.SIGINT, self.original_sigint) | ||
self.reset() | ||
|
||
|
||
signal_state = SignalState() | ||
|
||
|
||
def signal_callback(): | ||
return signal_state.sigterm_flag | ||
|
||
|
||
def with_signal_handling(f): | ||
""" | ||
Change signal handling to make signal_callback return True in event of SIGTERM or SIGINT. | ||
""" | ||
|
||
@functools.wraps(f) | ||
def _wrapped(*args, **kwargs): | ||
try: | ||
this_is_outermost_caller = False | ||
if not signal_state.is_active: | ||
signal_state.connect_signals() | ||
this_is_outermost_caller = True | ||
return f(*args, **kwargs) | ||
finally: | ||
if this_is_outermost_caller: | ||
signal_state.restore_signals() | ||
|
||
return _wrapped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import signal | ||
|
||
from awx.main.tasks.signals import signal_state, signal_callback, with_signal_handling | ||
|
||
|
||
def test_outer_inner_signal_handling(): | ||
""" | ||
Even if the flag is set in the outer context, its value should persist in the inner context | ||
""" | ||
|
||
@with_signal_handling | ||
def f2(): | ||
assert signal_callback() | ||
|
||
@with_signal_handling | ||
def f1(): | ||
assert signal_callback() is False | ||
signal_state.set_flag() | ||
assert signal_callback() | ||
f2() | ||
|
||
original_sigterm = signal.getsignal(signal.SIGTERM) | ||
assert signal_callback() is False | ||
f1() | ||
assert signal_callback() is False | ||
assert signal.getsignal(signal.SIGTERM) is original_sigterm | ||
|
||
|
||
def test_inner_outer_signal_handling(): | ||
""" | ||
Even if the flag is set in the inner context, its value should persist in the outer context | ||
""" | ||
|
||
@with_signal_handling | ||
def f2(): | ||
assert signal_callback() is False | ||
signal_state.set_flag() | ||
assert signal_callback() | ||
|
||
@with_signal_handling | ||
def f1(): | ||
assert signal_callback() is False | ||
f2() | ||
assert signal_callback() | ||
|
||
original_sigterm = signal.getsignal(signal.SIGTERM) | ||
assert signal_callback() is False | ||
f1() | ||
assert signal_callback() is False | ||
assert signal.getsignal(signal.SIGTERM) is original_sigterm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍰