Skip to content

Commit

Permalink
fix client request api log output twice
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Jan 16, 2024
1 parent c548c91 commit de85a27
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
6 changes: 1 addition & 5 deletions core/handler/client/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ import (

func Handler(middlewares chi.Middlewares) http.Handler {
r := chi.NewRouter()
r.Use(
middleware.Recoverer,
middleware.NoCache,
middleware.Logger,
)
r.Use(middlewares...)
r.Use(middleware.NoCache)

r.Post("/status", handleStatus)
r.Post("/stage", handleRequest)
Expand Down
36 changes: 23 additions & 13 deletions core/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (w *Worker) Run(ctx context.Context) error {
for {
select {
case <-wCtx.Done():
return context.Canceled
return wCtx.Err()
default:
}

Expand All @@ -106,7 +106,7 @@ func (w *Worker) Run(ctx context.Context) error {
select {
case <-time.After(time.Second * time.Duration(waitSec)):
case <-wCtx.Done():
return err
return wCtx.Err()
}
continue
}
Expand Down Expand Up @@ -209,25 +209,34 @@ func execute(

for _, step := range status.Steps {
stepSpec := spec.GetStep(step.Name)
stepLog := log.With(
"step_id", stepSpec.ID,
"step_name", stepSpec.Name,
)

step.Started = time.Now().Unix()
if stepSpec == nil || failed {
step.Phase = v1.PhaseSkipped
step.Stopped = step.Started

stepLog.Debug("Execute step end request by skipped")
if err := client.StepEnd(ctx, step); err != nil {
return fmt.Errorf("step(%s) end request failed: %v", step.Name, err)
}
continue
}
if canceled {
step.Phase = v1.PhaseCanceled

stepLog.Debug("Execute step end request by canceled")
if err := client.StepEnd(ctx, step); err != nil {
return fmt.Errorf("step(%s) end request failed: %v", step.Name, err)
}
continue
}

step.Phase = v1.PhaseRunning

stepLog.Debug("Execute step begin request")
if err := client.StepBegin(ctx, step); err != nil {
return fmt.Errorf("step(%s) begin request failed: %v", step.Name, err)
}
Expand All @@ -238,13 +247,15 @@ func execute(
}
if err := client.LogUpload(ctx, step.ID, lines, isAll); err != nil {
if errors.Is(ctx.Err(), context.Canceled) {
log.Debug("Upload log canceled")
stepLog.Debug("Upload log canceled")
return
}
log.Error("Upload log failed", "error", err)
stepLog.Error("Upload log failed", "error", err)
}
}
wc := livelog.NewWriter(logHandle)

stepLog.Debug("Execute step hook")
state, err := hook.Step(ctx, spec, stepSpec, wc)
_ = wc.Close()

Expand All @@ -253,32 +264,31 @@ func execute(
if errors.Is(err, context.Canceled) {
step.Phase = v1.PhaseCanceled
canceled = true
log.Debug("Execute step hook cancel", "step", step.Name)
stepLog.Debug("Execute step hook cancel")
} else if err != nil {
step.Phase = v1.PhaseFailed
step.Error = err.Error()
failed = true
log.Error("Execute step hook failed",
"error", err,
"step", step.Name,
)
stepLog.Error("Execute step hook failed")
}

if state != nil {
if state.OOMKilled {
log.Debug("received oom kill.")
stepLog.Debug("received oom kill.")
state.ExitCode = 137
} else {
log.Debugf("received exit code %d", state.ExitCode)
stepLog.Debugf("received exit code %d", state.ExitCode)
}
// if the exit code is 78, the system will skip all
// subsequent pending steps in the pipeline.
if state.ExitCode == 78 {
log.Debug("received exit code 78. early exit.")
stepLog.Debug("received exit code 78. early exit.")
step.Phase = v1.PhaseSkipped
failed = true
}
}

stepLog.Debug("Execute step end request")
if err := client.StepEnd(ctx, step); err != nil {
return fmt.Errorf("step(%s) end request failed: %v", step.Name, err)
}
Expand Down

0 comments on commit de85a27

Please sign in to comment.