From a7b3ab53d48639f099e81b78b1e5d65afdce21c3 Mon Sep 17 00:00:00 2001 From: codeskyblue Date: Sun, 6 Mar 2016 11:51:45 +0800 Subject: [PATCH] reformat code --- gosuv.go | 52 +++++++++++++++++++++++++--------------------------- program.go | 2 +- service.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- web.go | 16 ++++------------ 4 files changed, 72 insertions(+), 42 deletions(-) diff --git a/gosuv.go b/gosuv.go index 7dc020d..90f3e2e 100644 --- a/gosuv.go +++ b/gosuv.go @@ -18,7 +18,14 @@ import ( "google.golang.org/grpc" ) -const GOSUV_VERSION = "0.0.2" +const GOSUV_VERSION = "0.0.3" + +var ( + GOSUV_HOME = os.ExpandEnv("$HOME/.gosuv") + GOSUV_SOCK_PATH = filepath.Join(GOSUV_HOME, "gosuv.sock") + GOSUV_CONFIG = filepath.Join(GOSUV_HOME, "gosuv.json") + GOSUV_PROGRAM_CONFIG = filepath.Join(GOSUV_HOME, "programs.json") +) var ( CMDPLUGIN_DIR = filepath.Join(GOSUV_HOME, "cmdplugin") @@ -33,8 +40,7 @@ func MkdirIfNoExists(dir string) error { } func connect(ctx *cli.Context) (cc *grpc.ClientConn, err error) { - sockPath := filepath.Join(GOSUV_HOME, "gosuv.sock") - conn, err := grpcDial("unix", sockPath) + conn, err := grpcDial("unix", GOSUV_SOCK_PATH) return conn, err } @@ -63,8 +69,7 @@ func wrap(f interface{}) func(*cli.Context) { log.SetOutputLevel(log.Ldebug) } - sockPath := filepath.Join(GOSUV_HOME, "gosuv.sock") - if err := testConnection("unix", sockPath); err != nil { + if err := testConnection("unix", GOSUV_SOCK_PATH); err != nil { log.Fatal(err) } @@ -86,10 +91,10 @@ func wrap(f interface{}) func(*cli.Context) { func ServAction(ctx *cli.Context) { addr := ctx.GlobalString("addr") - ServeAddr(addr) + RunGosuvService(addr) } -func StatusAction(client pb.GoSuvClient) { +func ActionStatus(client pb.GoSuvClient) { res, err := client.Status(context.Background(), &pb.NopRequest{}) if err != nil { log.Fatal(err) @@ -99,7 +104,7 @@ func StatusAction(client pb.GoSuvClient) { } } -func AddAction(ctx *cli.Context, client pb.GoSuvClient) { +func ActionAdd(ctx *cli.Context, client pb.GoSuvClient) { name := ctx.String("name") if name == "" { name = filepath.Base(ctx.Args()[0]) @@ -133,7 +138,7 @@ func buildURI(ctx *cli.Context, uri string) string { return fmt.Sprintf("http://%s%s", ctx.GlobalString("addr"), uri) } -func StopAction(ctx *cli.Context) { +func ActionStop(ctx *cli.Context) { conn, err := connect(ctx) if err != nil { log.Fatal(err) @@ -149,7 +154,7 @@ func StopAction(ctx *cli.Context) { fmt.Println(res.Message) } -func TailAction(ctx *cli.Context, client pb.ProgramClient) { +func ActionTail(ctx *cli.Context, client pb.ProgramClient) { req := &pb.TailRequest{ Name: ctx.Args().First(), Number: int32(ctx.Int("number")), @@ -176,7 +181,7 @@ func Errorf(format string, v ...interface{}) { os.Exit(1) } -func StartAction(ctx *cli.Context) { +func ActionStart(ctx *cli.Context) { conn, err := connect(ctx) if err != nil { log.Fatal(err) @@ -200,7 +205,7 @@ func grpcDial(network, addr string) (*grpc.ClientConn, error) { })) } -func ShutdownAction(ctx *cli.Context) { +func ActionShutdown(ctx *cli.Context) { conn, err := connect(ctx) if err != nil { fmt.Println("server already closed") @@ -216,7 +221,7 @@ func ShutdownAction(ctx *cli.Context) { fmt.Println(res.Message) } -func VersionAction(ctx *cli.Context, client pb.GoSuvClient) { +func ActionVersion(ctx *cli.Context, client pb.GoSuvClient) { fmt.Printf("Client: %s\n", GOSUV_VERSION) res, err := client.Version(context.Background(), &pb.NopRequest{}) if err != nil { @@ -251,13 +256,13 @@ func initCli() { { Name: "version", Usage: "Show version", - Action: wrap(VersionAction), + Action: wrap(ActionVersion), }, { Name: "status", Aliases: []string{"st"}, Usage: "show program status", - Action: wrap(StatusAction), + Action: wrap(ActionStatus), }, { Name: "add", @@ -272,22 +277,22 @@ func initCli() { Usage: "Specify environ", }, }, - Action: wrap(AddAction), + Action: wrap(ActionAdd), }, { Name: "start", Usage: "start a not running program", - Action: wrap(StartAction), + Action: wrap(ActionStart), }, { Name: "stop", Usage: "Stop running program", - Action: wrap(StopAction), + Action: wrap(ActionStop), }, { Name: "tail", Usage: "tail log", - Action: wrap(TailAction), + Action: wrap(ActionTail), Flags: []cli.Flag{ cli.IntFlag{ Name: "number, n", @@ -303,7 +308,7 @@ func initCli() { { Name: "shutdown", Usage: "Shutdown server", - Action: ShutdownAction, + Action: ActionShutdown, }, { Name: "serv", @@ -350,13 +355,6 @@ func runPlugin(ctx *cli.Context, name string) { cmd.Env = append(os.Environ(), envs...) cmd.Run() } - -var ( - GOSUV_HOME = os.ExpandEnv("$HOME/.gosuv") - GOSUV_CONFIG = filepath.Join(GOSUV_HOME, "gosuv.json") - GOSUV_PROGRAM_CONFIG = filepath.Join(GOSUV_HOME, "programs.json") -) - func main() { MkdirIfNoExists(GOSUV_HOME) diff --git a/program.go b/program.go index 7883daf..ce2e6ee 100644 --- a/program.go +++ b/program.go @@ -206,7 +206,7 @@ func (p *Program) Start() error { var programTable *ProgramTable -func InitServer() { +func initProgramTable() { programTable = &ProgramTable{ table: make(map[string]*Program, 10), ch: make(chan string), diff --git a/service.go b/service.go index 2833d22..de21878 100644 --- a/service.go +++ b/service.go @@ -6,14 +6,16 @@ import ( "net" "os" "os/exec" + "os/signal" + "syscall" "time" pb "github.com/codeskyblue/gosuv/gosuvpb" "golang.org/x/net/context" + "google.golang.org/grpc" ) -type PbProgram struct { -} +type PbProgram struct{} func (this *PbProgram) Start(ctx context.Context, in *pb.Request) (res *pb.Response, err error) { res = &pb.Response{} @@ -122,3 +124,41 @@ func (s *PbSuvServer) Status(ctx context.Context, in *pb.NopRequest) (res *pb.St } return } + +func handleSignal(lis net.Listener) { + sigc := make(chan os.Signal, 2) + signal.Notify(sigc, syscall.SIGINT, syscall.SIGHUP) + go func() { + for sig := range sigc { + log.Println("Receive signal:", sig) + if sig == syscall.SIGHUP { + return // ignore, when shell session closed, gosuv will receive SIGHUP signal + } + lis.Close() + programTable.StopAll() + os.Exit(0) + return + } + }() +} + +func RunGosuvService(addr string) error { + initProgramTable() + + lis, err := net.Listen("unix", GOSUV_SOCK_PATH) + if err != nil { + log.Fatal(err) + } + handleSignal(lis) + + pbServ := &PbSuvServer{} + pbProgram := &PbProgram{} + + grpcServ := grpc.NewServer() + pb.RegisterGoSuvServer(grpcServ, pbServ) + pb.RegisterProgramServer(grpcServ, pbProgram) + + pbServ.lis = lis + grpcServ.Serve(lis) + return fmt.Errorf("Address: %s has been used", addr) +} diff --git a/web.go b/web.go index a264164..474c879 100644 --- a/web.go +++ b/web.go @@ -2,17 +2,7 @@ package main import ( "encoding/json" - "fmt" - "net" "net/http" - "os" - "os/signal" - "path/filepath" - "syscall" - - pb "github.com/codeskyblue/gosuv/gosuvpb" - "github.com/qiniu/log" - "google.golang.org/grpc" ) type JSONResponse struct { @@ -72,6 +62,7 @@ func shutdownHandler(w http.ResponseWriter, r *http.Request) { } */ +/* func handleSignal(lis net.Listener) { sigc := make(chan os.Signal, 2) signal.Notify(sigc, syscall.SIGINT, syscall.SIGHUP) @@ -79,7 +70,7 @@ func handleSignal(lis net.Listener) { for sig := range sigc { log.Println("Receive signal:", sig) if sig == syscall.SIGHUP { - return // ignore + return // ignore, when shell session closed, gosuv will receive SIGHUP signal } lis.Close() programTable.StopAll() @@ -90,7 +81,7 @@ func handleSignal(lis net.Listener) { } func ServeAddr(addr string) error { - InitServer() + initProgramTable() lis, err := net.Listen("unix", filepath.Join(GOSUV_HOME, "gosuv.sock")) if err != nil { @@ -109,3 +100,4 @@ func ServeAddr(addr string) error { grpcServ.Serve(lis) return fmt.Errorf("Address: %s has been used", addr) } +*/