From 28825f53954ded0dc10c9cd96c5a044d95e58698 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 9 Dec 2019 21:53:44 +0800 Subject: [PATCH] add gdebug.BuildInfo function; improving gproc --- .example/debug/gdebug/gdebug_info.go | 10 +++++++ .example/os/gproc/gproc2.go | 6 ++-- .example/other/test.go | 1 + .example/other/test2.go | 16 +++++++---- debug/gdebug/gdebug.go | 16 +++++++++++ net/ghttp/ghttp_server.go | 28 +++++++++++-------- .../{gproc_proccess.go => gproc_process.go} | 7 +++++ util/gconv/gconv_struct.go | 2 +- 8 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 .example/debug/gdebug/gdebug_info.go rename os/gproc/{gproc_proccess.go => gproc_process.go} (90%) diff --git a/.example/debug/gdebug/gdebug_info.go b/.example/debug/gdebug/gdebug_info.go new file mode 100644 index 000000000..0e07ca12b --- /dev/null +++ b/.example/debug/gdebug/gdebug_info.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + "github.com/gogf/gf/debug/gdebug" +) + +func main() { + fmt.Println(gdebug.BuildInfo()) +} diff --git a/.example/os/gproc/gproc2.go b/.example/os/gproc/gproc2.go index 5d7ae092f..69464f6b4 100644 --- a/.example/os/gproc/gproc2.go +++ b/.example/os/gproc/gproc2.go @@ -2,16 +2,16 @@ package main import ( "fmt" - "github.com/gogf/gf/os/gproc" ) // 使用gproc kill指定其他进程(清确保运行该程序的用户有足够权限) func main() { - pid := 28536 + pid := 14746 m := gproc.NewManager() m.AddProcess(pid) - m.KillAll() + err := m.KillAll() + fmt.Println(err) m.WaitAll() fmt.Printf("%d was killed\n", pid) } diff --git a/.example/other/test.go b/.example/other/test.go index 16865105d..3f26018b2 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -14,4 +14,5 @@ func main() { }) s.SetPort(8199) s.Run() + } diff --git a/.example/other/test2.go b/.example/other/test2.go index 8a7d1b5fa..bda8f04e5 100644 --- a/.example/other/test2.go +++ b/.example/other/test2.go @@ -1,13 +1,17 @@ package main import ( - "github.com/gogf/gf/frame/g" - "github.com/gogf/gf/text/gregex" + "github.com/gogf/gf/os/gproc" + "log" ) func main() { - s := `-abc` - m, err := gregex.MatchString(`^\-{1,2}a={0,1}(.*)`, s) - g.Dump(err) - g.Dump(m) + process := gproc.NewProcessCmd("go run test.go") + if pid, err := process.Start(); err != nil { + log.Print("Build failed:", err) + return + } else { + log.Printf("Build running: pid: %d", pid) + } + } diff --git a/debug/gdebug/gdebug.go b/debug/gdebug/gdebug.go index 6809ccfa2..4162f9233 100644 --- a/debug/gdebug/gdebug.go +++ b/debug/gdebug/gdebug.go @@ -10,6 +10,7 @@ package gdebug import ( "bytes" "fmt" + "github.com/gogf/gf" "path/filepath" "reflect" "runtime" @@ -28,6 +29,9 @@ const ( ) var ( + buildTime = "" // Binary time string, which is injected from "go build -ldflags '-X importpath.name=value'". + buildGoVersion = "" // Binary go version, which is injected from "go build -ldflags '-X importpath.name=value'". + buildGitCommit = "" // Binary git commit, which is injected from "go build -ldflags '-X importpath.name=value'". goRootForFilter = runtime.GOROOT() // goRootForFilter is used for stack filtering purpose. binaryVersion = "" // The version of current running binary(uint64 hex). binaryVersionMd5 = "" // The version of current running binary(MD5). @@ -39,6 +43,18 @@ func init() { } } +// BuildInfo returns the built information of the binary. +// Note that it should be used with gf-cli tool: gf build, +// which injects necessary information into the binary. +func BuildInfo() map[string]string { + return map[string]string{ + "gf": gf.VERSION, + "go": buildGoVersion, + "git": buildGitCommit, + "time": buildTime, + } +} + // BinVersion returns the version of current running binary. // It uses ghash.BKDRHash+BASE36 algorithm to calculate the unique version of the binary. func BinVersion() string { diff --git a/net/ghttp/ghttp_server.go b/net/ghttp/ghttp_server.go index 98257bbf7..2ca281c70 100644 --- a/net/ghttp/ghttp_server.go +++ b/net/ghttp/ghttp_server.go @@ -63,9 +63,11 @@ type ( // Router item just for dumping. RouterItem struct { + Server string + Address string + Domain string Type int Middleware string - Domain string Method string Route string Priority int @@ -318,7 +320,7 @@ func (s *Server) Start() error { if gproc.IsChild() { gtimer.SetTimeout(2*time.Second, func() { if err := gproc.Send(gproc.PPid(), []byte("exit"), gADMIN_GPROC_COMM_GROUP); err != nil { - glog.Error("[ghttp] server error in process communication:", err) + //glog.Error("[ghttp] server error in process communication:", err) } }) } @@ -347,18 +349,11 @@ func (s *Server) DumpRouterMap() { tablewriter.ALIGN_LEFT, }) - address := s.config.Address - if s.config.HTTPSAddr != "" { - if len(address) > 0 { - address += "," - } - address += "tls" + s.config.HTTPSAddr - } for _, array := range s.GetRouterMap() { data := make([]string, 8) for _, item := range array { - data[0] = s.name - data[1] = address + data[0] = item.Server + data[1] = item.Address data[2] = item.Domain data[3] = item.Method data[4] = gconv.String(len(strings.Split(item.Route, "/")) - 1 + item.Priority) @@ -377,13 +372,22 @@ func (s *Server) DumpRouterMap() { // The key of the returned map is the domain of the server. func (s *Server) GetRouterMap() map[string][]RouterItem { m := make(map[string]*garray.SortedArray) + address := s.config.Address + if s.config.HTTPSAddr != "" { + if len(address) > 0 { + address += "," + } + address += "tls" + s.config.HTTPSAddr + } for k, registeredItems := range s.routesMap { array, _ := gregex.MatchString(`(.*?)%([A-Z]+):(.+)@(.+)`, k) for index, registeredItem := range registeredItems { item := RouterItem{ + Server: s.name, + Address: address, + Domain: array[4], Type: registeredItem.handler.itemType, Middleware: array[1], - Domain: array[4], Method: array[2], Route: array[3], Priority: len(registeredItems) - index - 1, diff --git a/os/gproc/gproc_proccess.go b/os/gproc/gproc_process.go similarity index 90% rename from os/gproc/gproc_proccess.go rename to os/gproc/gproc_process.go index fe6f15589..9cc6b86b0 100644 --- a/os/gproc/gproc_proccess.go +++ b/os/gproc/gproc_process.go @@ -60,6 +60,11 @@ func NewProcess(path string, args []string, environment ...[]string) *Process { return p } +// NewProcessCmd creates and returns a process with given command and optional environment variable array. +func NewProcessCmd(cmd string, environment ...[]string) *Process { + return NewProcess(getShell(), []string{getShellOption(), cmd}, environment...) +} + // 开始执行(非阻塞) func (p *Process) Start() (int, error) { if p.Process != nil { @@ -114,6 +119,8 @@ func (p *Process) Kill() error { if p.Manager != nil { p.Manager.processes.Remove(p.Pid()) } + p.Process.Release() + p.Process.Wait() return nil } else { return err diff --git a/util/gconv/gconv_struct.go b/util/gconv/gconv_struct.go index cabfb84ec..db4039440 100644 --- a/util/gconv/gconv_struct.go +++ b/util/gconv/gconv_struct.go @@ -53,7 +53,7 @@ func Struct(params interface{}, pointer interface{}, mapping ...map[string]strin if !ok { rv := reflect.ValueOf(pointer) if kind := rv.Kind(); kind != reflect.Ptr { - return fmt.Errorf("object pointer should be type of: %v", kind) + return fmt.Errorf("object pointer should be type of '*struct', but got '%v'", kind) } // Using IsNil on reflect.Ptr variable is OK. if !rv.IsValid() || rv.IsNil() {