代码拉取完成,页面将自动刷新
package server
import (
"net"
"os"
"os/signal"
"syscall"
"time"
"gitee.com/hakwolf/taibak/internal/modules/rpc/auth"
pb "gitee.com/hakwolf/taibak/internal/modules/rpc/proto"
"gitee.com/hakwolf/taibak/internal/modules/utils"
//_ "github.com/mattn/go-adodb"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)
type Server struct{}
var keepAlivePolicy = keepalive.EnforcementPolicy{
MinTime: 10 * time.Second,
PermitWithoutStream: true,
}
var keepAliveParams = keepalive.ServerParameters{
MaxConnectionIdle: 30 * time.Second,
Time: 30 * time.Second,
Timeout: 3 * time.Second,
}
func (s Server) Run(ctx context.Context, req *pb.TaskRequest) (*pb.TaskResponse, error) {
defer func() {
if err := recover(); err != nil {
log.Error(err)
}
}()
log.Infof("execute cmd start: [id: %d cmd: %s]", req.Id, req.Command)
output, err := utils.ExecShell(ctx, req.Command)
resp := new(pb.TaskResponse)
resp.Output = output
if err != nil {
resp.Error = err.Error()
} else {
resp.Error = ""
}
log.Infof("execute cmd end: [id: %d cmd: %s err: %s]", req.Id, req.Command, resp.Error)
return resp, nil
}
func (s Server) DbRun(ctx context.Context, req *pb.DbTaskRequest) (*pb.DbTaskResponse, error) {
defer func() {
if err := recover(); err != nil {
log.Error(err)
}
}()
err := utils.ExecDb2(ctx, req)
resp := new(pb.DbTaskResponse)
//resp.Output = output
if err != nil {
resp.Error = err.Error()
} else {
resp.Error = ""
}
log.Infof("execute cmd start: [id: %d dbname: %s port %d]", req.Id, req.Dbname, req.Port)
return resp, nil
/* log.Infof("execute cmd start: [id: %d cmd: %s]", req.Id, req.Command)
output, err := utils.ExecShell(ctx, req.Command)
resp := new(pb.TaskResponse)
resp.Output = output
if err != nil {
resp.Error = err.Error()
} else {
resp.Error = ""
}
log.Infof("execute cmd end: [id: %d cmd: %s err: %s]", req.Id, req.Command, resp.Error)*/
}
func Start(addr string, enableTLS bool, certificate auth.Certificate) {
l, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal(err)
}
opts := []grpc.ServerOption{
grpc.KeepaliveParams(keepAliveParams),
grpc.KeepaliveEnforcementPolicy(keepAlivePolicy),
}
if enableTLS {
tlsConfig, err := certificate.GetTLSConfigForServer()
if err != nil {
log.Fatal(err)
}
opt := grpc.Creds(credentials.NewTLS(tlsConfig))
opts = append(opts, opt)
}
server := grpc.NewServer(opts...)
pb.RegisterTaskServer(server, Server{})
log.Infof("server listen on %s", addr)
go func() {
err = server.Serve(l)
if err != nil {
log.Fatal(err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
for {
s := <-c
log.Infoln("收到信号 -- ", s)
switch s {
case syscall.SIGHUP:
log.Infoln("收到终端断开信号, 忽略")
case syscall.SIGINT, syscall.SIGTERM:
log.Info("应用准备退出")
server.GracefulStop()
return
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。