Merge pull request #2102 from balena-os/supervisor-sigterm

Add SIGTERM listener on supervisor start
This commit is contained in:
Balena CI 2023-01-31 21:41:31 +02:00 committed by GitHub
commit 11952fd266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 9 deletions

View File

@ -53,7 +53,7 @@ interface DnsLookupOpts {
* and on openBalena setups, this extra code will perform
* the lookup instead of passing it to the built-in
* function.
================================================== */
================================================== */
if (name && name.endsWith('.local')) {
// determine which resolvers to use...
const getResolvers = () => {
@ -123,6 +123,17 @@ interface DnsLookupOpts {
import '@balena/happy-eyeballs/eye-patch';
import Supervisor from './supervisor';
import * as process from 'process';
import log from './lib/supervisor-console';
// Register signal handlers before starting the supervisor service
process.on('SIGTERM', () => {
log.info('Received SIGTERM. Exiting.');
// This is standard exit code to indicate a graceful shutdown
// it equals 128 + 15 (the signal code)
process.exit(143);
});
const supervisor = new Supervisor();
supervisor.init();

View File

@ -72,21 +72,18 @@ set_abort_timer() {
kill -USR2 "$2"
}
# Flag indicating whether the required services are ready
ready=0
abort_if_not_ready() {
# If the timeout is reached and the required services are not ready, it probably
# means something went wrong so we terminate the program with an error
if [ "${ready}" = "0" ]; then
echo "Something happened, failed to start in ${timeout}s" >&2
exit 1
fi
echo "Something happened, failed to start in ${timeout}s" >&2
exit 1
}
# Trap the signal and start the timer if user timeout is greater than 0
if [ "$timeout" -gt 0 ]; then
trap 'abort_if_not_ready' USR2
set_abort_timer "$timeout" $$ &
timer_pid=$!
fi
# Wait for docker
@ -110,7 +107,9 @@ if [ "${with_supervisor}" = "1" ]; then
done
fi
# Ignore signal if received
ready=1
# Kill the timer since we are ready to start
if [ "$timer_pid" != "" ]; then
kill $timer_pid
fi
exec ${cmd}