Skip to content

Commit

Permalink
feat: add new scanerr type: KindMavenTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed Apr 29, 2024
1 parent ee0368b commit ad4dffa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
16 changes: 11 additions & 5 deletions module/maven/mvn_graph_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (

// PluginGraphCmd helper to com.github.ferstl:depgraph-maven-plugin:4.0.1:graph
type PluginGraphCmd struct {
Profiles []string
Timeout time.Duration
ScanDir string
MavenCmdInfo *MvnCommandInfo
ErrText string
Profiles []string
Timeout time.Duration
ScanDir string
MavenCmdInfo *MvnCommandInfo
ErrText string
TimeoutKilled bool
}

//func multiWriteCloser(writer ...io.Writer)io.WriteCloser {
Expand Down Expand Up @@ -65,12 +66,17 @@ func (m *PluginGraphCmd) RunC(ctx context.Context) error {
return errors.WithMessage(ErrMvnCmd, e.Error())
}

timeoutToKilledCh := make(chan bool, 1)
defer func() { m.TimeoutKilled = <-timeoutToKilledCh }()
timerCtx, timerCancel := context.WithCancel(ctx)
defer func() { timerCancel() }()
go func() {
var timeoutKilled = false
defer func() { timeoutToKilledCh <- timeoutKilled }()
for timerCtx.Err() == nil {
if logStream.LastLineTimestamp.Load() != nil && time.Since(*logStream.LastLineTimestamp.Load()) > env.CommandTimeout {
logger.Warn("Maven stop print logs, killed")
timeoutKilled = true
utils.KillProcessGroup(c.Process.Pid)
_ = c.Process.Kill()
}
Expand Down
16 changes: 10 additions & 6 deletions module/maven/plugin_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ func ScanDepsByPluginCommand(ctx context.Context, projectDir string, mvnCmdInfo
ScanDir: projectDir,
}
if e := c.RunC(ctx); e != nil {
if c.ErrText != "" {
logger.Sugar().Warnf("error text recorded, size: %d", len(c.ErrText))
scanerr.Add(ctx, scanerr.Param{
Kind: scanerr.KindMavenFailed,
Content: c.ErrText,
})
logger.Sugar().Warnf("error text recorded, size: %d", len(c.ErrText))
var scanerrKind string
if c.TimeoutKilled {
scanerrKind = scanerr.KindMavenTimeout
} else {
scanerrKind = scanerr.KindMavenFailed
}
scanerr.Add(ctx, scanerr.Param{
Kind: scanerrKind,
Content: c.ErrText,
})
logger.Sugar().Error("Maven graph command execution failed", zap.Error(e))
return nil, e
}
Expand Down
1 change: 1 addition & 0 deletions scanerr/scan_err.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ func GetAll(ctx context.Context) []Param {
const KindMavenNotFound = "mvn_not_found"
const KindMavenFailed = "mvn_failed"
const KindBuildDisabled = "build_disabled"
const KindMavenTimeout = "mvn_timeout_killed"

0 comments on commit ad4dffa

Please sign in to comment.