2017-05-25 14:32:40 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"net"
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
pb "github.com/ouqiang/gocron/modules/rpc/proto"
|
2017-05-26 10:09:07 +00:00
|
|
|
"github.com/ouqiang/gocron/modules/utils"
|
2017-05-25 14:32:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {}
|
|
|
|
|
|
|
|
func (s Server) Run(ctx context.Context, req *pb.TaskRequest) (*pb.TaskResponse, error) {
|
2017-05-29 07:09:41 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
grpclog.Println(err)
|
|
|
|
}
|
|
|
|
} ()
|
2017-05-26 13:59:01 +00:00
|
|
|
output, err := utils.ExecShell(ctx, req.Command)
|
2017-05-25 14:32:40 +00:00
|
|
|
resp := new(pb.TaskResponse)
|
2017-05-26 10:09:07 +00:00
|
|
|
resp.Output = output
|
2017-05-27 05:10:57 +00:00
|
|
|
if err != nil {
|
|
|
|
resp.Error = err.Error()
|
|
|
|
} else {
|
|
|
|
resp.Error = ""
|
|
|
|
}
|
2017-05-25 14:32:40 +00:00
|
|
|
|
2017-05-27 05:10:57 +00:00
|
|
|
return resp, nil
|
2017-05-25 14:32:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 10:09:07 +00:00
|
|
|
func Start(addr string) {
|
2017-06-21 12:19:59 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
grpclog.Println("panic", err)
|
|
|
|
}
|
|
|
|
} ()
|
2017-05-26 10:09:07 +00:00
|
|
|
l, err := net.Listen("tcp", addr)
|
2017-05-25 14:32:40 +00:00
|
|
|
if err != nil {
|
|
|
|
grpclog.Fatal(err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer()
|
|
|
|
pb.RegisterTaskServer(s, Server{})
|
2017-07-22 00:54:42 +00:00
|
|
|
grpclog.Println("listen ", addr)
|
2017-05-26 13:59:01 +00:00
|
|
|
err = s.Serve(l)
|
|
|
|
if err != nil {
|
|
|
|
grpclog.Fatal(err)
|
|
|
|
}
|
2017-05-25 14:32:40 +00:00
|
|
|
}
|
|
|
|
|