Skip to content

Commit

Permalink
feat: install signal handler for graceful shutdown (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
starsz committed Nov 13, 2020
1 parent e08f3ab commit 1c05691
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
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()
}
53 changes: 42 additions & 11 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,31 +47,49 @@ go build -o ./manager-api .
sleep 3
pkill -f manager-api

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

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

#change level and path
clean_logfile

#change level

sed -i 's/file_path: logs\/error.log/file_path: .\/error.log/' conf/conf.yaml
sed -i 's/warn/info/' conf/conf.yaml

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

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

if [[ `grep -c "INFO" ${logfile}` -eq '0' ]]; then
echo "failed: failed to write log on right level"
exit 1
fi

clean_logfile

# test signal

./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 "INFO" ./error.log` -eq '0' ]]; then
echo "failed: failed to write log on right level"
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

clean_logfile

0 comments on commit 1c05691

Please sign in to comment.