@@ -2,12 +2,17 @@ package executor
22
33import (
44 "context"
5- "fmt"
65 "sync"
6+ "time"
77
88 "github.com/harness/lite-engine/api"
99 "github.com/harness/lite-engine/engine"
1010 "github.com/harness/lite-engine/errors"
11+ "github.com/harness/lite-engine/livelog"
12+ "github.com/harness/lite-engine/logstream"
13+ "github.com/harness/lite-engine/pipeline"
14+ "github.com/hashicorp/go-multierror"
15+ "github.com/sirupsen/logrus"
1116
1217 "github.com/drone/runner-go/pipeline/runtime"
1318)
@@ -50,21 +55,15 @@ func (e *StepExecutor) StartStep(ctx context.Context, r *api.StartStepRequest) e
5055 e .mu .Lock ()
5156 _ , ok := e .stepStatus [r .ID ]
5257 if ok {
58+ e .mu .Unlock ()
5359 return nil
5460 }
5561
5662 e .stepStatus [r .ID ] = StepStatus {Status : Running }
5763 e .mu .Unlock ()
5864
5965 go func () {
60- var state * runtime.State
61- var stepErr error
62- if r .Kind == api .Run {
63- state , stepErr = executeRunStep (context .Background (), e .engine , r )
64- } else {
65- executeRunTestStep ()
66- }
67-
66+ state , stepErr := e .executeStep (r )
6867 status := StepStatus {Status : Complete , State : state , StepErr : stepErr }
6968 e .mu .Lock ()
7069 e .stepStatus [r .ID ] = status
@@ -93,6 +92,10 @@ func (e *StepExecutor) PollStep(ctx context.Context, r *api.PollStepRequest) (*a
9392
9493 if s .Status == Complete {
9594 e .mu .Unlock ()
95+ if s .StepErr != nil {
96+ return & api.PollStepResponse {}, & errors.InternalServerError {Msg : s .StepErr .Error ()}
97+ }
98+
9699 return & api.PollStepResponse {
97100 Exited : s .State .Exited ,
98101 ExitCode : s .State .ExitCode ,
@@ -110,12 +113,75 @@ func (e *StepExecutor) PollStep(ctx context.Context, r *api.PollStepRequest) (*a
110113
111114 status := <- ch
112115 if status .StepErr != nil {
113- errMsg := fmt .Sprintf ("failed to execute step with err: %s" , status .StepErr .Error ())
114- return & api.PollStepResponse {}, & errors.InternalServerError {Msg : errMsg }
116+ return & api.PollStepResponse {}, & errors.InternalServerError {Msg : status .StepErr .Error ()}
115117 }
116118 return & api.PollStepResponse {
117119 Exited : status .State .Exited ,
118120 ExitCode : status .State .ExitCode ,
119121 OOMKilled : status .State .OOMKilled ,
120122 }, nil
121123}
124+
125+ func (e * StepExecutor ) executeStep (r * api.StartStepRequest ) (* runtime.State , error ) {
126+ state := pipeline .GetState ()
127+ secrets := append (state .GetSecrets (), r .Secrets ... )
128+
129+ // Create a log stream for step logs
130+ client := state .GetLogStreamClient ()
131+ wc := livelog .New (client , r .LogKey , getNudges ())
132+ wr := logstream .NewReplacer (wc , secrets )
133+ go wr .Open () // nolint:errcheck
134+
135+ // if the step is configured as a daemon, it is detached
136+ // from the main process and executed separately.
137+ if r .Detach {
138+ go func () {
139+ ctx := context .Background ()
140+ var cancel context.CancelFunc
141+ if r .Timeout > 0 {
142+ ctx , cancel = context .WithTimeout (ctx , time .Second * time .Duration (r .Timeout ))
143+ defer cancel ()
144+ }
145+ executeRunStep (ctx , e .engine , r , wr ) // nolint:errcheck
146+ wc .Close ()
147+ }()
148+ return & runtime.State {Exited : false }, nil
149+ }
150+
151+ var result error
152+
153+ ctx := context .Background ()
154+ var cancel context.CancelFunc
155+ if r .Timeout > 0 {
156+ ctx , cancel = context .WithTimeout (ctx , time .Second * time .Duration (r .Timeout ))
157+ defer cancel ()
158+ }
159+
160+ exited , stepErr := executeRunStep (ctx , e .engine , r , wr )
161+ if stepErr != nil {
162+ result = multierror .Append (result , stepErr )
163+ }
164+
165+ // close the stream. If the session is a remote session, the
166+ // full log buffer is uploaded to the remote server.
167+ if err := wc .Close (); err != nil {
168+ result = multierror .Append (result , err )
169+ }
170+
171+ // if the context was canceled and returns a canceled or
172+ // DeadlineExceeded error this indicates the pipeline was
173+ // canceled.
174+ switch ctx .Err () {
175+ case context .Canceled , context .DeadlineExceeded :
176+ return nil , ctx .Err ()
177+ }
178+
179+ if exited != nil {
180+ if exited .OOMKilled {
181+ logrus .WithField ("id" , r .ID ).Infoln ("received oom kill." )
182+ } else {
183+ logrus .WithField ("id" , r .ID ).Infof ("received exit code %d\n " , exited .ExitCode )
184+ }
185+ }
186+ return exited , result
187+ }
0 commit comments