Skip to content

Commit 0f7d2fa

Browse files
Fix: optimizing when querying DB to get running Helm deployments (#3637)
* fix GetLatestTriggersOfHelmPipelinesStuckInNonTerminalStatuses query * query fix * fixed query
1 parent 0bb20d6 commit 0f7d2fa

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

client/cron/CdApplicationStatusUpdateHandler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ func (impl *CdApplicationStatusUpdateHandlerImpl) HelmApplicationStatusUpdate()
166166
impl.logger.Errorw("error in converting string to int", "err", err)
167167
return
168168
}
169-
err = impl.CdHandler.CheckHelmAppStatusPeriodicallyAndUpdateInDb(HelmPipelineStatusCheckEligibleTime)
169+
getPipelineDeployedWithinHours := impl.AppStatusConfig.GetPipelineDeployedWithinHours
170+
err = impl.CdHandler.CheckHelmAppStatusPeriodicallyAndUpdateInDb(HelmPipelineStatusCheckEligibleTime, getPipelineDeployedWithinHours)
170171
if err != nil {
171172
impl.logger.Errorw("error helm app status update - cron job", "err", err)
172173
return

internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type CdWorkflowRepository interface {
6969

7070
FetchArtifactsByCdPipelineId(pipelineId int, runnerType bean.WorkflowType, offset, limit int) ([]CdWorkflowRunner, error)
7171

72-
GetLatestTriggersOfHelmPipelinesStuckInNonTerminalStatuses() ([]*CdWorkflowRunner, error)
72+
GetLatestTriggersOfHelmPipelinesStuckInNonTerminalStatuses(getPipelineDeployedWithinHours int) ([]*CdWorkflowRunner, error)
7373
}
7474

7575
type CdWorkflowRepositoryImpl struct {
@@ -575,18 +575,19 @@ func (impl *CdWorkflowRepositoryImpl) FetchArtifactsByCdPipelineId(pipelineId in
575575
return wfrList, err
576576
}
577577

578-
func (impl *CdWorkflowRepositoryImpl) GetLatestTriggersOfHelmPipelinesStuckInNonTerminalStatuses() ([]*CdWorkflowRunner, error) {
578+
func (impl *CdWorkflowRepositoryImpl) GetLatestTriggersOfHelmPipelinesStuckInNonTerminalStatuses(getPipelineDeployedWithinHours int) ([]*CdWorkflowRunner, error) {
579579
var wfrList []*CdWorkflowRunner
580580
err := impl.dbConnection.
581581
Model(&wfrList).
582-
Column("cd_workflow_runner.*", "CdWorkflow", "CdWorkflow.Pipeline", "CdWorkflow.Pipeline.Environment").
582+
Column("cd_workflow_runner.*", "CdWorkflow.id", "CdWorkflow.pipeline_id", "CdWorkflow.Pipeline.id", "CdWorkflow.Pipeline.deployment_app_name", "CdWorkflow.Pipeline.deployment_app_type", "CdWorkflow.Pipeline.deleted", "CdWorkflow.Pipeline.Environment").
583583
Join("inner join cd_workflow wf on wf.id = cd_workflow_runner.cd_workflow_id").
584584
Join("inner join pipeline p on p.id = wf.pipeline_id").
585585
Join("inner join environment e on e.id = p.environment_id").
586586
Where("cd_workflow_runner.workflow_type=?", bean.CD_WORKFLOW_TYPE_DEPLOY).
587587
Where("cd_workflow_runner.status not in (?)", pg.In([]string{WorkflowAborted, WorkflowFailed, WorkflowSucceeded, application.HIBERNATING, string(health.HealthStatusHealthy), string(health.HealthStatusDegraded)})).
588588
Where("cd_workflow_runner.cd_workflow_id in (select DISTINCT ON (pipeline_id) max(id) as id from cd_workflow group by pipeline_id, id order by pipeline_id, id desc)").
589589
Where("p.deployment_app_type = ?", util.PIPELINE_DEPLOYMENT_TYPE_HELM).
590+
Where("cd_workflow_runner.started_on > NOW() - INTERVAL '? hours'", getPipelineDeployedWithinHours).
590591
Where("p.deleted=?", false).
591592
Order("cd_workflow_runner.id DESC").
592593
Select()

internal/sql/repository/pipelineConfig/PipelineRepository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ func (impl PipelineRepositoryImpl) GetArgoPipelinesHavingTriggersStuckInLastPoss
572572

573573
func (impl PipelineRepositoryImpl) GetArgoPipelinesHavingLatestTriggerStuckInNonTerminalStatuses(getPipelineDeployedBeforeMinutes int, getPipelineDeployedWithinHours int) ([]*Pipeline, error) {
574574
var pipelines []*Pipeline
575-
queryString := `select p.* from pipeline p inner join cd_workflow cw on cw.pipeline_id = p.id
575+
queryString := `select p.id from pipeline p inner join cd_workflow cw on cw.pipeline_id = p.id
576576
inner join cd_workflow_runner cwr on cwr.cd_workflow_id=cw.id
577577
where cwr.id in (select id from cd_workflow_runner
578578
where started_on < NOW() - INTERVAL '? minutes' and started_on > NOW() - INTERVAL '? hours' and status not in (?)

pkg/pipeline/CdHandler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type CdHandler interface {
7171
FetchCdPrePostStageStatus(pipelineId int) ([]pipelineConfig.CdWorkflowWithArtifact, error)
7272
CancelStage(workflowRunnerId int, userId int32) (int, error)
7373
FetchAppWorkflowStatusForTriggerView(appId int) ([]*pipelineConfig.CdWorkflowStatus, error)
74-
CheckHelmAppStatusPeriodicallyAndUpdateInDb(helmPipelineStatusCheckEligibleTime int) error
74+
CheckHelmAppStatusPeriodicallyAndUpdateInDb(helmPipelineStatusCheckEligibleTime int, getPipelineDeployedWithinHours int) error
7575
CheckArgoAppStatusPeriodicallyAndUpdateInDb(getPipelineDeployedBeforeMinutes int, getPipelineDeployedWithinHours int) error
7676
CheckArgoPipelineTimelineStatusPeriodicallyAndUpdateInDb(pendingSinceSeconds int, timeForDegradation int) error
7777
UpdatePipelineTimelineAndStatusByLiveApplicationFetch(pipeline *pipelineConfig.Pipeline, installedApp repository3.InstalledApps, userId int32) (err error, isTimelineUpdated bool)
@@ -462,8 +462,8 @@ func (impl *CdHandlerImpl) UpdatePipelineTimelineAndStatusByLiveApplicationFetch
462462
return nil, isTimelineUpdated
463463
}
464464

465-
func (impl *CdHandlerImpl) CheckHelmAppStatusPeriodicallyAndUpdateInDb(helmPipelineStatusCheckEligibleTime int) error {
466-
wfrList, err := impl.cdWorkflowRepository.GetLatestTriggersOfHelmPipelinesStuckInNonTerminalStatuses()
465+
func (impl *CdHandlerImpl) CheckHelmAppStatusPeriodicallyAndUpdateInDb(helmPipelineStatusCheckEligibleTime int, getPipelineDeployedWithinHours int) error {
466+
wfrList, err := impl.cdWorkflowRepository.GetLatestTriggersOfHelmPipelinesStuckInNonTerminalStatuses(getPipelineDeployedWithinHours)
467467
if err != nil {
468468
impl.Logger.Errorw("error in getting latest triggers of helm pipelines which are stuck in non terminal statuses", "err", err)
469469
return err

0 commit comments

Comments
 (0)