Skip to content

Commit

Permalink
gocron-node增加命令执行日志
Browse files Browse the repository at this point in the history
  • Loading branch information
ouqiang committed Jun 2, 2019
1 parent 372a5e2 commit bf76393
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
6 changes: 3 additions & 3 deletions cmd/gocron/gocron.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os/signal"
"syscall"

macaron "gopkg.in/macaron.v1"

"github.com/ouqiang/gocron/internal/models"
"github.com/ouqiang/gocron/internal/modules/app"
"github.com/ouqiang/gocron/internal/modules/logger"
Expand All @@ -16,7 +18,6 @@ import (
"github.com/ouqiang/gocron/internal/service"
"github.com/ouqiang/goutil"
"github.com/urfave/cli"
"gopkg.in/macaron.v1"
)

var (
Expand Down Expand Up @@ -144,8 +145,7 @@ func setEnvironment(ctx *cli.Context) {

// 捕捉信号
func catchSignal() {
c := make(chan os.Signal)
// todo 配置热更新, windows 不支持 syscall.SIGUSR1, syscall.SIGUSR2
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
for {
s := <-c
Expand Down
18 changes: 12 additions & 6 deletions cmd/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main

import (
"flag"
"fmt"
"os"
"runtime"
"strings"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/ouqiang/gocron/internal/modules/rpc/server"
"github.com/ouqiang/gocron/internal/modules/utils"
"github.com/ouqiang/goutil"
log "github.com/sirupsen/logrus"
)

var (
Expand All @@ -26,14 +26,21 @@ func main() {
var certFile string
var keyFile string
var enableTLS bool
var logLevel string
flag.BoolVar(&allowRoot, "allow-root", false, "./gocron-node -allow-root")
flag.StringVar(&serverAddr, "s", "0.0.0.0:5921", "./gocron-node -s ip:port")
flag.BoolVar(&version, "v", false, "./gocron-node -v")
flag.BoolVar(&enableTLS, "enable-tls", false, "./gocron-node -enable-tls")
flag.StringVar(&CAFile, "ca-file", "", "./gocron-node -ca-file path")
flag.StringVar(&certFile, "cert-file", "", "./gocron-node -cert-file path")
flag.StringVar(&keyFile, "key-file", "", "./gocron-node -key-file path")
flag.StringVar(&logLevel, "log-level", "info", "-log-level error")
flag.Parse()
level, err := log.ParseLevel(logLevel)
if err != nil {
log.Fatal(err)
}
log.SetLevel(level)

if version {
goutil.PrintAppVersion(AppVersion, GitCommit, BuildDate)
Expand All @@ -42,15 +49,14 @@ func main() {

if enableTLS {
if !utils.FileExist(CAFile) {
fmt.Printf("failed to read ca cert file: %s", CAFile)
return
log.Fatalf("failed to read ca cert file: %s", CAFile)
}
if !utils.FileExist(certFile) {
fmt.Printf("failed to read server cert file: %s", certFile)
log.Fatalf("failed to read server cert file: %s", certFile)
return
}
if !utils.FileExist(keyFile) {
fmt.Printf("failed to read server key file: %s", keyFile)
log.Fatalf("failed to read server key file: %s", keyFile)
return
}
}
Expand All @@ -62,7 +68,7 @@ func main() {
}

if runtime.GOOS != "windows" && os.Getuid() == 0 && !allowRoot {
fmt.Println("Do not run gocron-node as root user")
log.Fatal("Do not run gocron-node as root user")
return
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/lib/pq v1.1.1
github.com/ouqiang/goutil v1.1.1
github.com/rakyll/statik v0.1.6
github.com/sirupsen/logrus v1.4.2
github.com/urfave/cli v1.20.0
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 // indirect
golang.org/x/net v0.0.0-20190522155817-f3200d17e092
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ github.com/klauspost/compress v1.5.0 h1:iDac0ZKbmSA4PRrRuXXjZL8C7UoJan8oBYxXkMzE
github.com/klauspost/compress v1.5.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -85,11 +86,14 @@ github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 h1:pntxY8Ary0t43dCZ5dqY4YTJCObLY1kIXl0uzMv+7DE=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 h1:Jpy1PXuP99tXNrhbq2BaPz9B+jNAvH1JPQQpG/9GCXY=
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c h1:Ho+uVpkel/udgjbwB5Lktg9BtvJSh2DT0Hi6LPSyI2w=
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down Expand Up @@ -118,6 +122,8 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down
48 changes: 34 additions & 14 deletions internal/modules/rpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package server

import (
"net"
"os"
"os/signal"
"syscall"
"time"

"google.golang.org/grpc/keepalive"

"github.com/ouqiang/gocron/internal/modules/rpc/auth"
pb "github.com/ouqiang/gocron/internal/modules/rpc/proto"
"github.com/ouqiang/gocron/internal/modules/utils"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/keepalive"
)

type Server struct{}
Expand All @@ -33,9 +35,10 @@ var keepAliveParams = keepalive.ServerParameters{
func (s Server) Run(ctx context.Context, req *pb.TaskRequest) (*pb.TaskResponse, error) {
defer func() {
if err := recover(); err != nil {
grpclog.Println(err)
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
Expand All @@ -44,35 +47,52 @@ func (s Server) Run(ctx context.Context, req *pb.TaskRequest) (*pb.TaskResponse,
} else {
resp.Error = ""
}
log.Infof("execute cmd end: [id: %d cmd: %s err: %s]", req.Id, req.Command, resp.Error)

return resp, nil
}

func Start(addr string, enableTLS bool, certificate auth.Certificate) {
l, err := net.Listen("tcp", addr)
if err != nil {
grpclog.Fatal(err)
log.Fatal(err)
}

var s *grpc.Server
opts := []grpc.ServerOption{
grpc.KeepaliveParams(keepAliveParams),
grpc.KeepaliveEnforcementPolicy(keepAlivePolicy),
}
if enableTLS {
tlsConfig, err := certificate.GetTLSConfigForServer()
if err != nil {
grpclog.Fatal(err)
log.Fatal(err)
}
opt := grpc.Creds(credentials.NewTLS(tlsConfig))
opts = append(opts, opt)
}
s = grpc.NewServer(opts...)
pb.RegisterTaskServer(s, Server{})
grpclog.Printf("server listen on %s", addr)
server := grpc.NewServer(opts...)
pb.RegisterTaskServer(server, Server{})
log.Infof("server listen on %s", addr)

err = s.Serve(l)
if err != nil {
grpclog.Fatal(err)
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
}
}

}

0 comments on commit bf76393

Please sign in to comment.