status use progobuf

master
hzsunshx 9 years ago
parent ed028d1000
commit 9a2b190955

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

@ -14,6 +14,7 @@ It has these top-level messages:
NopRequest
Response
Request
ProgramStatus
StatusResponse
*/
package gosuvpb
@ -120,38 +121,54 @@ func (m *Request) GetName() string {
return ""
}
type StatusResponse struct {
type ProgramStatus struct {
Name *string `protobuf:"bytes,1,req,name=name" json:"name,omitempty"`
Status *string `protobuf:"bytes,2,req,name=status" json:"status,omitempty"`
Extra *string `protobuf:"bytes,3,opt,name=extra" json:"extra,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 *ProgramStatus) Reset() { *m = ProgramStatus{} }
func (m *ProgramStatus) String() string { return proto.CompactTextString(m) }
func (*ProgramStatus) ProtoMessage() {}
func (m *StatusResponse) GetName() string {
func (m *ProgramStatus) GetName() string {
if m != nil && m.Name != nil {
return *m.Name
}
return ""
}
func (m *StatusResponse) GetStatus() string {
func (m *ProgramStatus) GetStatus() string {
if m != nil && m.Status != nil {
return *m.Status
}
return ""
}
func (m *StatusResponse) GetExtra() string {
func (m *ProgramStatus) GetExtra() string {
if m != nil && m.Extra != nil {
return *m.Extra
}
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.
var _ context.Context
var _ grpc.ClientConn

@ -21,12 +21,16 @@ message Request {
required string name = 1;
}
message StatusResponse {
message ProgramStatus {
required string name = 1;
required string status = 2;
optional string extra = 3;
}
message StatusResponse {
repeated ProgramStatus programs = 1;
}
service GoSuv {
rpc Control(CtrlRequest) returns (CtrlResponse) {};
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) {
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
}

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

Loading…
Cancel
Save