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-26 10:09:07 +00:00
|
|
|
output, err := utils.ExecShellWithTimeout(int(req.Timeout), 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-25 14:32:40 +00:00
|
|
|
|
2017-05-26 10:09:07 +00:00
|
|
|
return resp, err
|
2017-05-25 14:32:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 10:09:07 +00:00
|
|
|
func Start(addr string) {
|
|
|
|
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-05-26 10:09:07 +00:00
|
|
|
grpclog.Println("listen address ", addr)
|
2017-05-25 14:32:40 +00:00
|
|
|
s.Serve(l)
|
|
|
|
}
|
|
|
|
|