service: logging to syslog (#33)
* service: logging to syslog * chore: update gopsutil to v4 * chore: move println() to the util package * gpu: only return 0 on failure
This commit is contained in:
+3
-3
@@ -7,8 +7,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/shirou/gopsutil/v3/disk"
|
||||
psnet "github.com/shirou/gopsutil/v3/net"
|
||||
"github.com/shirou/gopsutil/v4/disk"
|
||||
psnet "github.com/shirou/gopsutil/v4/net"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -123,4 +123,4 @@ func editAgentConfig(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
fmt.Println("修改自定义配置成功,重启 Agent 后生效")
|
||||
}
|
||||
}
|
||||
|
||||
+4
-38
@@ -25,7 +25,7 @@ import (
|
||||
"github.com/nezhahq/go-github-selfupdate/selfupdate"
|
||||
"github.com/nezhahq/service"
|
||||
"github.com/quic-go/quic-go/http3"
|
||||
"github.com/shirou/gopsutil/v3/host"
|
||||
"github.com/shirou/gopsutil/v4/host"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
@@ -55,17 +55,11 @@ type AgentCliParam struct {
|
||||
IPReportPeriod uint32 // 上报IP间隔
|
||||
}
|
||||
|
||||
type program struct {
|
||||
exit chan struct{}
|
||||
service service.Service
|
||||
}
|
||||
|
||||
var (
|
||||
version string
|
||||
arch string
|
||||
client pb.NezhaServiceClient
|
||||
inited bool
|
||||
logger service.Logger
|
||||
)
|
||||
|
||||
var agentCmd = &cobra.Command{
|
||||
@@ -211,33 +205,6 @@ func preRun(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *program) Start(s service.Service) error {
|
||||
go p.run()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *program) Stop(s service.Service) error {
|
||||
close(p.exit)
|
||||
if service.Interactive() {
|
||||
os.Exit(0)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *program) run() {
|
||||
defer func() {
|
||||
if service.Interactive() {
|
||||
p.Stop(p.service)
|
||||
} else {
|
||||
p.service.Stop()
|
||||
}
|
||||
}()
|
||||
|
||||
run()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func run() {
|
||||
auth := model.AuthHandler{
|
||||
ClientSecret: agentCliParam.ClientSecret,
|
||||
@@ -345,7 +312,7 @@ func runService(action string, flags []string) {
|
||||
prg.service = s
|
||||
|
||||
errs := make(chan error, 5)
|
||||
logger, err = s.Logger(errs)
|
||||
util.Logger, err = s.Logger(errs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -374,7 +341,7 @@ func runService(action string, flags []string) {
|
||||
|
||||
err = s.Run()
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
util.Logger.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -740,8 +707,7 @@ func handleTerminalTask(task *pb.Task) {
|
||||
|
||||
func println(v ...interface{}) {
|
||||
if agentCliParam.Debug {
|
||||
fmt.Printf("NEZHA@%s>> ", time.Now().Format("2006-01-02 15:04:05"))
|
||||
fmt.Println(v...)
|
||||
util.Println(v...)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/nezhahq/service"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -13,6 +14,11 @@ type AgentCliFlags struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
type program struct {
|
||||
exit chan struct{}
|
||||
service service.Service
|
||||
}
|
||||
|
||||
var serviceCmd = &cobra.Command{
|
||||
Use: "service <install/uninstall/start/stop/restart>",
|
||||
Short: "服务与自启动设置",
|
||||
@@ -21,6 +27,33 @@ var serviceCmd = &cobra.Command{
|
||||
PreRun: servicePreRun,
|
||||
}
|
||||
|
||||
func (p *program) Start(s service.Service) error {
|
||||
go p.run()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *program) Stop(s service.Service) error {
|
||||
close(p.exit)
|
||||
if service.Interactive() {
|
||||
os.Exit(0)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *program) run() {
|
||||
defer func() {
|
||||
if service.Interactive() {
|
||||
p.Stop(p.service)
|
||||
} else {
|
||||
p.service.Stop()
|
||||
}
|
||||
}()
|
||||
|
||||
run()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
agentCmd.AddCommand(serviceCmd)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user