chore: graceful restart or stop

pull/1604/head
Noah Hsu 2022-08-28 15:34:12 +08:00
parent 5ab5cc327f
commit a6f3094c9a
2 changed files with 39 additions and 8 deletions

View File

@ -47,7 +47,7 @@ jobs:
git add . git add .
git config --local user.email "i@nn.ci" git config --local user.email "i@nn.ci"
git config --local user.name "Noah Hsu" git config --local user.name "Noah Hsu"
git commit -m "chore: update i18n file" -a git commit -m "chore: auto update i18n file" -a
cd .. cd ..
- name: Push lang files - name: Push lang files

View File

@ -1,7 +1,13 @@
package cmd package cmd
import ( import (
"context"
"fmt" "fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/alist-org/alist/v3/cmd/flags" "github.com/alist-org/alist/v3/cmd/flags"
_ "github.com/alist-org/alist/v3/drivers" _ "github.com/alist-org/alist/v3/drivers"
@ -31,15 +37,40 @@ the address is defined in config file`,
server.Init(r) server.Init(r)
base := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port) base := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port)
log.Infof("start server @ %s", base) log.Infof("start server @ %s", base)
var err error srv := &http.Server{Addr: base, Handler: r}
if conf.Conf.Scheme.Https { go func() {
err = r.RunTLS(base, conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile) var err error
} else { if conf.Conf.Scheme.Https {
err = r.Run(base) //err = r.RunTLS(base, conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile)
err = srv.ListenAndServeTLS(conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile)
} else {
err = srv.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
log.Errorf("failed to start: %s", err.Error())
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
} }
if err != nil { // catching ctx.Done(). timeout of 3 seconds.
log.Errorf("failed to start: %s", err.Error()) select {
case <-ctx.Done():
log.Println("timeout of 3 seconds.")
} }
log.Println("Server exiting")
}, },
} }