@@ -74,8 +74,8 @@ func NewCiStage(gitManager helper.GitManager, dockerHelper helper.DockerHelper,
7474 }
7575}
7676
77- func deferCIEvent (ciRequest * helper.CommonWorkflowRequest , artifactUploaded bool , err error ) (exitCode int ) {
78- log .Println (util .DEVTRON , "defer CI stage data." , "err: " , err , "artifactUploaded: " , artifactUploaded )
77+ func sendCIEventResult (ciRequest * helper.CommonWorkflowRequest , artifactUploaded bool , err error ) (exitCode int ) {
78+ log .Println (util .DEVTRON , "sendCIEventResult CI stage data." , "err: " , err , "artifactUploaded: " , artifactUploaded )
7979 if err != nil {
8080 var stageError * helper.CiStageError
8181 if errors .As (err , & stageError ) {
@@ -99,13 +99,23 @@ func deferCIEvent(ciRequest *helper.CommonWorkflowRequest, artifactUploaded bool
9999}
100100
101101func (impl * CiStage ) HandleCIEvent (ciCdRequest * helper.CiCdTriggerEvent , exitCode * int ) {
102- var artifactUploaded bool
103- var err error
102+ artifactUploaded , err := impl .handleCIEvent (ciCdRequest )
103+ if err != nil {
104+ //log error and send event result
105+ log .Println ("ci stage error: " , err )
106+ }
107+ * exitCode = sendCIEventResult (ciCdRequest .CommonWorkflowRequest , artifactUploaded , err )
108+ }
109+
110+ func (impl * CiStage ) handleCIEvent (ciCdRequest * helper.CiCdTriggerEvent ) (artifactUploaded bool , err error ) {
111+ defer func () { //recover in this function allows us to send event even if the code crashes
112+ if r := recover (); r != nil {
113+ log .Println ("recovered from panic in handleCIEvent:" , r )
114+ err = fmt .Errorf ("panic occurred during CI event handling" )
115+ }
116+ }()
104117 ciRequest := ciCdRequest .CommonWorkflowRequest
105118 ciContext := cicxt .BuildCiContext (context .Background (), ciRequest .EnableSecretMasking )
106- defer func () {
107- * exitCode = deferCIEvent (ciRequest , artifactUploaded , err )
108- }()
109119 artifactUploaded , err = impl .runCIStages (ciContext , ciCdRequest )
110120 log .Println (util .DEVTRON , artifactUploaded , err )
111121 var artifactUploadErr error
@@ -116,16 +126,16 @@ func (impl *CiStage) HandleCIEvent(ciCdRequest *helper.CiCdTriggerEvent, exitCod
116126
117127 if err != nil {
118128 log .Println (util .DEVTRON , err )
119- return
129+ return artifactUploaded , err
120130 }
121131
122132 if artifactUploadErr != nil {
123133 log .Println (util .DEVTRON , "error in artifact upload: " , artifactUploadErr )
124134 if ciCdRequest .CommonWorkflowRequest .IsExtRun {
125135 log .Println (util .DEVTRON , "Ignoring artifactUploadErr" )
126- return
136+ return artifactUploaded , err
127137 }
128- return
138+ return artifactUploaded , err
129139 }
130140
131141 // sync cache
@@ -148,7 +158,7 @@ func (impl *CiStage) HandleCIEvent(ciCdRequest *helper.CiCdTriggerEvent, exitCod
148158 if err != nil {
149159 log .Println ("error in cache push" , err )
150160 }
151- return
161+ return artifactUploaded , err
152162}
153163
154164// TODO: take as tech debt and break this function into parts for better code readability
@@ -672,17 +682,10 @@ func pullCache(metrics *helper.CIMetrics, ciCdRequest *helper.CiCdTriggerEvent)
672682 log .Println (util .DEVTRON , " cache-pull" )
673683 start := time .Now ()
674684 metrics .CacheDownStartTime = start
675-
676- defer func () {
677- log .Println (util .DEVTRON , " /cache-pull" )
678- metrics .CacheDownDuration = time .Since (start ).Seconds ()
679- }()
680-
681685 err := helper .GetCache (ciCdRequest .CommonWorkflowRequest )
682- if err != nil {
683- return err
684- }
685- return nil
686+ log .Println (util .DEVTRON , " /cache-pull" )
687+ metrics .CacheDownDuration = time .Since (start ).Seconds ()
688+ return err
686689}
687690
688691func (impl * CiStage ) prepareStep (ciCdRequest * helper.CiCdTriggerEvent , metrics * helper.CIMetrics , skipCheckout bool ) error {
0 commit comments