Skip to content

Commit

Permalink
解决caddy重载可能出现卡死的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Jul 28, 2019
1 parent 0c72d91 commit 4ca46b6
Show file tree
Hide file tree
Showing 202 changed files with 787 additions and 27,242 deletions.
63 changes: 49 additions & 14 deletions application/library/caddy/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package caddy

import (
"bufio"
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -54,6 +55,7 @@ var (
PidFile: `./caddy.pid`,
}
DefaultVersion = `2.0.0`
EnableReload = true
)

func TrapSignals() {
Expand Down Expand Up @@ -97,6 +99,7 @@ func Fixed(c *Config) {
c.appName = `nging`
c.appVersion = echo.String(`VERSION`, DefaultVersion)
c.Agreed = true
c.ctx, c.cancel = context.WithCancel(context.Background())
}

type Config struct {
Expand Down Expand Up @@ -124,6 +127,12 @@ type Config struct {
appName string
instance *caddy.Instance
stopped bool
ctx context.Context
cancel context.CancelFunc
}

func now() string {
return time.Now().Format(`2006-01-02 15:04:05`)
}

func (c *Config) Start() error {
Expand All @@ -140,36 +149,58 @@ func (c *Config) Start() error {
if err != nil {
return err
}

if EnableReload {
c.watchingSignal()
}

// Start your engines
c.instance, err = caddy.Start(caddyfile)
if err != nil {
return err
}

// Execute instantiation events
caddy.EmitEvent(caddy.InstanceStartupEvent, c.instance)
msgbox.Success(`Caddy`, `Server has been successfully started at `+now())

// Listen to keypress of "return" and restart the app automatically
// Twiddle your thumbs
c.instance.Wait()
return nil
}

// Listen to keypress of "return" and restart the app automatically
func (c *Config) watchingSignal() {
debug := false
go func() {
if debug {
msgbox.Info(`Caddy`, `listen return ==> `+now())
}
in := bufio.NewReader(os.Stdin)
for {
input, _ := in.ReadString('\n')
if input == "\n" || input == "\r\n" {
c.Restart()
}
if c.stopped {
break
select {
case <-c.ctx.Done():
return
default:
if debug {
msgbox.Info(`Caddy`, `reading ==> `+now())
}
input, _ := in.ReadString(com.LF)
if input == com.StrLF || input == com.StrCRLF {
if debug {
msgbox.Info(`Caddy`, `restart ==> `+now())
}
c.Restart()
} else {
if debug {
msgbox.Info(`Caddy`, `waiting ==> `+now())
}
}
}
}
}()

// Twiddle your thumbs
c.instance.Wait()
return nil
}

func (c *Config) Restart() error {
msgbox.Info(`Information`, `Caddy Server has been successfully reloaded at `+time.Now().Format(`2006-01-02 15:04:05`))
defer msgbox.Success(`Caddy`, `Server has been successfully reloaded at `+now())
if c.instance == nil {
return nil
}
Expand All @@ -187,6 +218,10 @@ func (c *Config) Restart() error {

func (c *Config) Stop() error {
c.stopped = true
defer func() {
c.cancel()
msgbox.Success(`Caddy`, `Server has been successfully stopped at `+now())
}()
if c.instance == nil {
return nil
}
Expand Down
6 changes: 5 additions & 1 deletion application/library/config/cliconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ func (c *CLIConfig) CmdSendSignal(typeName string, sig os.Signal) error {
if cmd.Process == nil {
return nil
}
return cmd.Process.Signal(sig)
err := cmd.Process.Signal(sig)
if err != nil && (cmd.ProcessState == nil || cmd.ProcessState.Exited()) {
err = nil
}
return err
}

func (c *CLIConfig) Kill(cmd *exec.Cmd) error {
Expand Down
14 changes: 9 additions & 5 deletions application/library/config/cliconfig_caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"

"github.com/admpub/log"
"github.com/admpub/nging/application/library/caddy"
"github.com/webx-top/com"
)

Expand All @@ -39,8 +40,12 @@ func (c *CLIConfig) CaddyStart(writer ...io.Writer) (err error) {
log.Error(err.Error())
}
params := []string{os.Args[0], `--config`, c.Conf, `--type`, `webserver`}
c.caddyCh = com.NewCmdChanReader()
c.cmds["caddy"] = com.RunCmdWithReaderWriter(params, c.caddyCh, writer...)
if caddy.EnableReload {
c.caddyCh = com.NewCmdChanReader()
c.cmds["caddy"] = com.RunCmdWithReaderWriter(params, c.caddyCh, writer...)
} else {
c.cmds["caddy"] = com.RunCmdWithWriter(params, writer...)
}
return nil
}

Expand All @@ -54,14 +59,13 @@ func (c *CLIConfig) CaddyStop() error {
return c.CmdStop("caddy")
}

var eol = []byte("\n")

func (c *CLIConfig) CaddyReload() error {
if c.caddyCh == nil || com.IsWindows { //windows不支持重载,需要重启
return c.CaddyRestart()
}
c.caddyCh.Send(eol)
c.caddyCh.Send(com.BreakLine)
return nil
//c.CmdSendSignal("caddy", os.Interrupt)
}

func (c *CLIConfig) CaddyRestart(writer ...io.Writer) error {
Expand Down
28 changes: 25 additions & 3 deletions application/library/msgbox/msgbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ package msgbox

import (
"fmt"
"io"
"os"
"strings"

isatty "github.com/admpub/go-isatty"
"github.com/admpub/go-pretty/table"
"github.com/admpub/go-pretty/text"
)
Expand All @@ -47,14 +49,34 @@ func Debug(title, content string, args ...interface{}) {
Render(title, content, `debug`, args...)
}

func Colorable(w io.Writer) bool {
file, ok := w.(*os.File)
if !ok {
return false
}
if isatty.IsTerminal(file.Fd()) {
return true
}
if isatty.IsCygwinTerminal(file.Fd()) {
return true
}
return false
}

var StdoutColorable = Colorable(os.Stdout)

func Render(title, content, typ string, args ...interface{}) {
if len(args) > 0 {
content = fmt.Sprintf(content, args...)
}
if !StdoutColorable {
os.Stdout.WriteString(`[` + strings.ToUpper(typ) + `][` + title + `] ` + content + "\n")
return
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{title})
t.AppendRow([]interface{}{""})
if len(args) > 0 {
content = fmt.Sprintf(content, args...)
}
for _, row := range strings.Split(content, "\n") {
t.AppendRow([]interface{}{row})
}
Expand Down
9 changes: 9 additions & 0 deletions vendor/github.com/admpub/go-isatty/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions vendor/github.com/admpub/go-isatty/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/admpub/go-isatty/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/admpub/go-isatty/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/admpub/go-isatty/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/admpub/go-isatty/isatty_android.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions vendor/github.com/admpub/go-isatty/isatty_bsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/admpub/go-isatty/isatty_others.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/admpub/go-isatty/isatty_solaris.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4ca46b6

Please sign in to comment.