@@ -20,12 +20,15 @@ import (
20
20
"context"
21
21
"encoding/json"
22
22
"fmt"
23
+ health2 "github.com/argoproj/gitops-engine/pkg/health"
23
24
argoApplication "github.com/devtron-labs/devtron/client/argocdServer/bean"
25
+ "github.com/devtron-labs/devtron/internal/sql/models"
24
26
commonBean "github.com/devtron-labs/devtron/pkg/deployment/gitOps/common/bean"
25
27
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/config"
26
28
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/git"
27
29
"github.com/devtron-labs/devtron/pkg/deployment/manifest/deploymentTemplate"
28
30
bean4 "github.com/devtron-labs/devtron/pkg/deployment/trigger/devtronApps/bean"
31
+ "github.com/pkg/errors"
29
32
"io/ioutil"
30
33
"net/url"
31
34
"path"
@@ -117,6 +120,7 @@ type AppServiceImpl struct {
117
120
gitOpsConfigReadService config.GitOpsConfigReadService
118
121
gitOperationService git.GitOperationService
119
122
deploymentTemplateService deploymentTemplate.DeploymentTemplateService
123
+ appListingService AppListingService
120
124
}
121
125
122
126
type AppService interface {
@@ -139,6 +143,7 @@ type AppService interface {
139
143
GetDeployedManifestByPipelineIdAndCDWorkflowId (appId int , envId int , cdWorkflowId int , ctx context.Context ) ([]byte , error )
140
144
//SetPipelineFieldsInOverrideRequest(overrideRequest *bean.ValuesOverrideRequest, pipeline *pipelineConfig.Pipeline)
141
145
GetActiveCiCdAppsCount () (int , error )
146
+ ComputeAppstatus (appId , envId int , status health2.HealthStatusCode ) (string , error )
142
147
}
143
148
144
149
func NewAppService (
@@ -162,7 +167,8 @@ func NewAppService(
162
167
installedAppVersionHistoryRepository repository4.InstalledAppVersionHistoryRepository ,
163
168
scopedVariableManager variables.ScopedVariableCMCSManager , acdConfig * argocdServer.ACDConfig ,
164
169
gitOpsConfigReadService config.GitOpsConfigReadService , gitOperationService git.GitOperationService ,
165
- deploymentTemplateService deploymentTemplate.DeploymentTemplateService ) * AppServiceImpl {
170
+ deploymentTemplateService deploymentTemplate.DeploymentTemplateService ,
171
+ appListingService AppListingService ) * AppServiceImpl {
166
172
appServiceImpl := & AppServiceImpl {
167
173
environmentConfigRepository : environmentConfigRepository ,
168
174
mergeUtil : mergeUtil ,
@@ -192,6 +198,7 @@ func NewAppService(
192
198
gitOpsConfigReadService : gitOpsConfigReadService ,
193
199
gitOperationService : gitOperationService ,
194
200
deploymentTemplateService : deploymentTemplateService ,
201
+ appListingService : appListingService ,
195
202
}
196
203
return appServiceImpl
197
204
}
@@ -263,6 +270,42 @@ func (impl *AppServiceImpl) UpdateDeploymentStatusAndCheckIsSucceeded(app *v1alp
263
270
return isSucceeded , pipelineOverride , nil
264
271
}
265
272
273
+ func (impl * AppServiceImpl ) ComputeAppstatus (appId , envId int , status health2.HealthStatusCode ) (string , error ) {
274
+ appStatusInternal := string (status )
275
+
276
+ // get the last accepted deploy type workflow runner, accepted state -> not in (initiated/queued/failed)
277
+ cdWfr , err := impl .cdWorkflowRepository .FindLastUnFailedProcessedRunner (appId , envId )
278
+ if err != nil {
279
+ impl .logger .Errorw ("error in getting latest wfr by appId and envId" , "err" , err , "appId" , appId , "envId" , envId )
280
+ return "" , err
281
+ }
282
+
283
+ override , err := impl .pipelineOverrideRepository .FindLatestByCdWorkflowId (cdWfr .CdWorkflowId )
284
+ if err != nil {
285
+ impl .logger .Errorw ("error in getting latest wfr by pipelineId" , "cdWorkflowId" , cdWfr .CdWorkflowId , "err" , err )
286
+ return "" , err
287
+ }
288
+
289
+ if errors .Is (err , pg .ErrNoRows ) {
290
+ // not deployed
291
+ return appStatusInternal , nil
292
+ }
293
+
294
+ // this is not stop/start type
295
+ if override .DeploymentType != models .DEPLOYMENTTYPE_STOP && override .DeploymentType != models .DEPLOYMENTTYPE_START {
296
+ return appStatusInternal , nil
297
+ }
298
+
299
+ // for stop, then user requested for hibernate, then check for hibernation.
300
+ IslastAcceptedReleaseIsStopRequest := models .DEPLOYMENTTYPE_STOP == override .DeploymentType
301
+ // request essentially means that the previous state of the release was hibernation/partial hibernation
302
+
303
+ if IslastAcceptedReleaseIsStopRequest {
304
+ appStatusInternal = appStatus .GetHibernationStatus (status )
305
+ }
306
+ return appStatusInternal , nil
307
+ }
308
+
266
309
func (impl * AppServiceImpl ) UpdateDeploymentStatusForGitOpsPipelines (app * v1alpha1.Application , statusTime time.Time , isAppStore bool ) (bool , bool , * chartConfig.PipelineOverride , error ) {
267
310
isSucceeded := false
268
311
isTimelineUpdated := false
@@ -296,7 +339,13 @@ func (impl *AppServiceImpl) UpdateDeploymentStatusForGitOpsPipelines(app *v1alph
296
339
impl .logger .Errorw ("error in getting latest timeline before update" , "err" , err , "cdWfrId" , cdWfr .Id )
297
340
return isSucceeded , isTimelineUpdated , pipelineOverride , err
298
341
}
299
- err = impl .appStatusService .UpdateStatusWithAppIdEnvId (cdPipeline .AppId , cdPipeline .EnvironmentId , string (app .Status .Health .Status ))
342
+
343
+ appStatusInternal , err := impl .ComputeAppstatus (cdPipeline .AppId , cdPipeline .EnvironmentId , app .Status .Health .Status )
344
+ if err != nil {
345
+ impl .logger .Errorw ("error in checking if last release is stop type" , "err" , err , cdPipeline .AppId , "envId" , cdPipeline .EnvironmentId )
346
+ return isSucceeded , isTimelineUpdated , pipelineOverride , err
347
+ }
348
+ err = impl .appStatusService .UpdateStatusWithAppIdEnvId (cdPipeline .AppId , cdPipeline .EnvironmentId , appStatusInternal )
300
349
if err != nil {
301
350
impl .logger .Errorw ("error occurred while updating app status in app_status table" , "error" , err , "appId" , cdPipeline .AppId , "envId" , cdPipeline .EnvironmentId )
302
351
}
0 commit comments