More Standardization

This commit is contained in:
akrike
2025-07-06 19:52:00 +08:00
parent ec9f1fefdb
commit 0db3e60460
9 changed files with 6 additions and 63 deletions

View File

@@ -53,7 +53,7 @@ func (p *procApi) StartProcess(ctx *gin.Context, req model.ProcessUuidReq) (err
}
logic.ProcessCtlLogic.AddProcess(req.Uuid, proc)
return nil
}
}
if prod.State.State == 1 {
return errors.New("process is currently running")
}

View File

@@ -263,8 +263,6 @@ func (p *ProcessBase) monitorHanler() {
if !p.monitor.enable {
return
}
log.Logger.AddAdditionalInfo("name", p.Name)
log.Logger.AddAdditionalInfo("pid", p.Pid)
defer log.Logger.Infow("性能监控结束")
ticker := time.NewTicker(time.Second * time.Duration(config.CF.PerformanceInfoInterval))
defer ticker.Stop()

View File

@@ -47,11 +47,11 @@ func (p *processCtlLogic) KillProcess(uuid int) error {
func (p *processCtlLogic) GetProcess(uuid int) (*ProcessBase, error) {
process, ok := p.processMap.Load(uuid)
if !ok {
return nil, errors.New("进程获取失败")
return nil, errors.New("process not exist")
}
result, ok := process.(*ProcessBase)
if !ok {
return nil, errors.New("进程类型错误")
return nil, errors.New("process type error")
}
return result, nil

View File

@@ -30,14 +30,12 @@ func (p *ProcessPty) Type() constants.TerminalType {
func (p *ProcessPty) Start() (err error) {
defer func() {
log.Logger.DeleteAdditionalInfo(1)
if err != nil {
p.Config.AutoRestart = false
p.SetState(constants.PROCESS_WARNNING)
p.State.Info = "进程启动失败:" + err.Error()
}
}()
log.Logger.AddAdditionalInfo("进程名称", p.Name)
if ok := p.SetState(constants.PROCESS_START, func() bool {
return p.State.State != 1
}); !ok {

View File

@@ -38,14 +38,12 @@ func (p *ProcessStd) Write(input string) (err error) {
func (p *ProcessStd) Start() (err error) {
defer func() {
log.Logger.DeleteAdditionalInfo(1)
if err != nil {
p.Config.AutoRestart = false
p.SetState(constants.PROCESS_WARNNING)
p.State.Info = "进程启动失败:" + err.Error()
}
}()
log.Logger.AddAdditionalInfo("进程名称", p.Name)
if ok := p.SetState(constants.PROCESS_START, func() bool {
return p.State.State != 1
}); !ok {

View File

@@ -34,8 +34,6 @@ func (t *taskLogic) run(ctx context.Context, data *model.TaskJob) {
data.Running = false
middle.TaskWaitCond.Trigger()
}()
log.Logger.AddAdditionalInfo("taskId", data.Task.Id)
defer log.Logger.DeleteAdditionalInfo(1)
var ok bool
// 判断条件是否满足
if data.Task.Condition == constants.PASS {

View File

@@ -54,8 +54,6 @@ func (t *taskLogic) InitTaskJob() {
func (t *taskLogic) cronHandle(data *model.TaskJob) func() {
return func() {
log.Logger.AddAdditionalInfo("id", data.Task.Id)
defer log.Logger.DeleteAdditionalInfo(1)
log.Logger.Infow("定时任务启动")
if data.Running {
log.Logger.Infow("任务已在运行,跳过当前任务")

View File

@@ -67,7 +67,7 @@ func routePathInit(r *gin.Engine) {
processGroup.DELETE("", middle.OprPermission(constants.OPERATION_STOP), bind(api.ProcApi.KillProcess, Query))
processGroup.GET("", 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.DELETE("/all", bind(api.ProcApi.KillAllProcess, None))
processGroup.POST("/share", middle.RolePermission(constants.ROLE_ADMIN), bind(api.ProcApi.ProcessCreateShare, Query))

View File

@@ -5,57 +5,13 @@ import (
"github.com/lzh-1625/go_process_manager/config"
"github.com/timandy/routine"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var Logger *logWithAdditional
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])
}
var Logger *zap.SugaredLogger
func InitLog() {
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
@@ -89,8 +45,5 @@ func InitLog() {
ErrorOutputPaths: []string{"stderr"},
}
log, _ := config.Build()
Logger = &logWithAdditional{
SugaredLogger: log.Sugar(),
threadLocal: routine.NewThreadLocal[[]any](),
}
Logger = log.Sugar()
}