Skip to content
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

feat: install signal handler for graceful shutdown #796

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
package main

import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/apisix/manager-api/conf"
Expand Down Expand Up @@ -51,9 +55,27 @@ func main() {

log.Infof("The Manager API is listening on %s ", addr)

if err := s.ListenAndServe(); err != nil {
log.Errorf("listen and serv fail: %w", err)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
utils.CloseAll()
log.Fatalf("listen and serv fail: %w", err)
}
}()

sig := <-quit
log.Infof("The Manager API server receive %s and start shutting down", sig.String())

ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()

if err := s.Shutdown(ctx); err != nil {
log.Errorf("Shutting down server error: %w", err)
}

log.Infof("The Manager API server exited")

utils.CloseAll()
}
51 changes: 41 additions & 10 deletions api/test/shell/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ clean_up() {
git checkout conf/conf.yaml
}

logfile="./logs/error.log"

check_logfile() {
if [[ ! -f $logfile ]]; then
echo "failed: failed to write log"
exit 1
fi
}

clean_logfile() {
echo > $logfile
}

trap clean_up EXIT

export GO111MODULE=on
Expand All @@ -34,29 +47,47 @@ go build -o ./manager-api .
sleep 3
pkill -f manager-api

if [[ ! -f "./logs/error.log" ]]; then
echo "failed: failed to write log"
check_logfile

if [[ `grep -c "INFO" ${logfile}` -ne '0' ]]; then
echo "failed: should not write info log when level is warn"
exit 1
fi

if [[ `grep -c "INFO" ./logs/error.log` -ne '0' ]]; then
echo "failed: should not write info log when level is warn"
clean_logfile

# change level and test signal

sed -i 's/level: warn/level: info/' conf/conf.yaml

./manager-api &>/dev/null &
sleep 3
pkill -2 -f manager-api
sleep 6

check_logfile

if [[ `ps -ef | grep "[m]anager-api" -c` -eq '1' ]]; then
echo "failed: the manager server didn't deal with signal in correct way"
exit 1
fi

if [[ `grep -c "server receive interrupt" ${logfile}` -ne '1' ]]; then
echo "failed: the manager server didn't deal with signal in correct way"
exit 1
fi

#change level and path
clean_logfile

#change path

sed -i 's/logs\/error.log/.\/error.log/' conf/conf.yaml
sed -i 's/warn/info/' conf/conf.yaml
membphis marked this conversation as resolved.
Show resolved Hide resolved

./manager-api &
sleep 3
pkill -f manager-api

if [[ ! -f "./error.log" ]]; then
echo "failed: failed to write log"
exit 1
fi
check_logfile

if [[ `grep -c "INFO" ./error.log` -eq '0' ]]; then
echo "failed: failed to write log on right level"
Expand Down