reformat code

master
codeskyblue 8 years ago
parent b8b60f2418
commit a7b3ab53d4

@ -18,7 +18,14 @@ import (
"google.golang.org/grpc" "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 ( var (
CMDPLUGIN_DIR = filepath.Join(GOSUV_HOME, "cmdplugin") 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) { func connect(ctx *cli.Context) (cc *grpc.ClientConn, err error) {
sockPath := filepath.Join(GOSUV_HOME, "gosuv.sock") conn, err := grpcDial("unix", GOSUV_SOCK_PATH)
conn, err := grpcDial("unix", sockPath)
return conn, err return conn, err
} }
@ -63,8 +69,7 @@ func wrap(f interface{}) func(*cli.Context) {
log.SetOutputLevel(log.Ldebug) log.SetOutputLevel(log.Ldebug)
} }
sockPath := filepath.Join(GOSUV_HOME, "gosuv.sock") if err := testConnection("unix", GOSUV_SOCK_PATH); err != nil {
if err := testConnection("unix", sockPath); err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -86,10 +91,10 @@ func wrap(f interface{}) func(*cli.Context) {
func ServAction(ctx *cli.Context) { func ServAction(ctx *cli.Context) {
addr := ctx.GlobalString("addr") 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{}) res, err := client.Status(context.Background(), &pb.NopRequest{})
if err != nil { if err != nil {
log.Fatal(err) 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") name := ctx.String("name")
if name == "" { if name == "" {
name = filepath.Base(ctx.Args()[0]) 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) return fmt.Sprintf("http://%s%s", ctx.GlobalString("addr"), uri)
} }
func StopAction(ctx *cli.Context) { func ActionStop(ctx *cli.Context) {
conn, err := connect(ctx) conn, err := connect(ctx)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -149,7 +154,7 @@ func StopAction(ctx *cli.Context) {
fmt.Println(res.Message) fmt.Println(res.Message)
} }
func TailAction(ctx *cli.Context, client pb.ProgramClient) { func ActionTail(ctx *cli.Context, client pb.ProgramClient) {
req := &pb.TailRequest{ req := &pb.TailRequest{
Name: ctx.Args().First(), Name: ctx.Args().First(),
Number: int32(ctx.Int("number")), Number: int32(ctx.Int("number")),
@ -176,7 +181,7 @@ func Errorf(format string, v ...interface{}) {
os.Exit(1) os.Exit(1)
} }
func StartAction(ctx *cli.Context) { func ActionStart(ctx *cli.Context) {
conn, err := connect(ctx) conn, err := connect(ctx)
if err != nil { if err != nil {
log.Fatal(err) 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) conn, err := connect(ctx)
if err != nil { if err != nil {
fmt.Println("server already closed") fmt.Println("server already closed")
@ -216,7 +221,7 @@ func ShutdownAction(ctx *cli.Context) {
fmt.Println(res.Message) 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) fmt.Printf("Client: %s\n", GOSUV_VERSION)
res, err := client.Version(context.Background(), &pb.NopRequest{}) res, err := client.Version(context.Background(), &pb.NopRequest{})
if err != nil { if err != nil {
@ -251,13 +256,13 @@ func initCli() {
{ {
Name: "version", Name: "version",
Usage: "Show version", Usage: "Show version",
Action: wrap(VersionAction), Action: wrap(ActionVersion),
}, },
{ {
Name: "status", Name: "status",
Aliases: []string{"st"}, Aliases: []string{"st"},
Usage: "show program status", Usage: "show program status",
Action: wrap(StatusAction), Action: wrap(ActionStatus),
}, },
{ {
Name: "add", Name: "add",
@ -272,22 +277,22 @@ func initCli() {
Usage: "Specify environ", Usage: "Specify environ",
}, },
}, },
Action: wrap(AddAction), Action: wrap(ActionAdd),
}, },
{ {
Name: "start", Name: "start",
Usage: "start a not running program", Usage: "start a not running program",
Action: wrap(StartAction), Action: wrap(ActionStart),
}, },
{ {
Name: "stop", Name: "stop",
Usage: "Stop running program", Usage: "Stop running program",
Action: wrap(StopAction), Action: wrap(ActionStop),
}, },
{ {
Name: "tail", Name: "tail",
Usage: "tail log", Usage: "tail log",
Action: wrap(TailAction), Action: wrap(ActionTail),
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.IntFlag{ cli.IntFlag{
Name: "number, n", Name: "number, n",
@ -303,7 +308,7 @@ func initCli() {
{ {
Name: "shutdown", Name: "shutdown",
Usage: "Shutdown server", Usage: "Shutdown server",
Action: ShutdownAction, Action: ActionShutdown,
}, },
{ {
Name: "serv", Name: "serv",
@ -350,13 +355,6 @@ func runPlugin(ctx *cli.Context, name string) {
cmd.Env = append(os.Environ(), envs...) cmd.Env = append(os.Environ(), envs...)
cmd.Run() 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() { func main() {
MkdirIfNoExists(GOSUV_HOME) MkdirIfNoExists(GOSUV_HOME)

@ -206,7 +206,7 @@ func (p *Program) Start() error {
var programTable *ProgramTable var programTable *ProgramTable
func InitServer() { func initProgramTable() {
programTable = &ProgramTable{ programTable = &ProgramTable{
table: make(map[string]*Program, 10), table: make(map[string]*Program, 10),
ch: make(chan string), ch: make(chan string),

@ -6,14 +6,16 @@ import (
"net" "net"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"syscall"
"time" "time"
pb "github.com/codeskyblue/gosuv/gosuvpb" pb "github.com/codeskyblue/gosuv/gosuvpb"
"golang.org/x/net/context" "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) { func (this *PbProgram) Start(ctx context.Context, in *pb.Request) (res *pb.Response, err error) {
res = &pb.Response{} res = &pb.Response{}
@ -122,3 +124,41 @@ func (s *PbSuvServer) Status(ctx context.Context, in *pb.NopRequest) (res *pb.St
} }
return 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)
}

@ -2,17 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net"
"net/http" "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 { type JSONResponse struct {
@ -72,6 +62,7 @@ func shutdownHandler(w http.ResponseWriter, r *http.Request) {
} }
*/ */
/*
func handleSignal(lis net.Listener) { func handleSignal(lis net.Listener) {
sigc := make(chan os.Signal, 2) sigc := make(chan os.Signal, 2)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGHUP) signal.Notify(sigc, syscall.SIGINT, syscall.SIGHUP)
@ -79,7 +70,7 @@ func handleSignal(lis net.Listener) {
for sig := range sigc { for sig := range sigc {
log.Println("Receive signal:", sig) log.Println("Receive signal:", sig)
if sig == syscall.SIGHUP { if sig == syscall.SIGHUP {
return // ignore return // ignore, when shell session closed, gosuv will receive SIGHUP signal
} }
lis.Close() lis.Close()
programTable.StopAll() programTable.StopAll()
@ -90,7 +81,7 @@ func handleSignal(lis net.Listener) {
} }
func ServeAddr(addr string) error { func ServeAddr(addr string) error {
InitServer() initProgramTable()
lis, err := net.Listen("unix", filepath.Join(GOSUV_HOME, "gosuv.sock")) lis, err := net.Listen("unix", filepath.Join(GOSUV_HOME, "gosuv.sock"))
if err != nil { if err != nil {
@ -109,3 +100,4 @@ func ServeAddr(addr string) error {
grpcServ.Serve(lis) grpcServ.Serve(lis)
return fmt.Errorf("Address: %s has been used", addr) return fmt.Errorf("Address: %s has been used", addr)
} }
*/

Loading…
Cancel
Save