mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-09-29 13:22:14 +08:00
More Standardization
This commit is contained in:
@@ -53,7 +53,7 @@ func (p *procApi) StartProcess(ctx *gin.Context, req model.ProcessUuidReq) (err
|
|||||||
}
|
}
|
||||||
logic.ProcessCtlLogic.AddProcess(req.Uuid, proc)
|
logic.ProcessCtlLogic.AddProcess(req.Uuid, proc)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if prod.State.State == 1 {
|
if prod.State.State == 1 {
|
||||||
return errors.New("process is currently running")
|
return errors.New("process is currently running")
|
||||||
}
|
}
|
||||||
|
@@ -263,8 +263,6 @@ func (p *ProcessBase) monitorHanler() {
|
|||||||
if !p.monitor.enable {
|
if !p.monitor.enable {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Logger.AddAdditionalInfo("name", p.Name)
|
|
||||||
log.Logger.AddAdditionalInfo("pid", p.Pid)
|
|
||||||
defer log.Logger.Infow("性能监控结束")
|
defer log.Logger.Infow("性能监控结束")
|
||||||
ticker := time.NewTicker(time.Second * time.Duration(config.CF.PerformanceInfoInterval))
|
ticker := time.NewTicker(time.Second * time.Duration(config.CF.PerformanceInfoInterval))
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
@@ -47,11 +47,11 @@ func (p *processCtlLogic) KillProcess(uuid int) error {
|
|||||||
func (p *processCtlLogic) GetProcess(uuid int) (*ProcessBase, error) {
|
func (p *processCtlLogic) GetProcess(uuid int) (*ProcessBase, error) {
|
||||||
process, ok := p.processMap.Load(uuid)
|
process, ok := p.processMap.Load(uuid)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("进程获取失败")
|
return nil, errors.New("process not exist")
|
||||||
}
|
}
|
||||||
result, ok := process.(*ProcessBase)
|
result, ok := process.(*ProcessBase)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("进程类型错误")
|
return nil, errors.New("process type error")
|
||||||
|
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
|
@@ -30,14 +30,12 @@ func (p *ProcessPty) Type() constants.TerminalType {
|
|||||||
|
|
||||||
func (p *ProcessPty) Start() (err error) {
|
func (p *ProcessPty) Start() (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
log.Logger.DeleteAdditionalInfo(1)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.Config.AutoRestart = false
|
p.Config.AutoRestart = false
|
||||||
p.SetState(constants.PROCESS_WARNNING)
|
p.SetState(constants.PROCESS_WARNNING)
|
||||||
p.State.Info = "进程启动失败:" + err.Error()
|
p.State.Info = "进程启动失败:" + err.Error()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
log.Logger.AddAdditionalInfo("进程名称", p.Name)
|
|
||||||
if ok := p.SetState(constants.PROCESS_START, func() bool {
|
if ok := p.SetState(constants.PROCESS_START, func() bool {
|
||||||
return p.State.State != 1
|
return p.State.State != 1
|
||||||
}); !ok {
|
}); !ok {
|
||||||
|
@@ -38,14 +38,12 @@ func (p *ProcessStd) Write(input string) (err error) {
|
|||||||
|
|
||||||
func (p *ProcessStd) Start() (err error) {
|
func (p *ProcessStd) Start() (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
log.Logger.DeleteAdditionalInfo(1)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.Config.AutoRestart = false
|
p.Config.AutoRestart = false
|
||||||
p.SetState(constants.PROCESS_WARNNING)
|
p.SetState(constants.PROCESS_WARNNING)
|
||||||
p.State.Info = "进程启动失败:" + err.Error()
|
p.State.Info = "进程启动失败:" + err.Error()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
log.Logger.AddAdditionalInfo("进程名称", p.Name)
|
|
||||||
if ok := p.SetState(constants.PROCESS_START, func() bool {
|
if ok := p.SetState(constants.PROCESS_START, func() bool {
|
||||||
return p.State.State != 1
|
return p.State.State != 1
|
||||||
}); !ok {
|
}); !ok {
|
||||||
|
@@ -34,8 +34,6 @@ func (t *taskLogic) run(ctx context.Context, data *model.TaskJob) {
|
|||||||
data.Running = false
|
data.Running = false
|
||||||
middle.TaskWaitCond.Trigger()
|
middle.TaskWaitCond.Trigger()
|
||||||
}()
|
}()
|
||||||
log.Logger.AddAdditionalInfo("taskId", data.Task.Id)
|
|
||||||
defer log.Logger.DeleteAdditionalInfo(1)
|
|
||||||
var ok bool
|
var ok bool
|
||||||
// 判断条件是否满足
|
// 判断条件是否满足
|
||||||
if data.Task.Condition == constants.PASS {
|
if data.Task.Condition == constants.PASS {
|
||||||
|
@@ -54,8 +54,6 @@ func (t *taskLogic) InitTaskJob() {
|
|||||||
|
|
||||||
func (t *taskLogic) cronHandle(data *model.TaskJob) func() {
|
func (t *taskLogic) cronHandle(data *model.TaskJob) func() {
|
||||||
return func() {
|
return func() {
|
||||||
log.Logger.AddAdditionalInfo("id", data.Task.Id)
|
|
||||||
defer log.Logger.DeleteAdditionalInfo(1)
|
|
||||||
log.Logger.Infow("定时任务启动")
|
log.Logger.Infow("定时任务启动")
|
||||||
if data.Running {
|
if data.Running {
|
||||||
log.Logger.Infow("任务已在运行,跳过当前任务")
|
log.Logger.Infow("任务已在运行,跳过当前任务")
|
||||||
|
@@ -67,7 +67,7 @@ func routePathInit(r *gin.Engine) {
|
|||||||
processGroup.DELETE("", middle.OprPermission(constants.OPERATION_STOP), bind(api.ProcApi.KillProcess, Query))
|
processGroup.DELETE("", middle.OprPermission(constants.OPERATION_STOP), bind(api.ProcApi.KillProcess, Query))
|
||||||
processGroup.GET("", bind(api.ProcApi.GetProcessList, None))
|
processGroup.GET("", bind(api.ProcApi.GetProcessList, None))
|
||||||
processGroup.GET("/wait", middle.ProcessWaitCond.WaitGetMiddel, bind(api.ProcApi.GetProcessList, None))
|
processGroup.GET("/wait", middle.ProcessWaitCond.WaitGetMiddel, bind(api.ProcApi.GetProcessList, None))
|
||||||
processGroup.PUT("", middle.OprPermission(constants.OPERATION_START), bind(api.ProcApi.StartProcess, Body))
|
processGroup.PUT("", middle.OprPermission(constants.OPERATION_START), bind(api.ProcApi.StartProcess, Query))
|
||||||
processGroup.PUT("/all", bind(api.ProcApi.StartAllProcess, None))
|
processGroup.PUT("/all", bind(api.ProcApi.StartAllProcess, None))
|
||||||
processGroup.DELETE("/all", bind(api.ProcApi.KillAllProcess, None))
|
processGroup.DELETE("/all", bind(api.ProcApi.KillAllProcess, None))
|
||||||
processGroup.POST("/share", middle.RolePermission(constants.ROLE_ADMIN), bind(api.ProcApi.ProcessCreateShare, Query))
|
processGroup.POST("/share", middle.RolePermission(constants.ROLE_ADMIN), bind(api.ProcApi.ProcessCreateShare, Query))
|
||||||
|
51
log/log.go
51
log/log.go
@@ -5,57 +5,13 @@ import (
|
|||||||
|
|
||||||
"github.com/lzh-1625/go_process_manager/config"
|
"github.com/lzh-1625/go_process_manager/config"
|
||||||
|
|
||||||
"github.com/timandy/routine"
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Logger *logWithAdditional
|
var Logger *zap.SugaredLogger
|
||||||
|
|
||||||
type logWithAdditional struct {
|
|
||||||
*zap.SugaredLogger
|
|
||||||
threadLocal routine.ThreadLocal[[]any]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *logWithAdditional) Infow(msg string, keysAndValues ...interface{}) {
|
|
||||||
keysAndValues = append(keysAndValues, l.threadLocal.Get()...)
|
|
||||||
l.SugaredLogger.WithOptions(zap.AddCallerSkip(1)).Infow(msg, keysAndValues...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *logWithAdditional) Debugw(msg string, keysAndValues ...interface{}) {
|
|
||||||
keysAndValues = append(keysAndValues, l.threadLocal.Get()...)
|
|
||||||
l.SugaredLogger.WithOptions(zap.AddCallerSkip(1)).Debugw(msg, keysAndValues...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *logWithAdditional) Errorw(msg string, keysAndValues ...interface{}) {
|
|
||||||
keysAndValues = append(keysAndValues, l.threadLocal.Get()...)
|
|
||||||
l.SugaredLogger.WithOptions(zap.AddCallerSkip(1)).Errorw(msg, keysAndValues...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *logWithAdditional) Warnw(msg string, keysAndValues ...interface{}) {
|
|
||||||
keysAndValues = append(keysAndValues, l.threadLocal.Get()...)
|
|
||||||
l.SugaredLogger.WithOptions(zap.AddCallerSkip(1)).Warnw(msg, keysAndValues...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *logWithAdditional) AddAdditionalInfo(k, v any) {
|
|
||||||
l.threadLocal.Set(append(l.threadLocal.Get(), k, v))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *logWithAdditional) DeleteAdditionalInfo(layer int) {
|
|
||||||
if layer < 0 {
|
|
||||||
l.threadLocal.Set([]any{})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
oldKv := l.threadLocal.Get()
|
|
||||||
if len(oldKv) < layer*2 {
|
|
||||||
l.threadLocal.Set([]any{})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l.threadLocal.Set(oldKv[:len(oldKv)-2*layer])
|
|
||||||
}
|
|
||||||
|
|
||||||
func InitLog() {
|
func InitLog() {
|
||||||
|
|
||||||
encoderConfig := zapcore.EncoderConfig{
|
encoderConfig := zapcore.EncoderConfig{
|
||||||
TimeKey: "time",
|
TimeKey: "time",
|
||||||
LevelKey: "level",
|
LevelKey: "level",
|
||||||
@@ -89,8 +45,5 @@ func InitLog() {
|
|||||||
ErrorOutputPaths: []string{"stderr"},
|
ErrorOutputPaths: []string{"stderr"},
|
||||||
}
|
}
|
||||||
log, _ := config.Build()
|
log, _ := config.Build()
|
||||||
Logger = &logWithAdditional{
|
Logger = log.Sugar()
|
||||||
SugaredLogger: log.Sugar(),
|
|
||||||
threadLocal: routine.NewThreadLocal[[]any](),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user