remove control from grpc, and remove ST_STANDBY

master
hzsunshx 9 years ago
parent e381e923db
commit 44b5e5e327

@ -9,8 +9,6 @@ It is generated from these files:
gosuv.proto
It has these top-level messages:
CtrlRequest
CtrlResponse
NopRequest
Response
Request
@ -33,46 +31,6 @@ var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
type CtrlRequest struct {
Action *string `protobuf:"bytes,1,req,name=action" json:"action,omitempty"`
Name *string `protobuf:"bytes,2,req,name=name" json:"name,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *CtrlRequest) Reset() { *m = CtrlRequest{} }
func (m *CtrlRequest) String() string { return proto.CompactTextString(m) }
func (*CtrlRequest) ProtoMessage() {}
func (m *CtrlRequest) GetAction() string {
if m != nil && m.Action != nil {
return *m.Action
}
return ""
}
func (m *CtrlRequest) GetName() string {
if m != nil && m.Name != nil {
return *m.Name
}
return ""
}
type CtrlResponse struct {
Value *string `protobuf:"bytes,1,opt,name=value" json:"value,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *CtrlResponse) Reset() { *m = CtrlResponse{} }
func (m *CtrlResponse) String() string { return proto.CompactTextString(m) }
func (*CtrlResponse) ProtoMessage() {}
func (m *CtrlResponse) GetValue() string {
if m != nil && m.Value != nil {
return *m.Value
}
return ""
}
type NopRequest struct {
XXX_unrecognized []byte `json:"-"`
}
@ -176,7 +134,6 @@ var _ grpc.ClientConn
// Client API for GoSuv service
type GoSuvClient interface {
Control(ctx context.Context, in *CtrlRequest, opts ...grpc.CallOption) (*CtrlResponse, error)
Shutdown(ctx context.Context, in *NopRequest, opts ...grpc.CallOption) (*Response, error)
Version(ctx context.Context, in *NopRequest, opts ...grpc.CallOption) (*Response, error)
Status(ctx context.Context, in *NopRequest, opts ...grpc.CallOption) (*StatusResponse, error)
@ -190,15 +147,6 @@ func NewGoSuvClient(cc *grpc.ClientConn) GoSuvClient {
return &goSuvClient{cc}
}
func (c *goSuvClient) Control(ctx context.Context, in *CtrlRequest, opts ...grpc.CallOption) (*CtrlResponse, error) {
out := new(CtrlResponse)
err := grpc.Invoke(ctx, "/gosuvpb.GoSuv/Control", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *goSuvClient) Shutdown(ctx context.Context, in *NopRequest, opts ...grpc.CallOption) (*Response, error) {
out := new(Response)
err := grpc.Invoke(ctx, "/gosuvpb.GoSuv/Shutdown", in, out, c.cc, opts...)
@ -229,7 +177,6 @@ func (c *goSuvClient) Status(ctx context.Context, in *NopRequest, opts ...grpc.C
// Server API for GoSuv service
type GoSuvServer interface {
Control(context.Context, *CtrlRequest) (*CtrlResponse, error)
Shutdown(context.Context, *NopRequest) (*Response, error)
Version(context.Context, *NopRequest) (*Response, error)
Status(context.Context, *NopRequest) (*StatusResponse, error)
@ -239,18 +186,6 @@ func RegisterGoSuvServer(s *grpc.Server, srv GoSuvServer) {
s.RegisterService(&_GoSuv_serviceDesc, srv)
}
func _GoSuv_Control_Handler(srv interface{}, ctx context.Context, codec grpc.Codec, buf []byte) (interface{}, error) {
in := new(CtrlRequest)
if err := codec.Unmarshal(buf, in); err != nil {
return nil, err
}
out, err := srv.(GoSuvServer).Control(ctx, in)
if err != nil {
return nil, err
}
return out, nil
}
func _GoSuv_Shutdown_Handler(srv interface{}, ctx context.Context, codec grpc.Codec, buf []byte) (interface{}, error) {
in := new(NopRequest)
if err := codec.Unmarshal(buf, in); err != nil {
@ -291,10 +226,6 @@ var _GoSuv_serviceDesc = grpc.ServiceDesc{
ServiceName: "gosuvpb.GoSuv",
HandlerType: (*GoSuvServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Control",
Handler: _GoSuv_Control_Handler,
},
{
MethodName: "Shutdown",
Handler: _GoSuv_Shutdown_Handler,

@ -1,16 +1,6 @@
package gosuvpb;
message CtrlRequest {
required string action =1;
required string name = 2;
}
message CtrlResponse {
optional string value = 1;
}
message NopRequest {
}
message NopRequest {}
message Response {
optional int32 code = 1;
@ -32,7 +22,6 @@ message StatusResponse {
}
service GoSuv {
rpc Control(CtrlRequest) returns (CtrlResponse) {};
rpc Shutdown(NopRequest) returns (Response) {};
rpc Version(NopRequest) returns (Response) {};
rpc Status(NopRequest) returns (StatusResponse) {};

@ -42,11 +42,10 @@ func GoTimeoutFunc(timeout time.Duration, f func() error) chan error {
}
const (
ST_STANDBY = "STANDBY"
ST_RUNNING = "RUNNING"
ST_STOPPED = "STOPPED"
ST_FATAL = "FATAL"
ST_RETRYWAIT = "RETRYWAIT"
ST_RETRYWAIT = "RETRYWAIT" // some like python-supervisor EXITED
)
type Event int
@ -61,8 +60,9 @@ type ProgramInfo struct {
Command []string `json:"command"`
Dir string `json:"dir"`
Environ []string `json:"environ"`
AutoStart bool `json:"autostart"`
AutoStart bool `json:"autostart"` // change to *bool, which support unexpected
StartRetries int `json:"startretries"`
StartSeconds int `json:"startsecs"`
}
func (p *ProgramInfo) buildCmd() *exec.Cmd {
@ -87,9 +87,12 @@ func NewProgram(info *ProgramInfo) *Program {
if info.StartRetries == 0 {
info.StartRetries = 3
}
if info.StartSeconds == 0 {
info.StartSeconds = 3
}
return &Program{
//Process: kproc.ProcCommand(cmd),
Status: ST_STANDBY,
Status: ST_STOPPED,
Sig: make(chan os.Signal),
Info: info,
stopc: make(chan bool),
@ -104,11 +107,11 @@ func (p *Program) setStatus(st string) {
func (p *Program) InputData(event Event) {
switch event {
case EVENT_START:
if p.Status != ST_RUNNING {
if p.Status == ST_STOPPED || p.Status == ST_FATAL {
go p.RunWithRetry()
}
case EVENT_STOP:
if p.Status == ST_RUNNING {
if p.Status == ST_RUNNING || p.Status == ST_RETRYWAIT {
p.Stop()
}
}
@ -117,7 +120,7 @@ func (p *Program) InputData(event Event) {
func (p *Program) createLog() (*os.File, error) {
logDir := filepath.Join(GOSUV_HOME, "logs")
os.MkdirAll(logDir, 0755) // just do it, err ignore it
logFile := filepath.Join(logDir, p.Info.Name+".output.log")
logFile := filepath.Join(logDir, p.Info.Name+".log")
return os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
}
@ -130,16 +133,25 @@ func (p *Program) sleep(d time.Duration) {
}
func (p *Program) RunWithRetry() {
for p.retry = 0; p.retry < p.Info.StartRetries; p.retry += 1 {
for p.retry = 0; p.retry < p.Info.StartRetries+1; p.retry += 1 {
// wait program to exit
errc := GoFunc(p.Run)
var err error
PROGRAM_WAIT:
// Here is RUNNING State
select {
case err := <-GoFunc(p.Run):
case err = <-errc:
log.Info(p.Info.Name, err)
case <-time.After(time.Second * time.Duration(p.Info.StartSeconds)): // reset retry
p.retry = 0
goto PROGRAM_WAIT
case <-p.stopc:
return
}
// retry
if p.retry+1 < p.Info.StartRetries {
// Enter RETRY_WAIT State
if p.retry < p.Info.StartRetries {
p.setStatus(ST_RETRYWAIT)
select {
case <-p.stopc:

@ -39,12 +39,6 @@ type PbSuvServer struct {
lis net.Listener
}
func (s *PbSuvServer) Control(ctx context.Context, in *pb.CtrlRequest) (*pb.CtrlResponse, error) {
res := &pb.CtrlResponse{}
res.Value = proto.String("Hi")
return res, nil
}
func (s *PbSuvServer) Shutdown(ctx context.Context, in *pb.NopRequest) (*pb.Response, error) {
go func() {
time.Sleep(50 * time.Millisecond)

Loading…
Cancel
Save