status use progobuf

master
hzsunshx 9 years ago
parent ed028d1000
commit 9a2b190955

@ -33,10 +33,6 @@ func MkdirIfNoExists(dir string) error {
return nil return nil
} }
func init() {
log.SetOutputLevel(log.Ldebug)
}
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") sockPath := filepath.Join(GOSUV_HOME, "gosuv.sock")
conn, err := grpcDial("unix", sockPath) conn, err := grpcDial("unix", sockPath)
@ -64,6 +60,10 @@ func testConnection(network, address string) error {
func wrap(f interface{}) func(*cli.Context) { func wrap(f interface{}) func(*cli.Context) {
return func(ctx *cli.Context) { return func(ctx *cli.Context) {
if ctx.GlobalBool("debug") {
log.SetOutputLevel(log.Ldebug)
}
sockPath := filepath.Join(GOSUV_HOME, "gosuv.sock") sockPath := filepath.Join(GOSUV_HOME, "gosuv.sock")
if err := testConnection("unix", sockPath); err != nil { if err := testConnection("unix", sockPath); err != nil {
log.Fatal(err) log.Fatal(err)
@ -90,23 +90,13 @@ func ServAction(ctx *cli.Context) {
ServeAddr(addr) ServeAddr(addr)
} }
func StatusAction(ctx *cli.Context) { func StatusAction(client pb.GoSuvClient) {
programs := make([]*Program, 0) res, err := client.Status(context.Background(), &pb.NopRequest{})
res, err := goreq.Request{
Method: "GET",
Uri: buildURI(ctx, "/api/programs"),
}.Do()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if res.StatusCode != http.StatusOK { for _, ps := range res.GetPrograms() {
log.Fatal(res.Body.ToString()) fmt.Printf("%-10s\t%-8s\t%s\n", ps.GetName(), ps.GetStatus(), ps.GetExtra())
}
if err = res.Body.FromJsonTo(&programs); err != nil {
log.Fatal(err)
}
for _, p := range programs {
fmt.Printf("%10s\t%s\n", p.Info.Name, p.Status)
} }
} }
@ -236,6 +226,11 @@ func initCli() {
Usage: "server address", Usage: "server address",
EnvVar: "GOSUV_SERVER_ADDR", EnvVar: "GOSUV_SERVER_ADDR",
}, },
cli.BoolFlag{
Name: "debug, d",
Usage: "enable debug info",
EnvVar: "GOSUV_DEBUG",
},
} }
app.Commands = []cli.Command{ app.Commands = []cli.Command{

@ -14,6 +14,7 @@ It has these top-level messages:
NopRequest NopRequest
Response Response
Request Request
ProgramStatus
StatusResponse StatusResponse
*/ */
package gosuvpb package gosuvpb
@ -120,38 +121,54 @@ func (m *Request) GetName() string {
return "" return ""
} }
type StatusResponse struct { type ProgramStatus struct {
Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"` Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"`
Status *string `protobuf:"bytes,2,req,name=status" json:"status,omitempty"` Status *string `protobuf:"bytes,2,req,name=status" json:"status,omitempty"`
Extra *string `protobuf:"bytes,3,opt,name=extra" json:"extra,omitempty"` Extra *string `protobuf:"bytes,3,opt,name=extra" json:"extra,omitempty"`
XXX_unrecognized []byte `json:"-"` XXX_unrecognized []byte `json:"-"`
} }
func (m *StatusResponse) Reset() { *m = StatusResponse{} } func (m *ProgramStatus) Reset() { *m = ProgramStatus{} }
func (m *StatusResponse) String() string { return proto.CompactTextString(m) } func (m *ProgramStatus) String() string { return proto.CompactTextString(m) }
func (*StatusResponse) ProtoMessage() {} func (*ProgramStatus) ProtoMessage() {}
func (m *StatusResponse) GetName() string { func (m *ProgramStatus) GetName() string {
if m != nil && m.Name != nil { if m != nil && m.Name != nil {
return *m.Name return *m.Name
} }
return "" return ""
} }
func (m *StatusResponse) GetStatus() string { func (m *ProgramStatus) GetStatus() string {
if m != nil && m.Status != nil { if m != nil && m.Status != nil {
return *m.Status return *m.Status
} }
return "" return ""
} }
func (m *StatusResponse) GetExtra() string { func (m *ProgramStatus) GetExtra() string {
if m != nil && m.Extra != nil { if m != nil && m.Extra != nil {
return *m.Extra return *m.Extra
} }
return "" return ""
} }
type StatusResponse struct {
Programs []*ProgramStatus `protobuf:"bytes,1,rep,name=programs" json:"programs,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *StatusResponse) Reset() { *m = StatusResponse{} }
func (m *StatusResponse) String() string { return proto.CompactTextString(m) }
func (*StatusResponse) ProtoMessage() {}
func (m *StatusResponse) GetPrograms() []*ProgramStatus {
if m != nil {
return m.Programs
}
return nil
}
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
var _ context.Context var _ context.Context
var _ grpc.ClientConn var _ grpc.ClientConn

@ -21,12 +21,16 @@ message Request {
required string name = 1; required string name = 1;
} }
message StatusResponse { message ProgramStatus {
required string name = 1; required string name = 1;
required string status = 2; required string status = 2;
optional string extra = 3; optional string extra = 3;
} }
message StatusResponse {
repeated ProgramStatus programs = 1;
}
service GoSuv { service GoSuv {
rpc Control(CtrlRequest) returns (CtrlResponse) {}; rpc Control(CtrlRequest) returns (CtrlResponse) {};
rpc Shutdown(NopRequest) returns (Response) {}; rpc Shutdown(NopRequest) returns (Response) {};

@ -65,5 +65,13 @@ func (s *PbSuvServer) Version(ctx context.Context, in *pb.NopRequest) (res *pb.R
} }
func (s *PbSuvServer) Status(ctx context.Context, in *pb.NopRequest) (res *pb.StatusResponse, err error) { func (s *PbSuvServer) Status(ctx context.Context, in *pb.NopRequest) (res *pb.StatusResponse, err error) {
res = &pb.StatusResponse{}
for _, program := range programTable.Programs() {
ps := &pb.ProgramStatus{}
ps.Name = proto.String(program.Info.Name)
ps.Status = proto.String(program.Status)
ps.Extra = proto.String("nothing")
res.Programs = append(res.Programs, ps)
}
return return
} }

@ -12,8 +12,8 @@ import (
"time" "time"
pb "github.com/codeskyblue/gosuv/gosuvpb" pb "github.com/codeskyblue/gosuv/gosuvpb"
"github.com/lunny/log"
"github.com/lunny/tango" "github.com/lunny/tango"
"github.com/qiniu/log"
"google.golang.org/grpc" "google.golang.org/grpc"
) )

Loading…
Cancel
Save