mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-23 01:20:11 +00:00
Safe shutdown
This commit is contained in:
parent
8a8fe65192
commit
39bf3ad7f1
10
api.go
10
api.go
@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/lithammer/shortuuid/v3"
|
"github.com/lithammer/shortuuid/v3"
|
||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
"os"
|
"os"
|
||||||
//"os/exec"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -583,7 +583,7 @@ func (ctx *appContext) ModifyConfig(gc *gin.Context) {
|
|||||||
gc.JSON(200, map[string]bool{"success": true})
|
gc.JSON(200, map[string]bool{"success": true})
|
||||||
if req["restart-program"].(bool) {
|
if req["restart-program"].(bool) {
|
||||||
ctx.info.Println("Restarting...")
|
ctx.info.Println("Restarting...")
|
||||||
err := Restart()
|
err := ctx.Restart()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.err.Printf("Couldn't restart, try restarting manually. (%s)", err)
|
ctx.err.Printf("Couldn't restart, try restarting manually. (%s)", err)
|
||||||
}
|
}
|
||||||
@ -634,10 +634,11 @@ func (ctx *appContext) ModifyConfig(gc *gin.Context) {
|
|||||||
// panic(fmt.Errorf("restarting"))
|
// panic(fmt.Errorf("restarting"))
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func Restart() error {
|
func (ctx *appContext) Restart() error {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
os.Exit(0)
|
signal.Notify(ctx.quit, os.Interrupt)
|
||||||
|
<-ctx.quit
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
args := os.Args
|
args := os.Args
|
||||||
@ -648,7 +649,6 @@ func Restart() error {
|
|||||||
os.Setenv("JFA_EXEC", args[0])
|
os.Setenv("JFA_EXEC", args[0])
|
||||||
}
|
}
|
||||||
env := os.Environ()
|
env := os.Environ()
|
||||||
fmt.Printf("EXECUTABLE: %s\n", os.Getenv("JFA_EXEC"))
|
|
||||||
err := syscall.Exec(os.Getenv("JFA_EXEC"), []string{""}, env)
|
err := syscall.Exec(os.Getenv("JFA_EXEC"), []string{""}, env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
25
main.go
25
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -14,7 +15,9 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -48,6 +51,7 @@ type appContext struct {
|
|||||||
host string
|
host string
|
||||||
port int
|
port int
|
||||||
version string
|
version string
|
||||||
|
quit chan os.Signal
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateSecret(length int) (string, error) {
|
func GenerateSecret(length int) (string, error) {
|
||||||
@ -319,5 +323,24 @@ func main() {
|
|||||||
router.POST("/modifyConfig", ctx.ModifyConfig)
|
router.POST("/modifyConfig", ctx.ModifyConfig)
|
||||||
ctx.info.Printf("Loading setup @ %s", address)
|
ctx.info.Printf("Loading setup @ %s", address)
|
||||||
}
|
}
|
||||||
router.Run(address)
|
// router.Run(address)
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: address,
|
||||||
|
Handler: router,
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
if err := srv.ListenAndServe(); err != nil {
|
||||||
|
ctx.err.Printf("Failure serving: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
ctx.quit = make(chan os.Signal)
|
||||||
|
signal.Notify(ctx.quit, os.Interrupt)
|
||||||
|
<-ctx.quit
|
||||||
|
ctx.info.Println("Shutting down...")
|
||||||
|
|
||||||
|
cntx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
|
defer cancel()
|
||||||
|
if err := srv.Shutdown(cntx); err != nil {
|
||||||
|
ctx.err.Fatalf("Server shutdown error: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user