Skip to content

Commit 714107a

Browse files
committed
Remove redundant workflow status update logic for CI/CD workflows and refactor method signatures and repository integration in WorkflowStatusLatestService.
1 parent 4443e57 commit 714107a

File tree

6 files changed

+23
-182
lines changed

6 files changed

+23
-182
lines changed

pkg/pipeline/CiHandler.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -606,18 +606,6 @@ func (impl *CiHandlerImpl) UpdateWorkflow(workflowStatus eventProcessorBean.CiCd
606606
return savedWorkflow.Id, true, err
607607
}
608608

609-
// Update latest status table for CI workflow
610-
// Check if CiPipeline is loaded, if not pass 0 as appId to let the function fetch it
611-
appId := 0
612-
if savedWorkflow.CiPipeline != nil {
613-
appId = savedWorkflow.CiPipeline.AppId
614-
}
615-
err = impl.workflowStatusUpdateService.UpdateCiWorkflowStatusLatest(nil, savedWorkflow.CiPipelineId, appId, savedWorkflow.Id, savedWorkflow.TriggeredBy)
616-
if err != nil {
617-
impl.Logger.Errorw("error in updating ci workflow status latest", "err", err, "pipelineId", savedWorkflow.CiPipelineId, "workflowId", savedWorkflow.Id)
618-
// Don't return error here as the main workflow update was successful
619-
}
620-
621609
impl.sendCIFailEvent(savedWorkflow, status, message)
622610
return savedWorkflow.Id, true, nil
623611
}

pkg/pipeline/CiService.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,7 @@ func (impl *CiServiceImpl) SaveCiWorkflowWithStage(wf *pipelineConfig.CiWorkflow
135135
return err
136136
}
137137

138-
// Update latest status table for CI workflow within the transaction
139-
// Check if CiPipeline is loaded, if not pass 0 as appId to let the function fetch it
140-
appId := 0
141-
if wf.CiPipeline != nil {
142-
appId = wf.CiPipeline.AppId
143-
}
144-
err = impl.workflowStatusUpdateService.UpdateCiWorkflowStatusLatest(tx, wf.CiPipelineId, appId, wf.Id, wf.TriggeredBy)
138+
err = impl.workflowStatusUpdateService.SaveCiWorkflowStatusLatest(tx, wf.CiPipelineId, wf.Id, wf.TriggeredBy)
145139
if err != nil {
146140
impl.Logger.Errorw("error in updating ci workflow status latest", "err", err, "pipelineId", wf.CiPipelineId, "workflowId", wf.Id)
147141
return err
@@ -184,18 +178,6 @@ func (impl *CiServiceImpl) UpdateCiWorkflowWithStage(wf *pipelineConfig.CiWorkfl
184178
return err
185179
}
186180

187-
// Update latest status table for CI workflow within the transaction
188-
// Check if CiPipeline is loaded, if not pass 0 as appId to let the function fetch it
189-
appId := 0
190-
if wf.CiPipeline != nil {
191-
appId = wf.CiPipeline.AppId
192-
}
193-
err = impl.workflowStatusUpdateService.UpdateCiWorkflowStatusLatest(tx, wf.CiPipelineId, appId, wf.Id, wf.TriggeredBy)
194-
if err != nil {
195-
impl.Logger.Errorw("error in updating ci workflow status latest", "err", err, "pipelineId", wf.CiPipelineId, "workflowId", wf.Id)
196-
return err
197-
}
198-
199181
err = impl.transactionManager.CommitTx(tx)
200182
if err != nil {
201183
impl.Logger.Errorw("error in committing transaction", "workflowName", wf.Name, "error", err)

pkg/workflow/cd/CdWorkflowRunnerService.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,6 @@ func (impl *CdWorkflowRunnerServiceImpl) UpdateWfr(dto *bean.CdWorkflowRunnerDto
8080
impl.logger.Errorw("error in updating runner status in db", "runnerId", runnerDbObj.Id, "err", err)
8181
return err
8282
}
83-
84-
// Update latest status table for CD workflow
85-
// Check if CdWorkflow and Pipeline are loaded, if not pass 0 as appId/environmentId to let the function fetch them
86-
appId := 0
87-
environmentId := 0
88-
if runnerDbObj.CdWorkflow != nil && runnerDbObj.CdWorkflow.Pipeline != nil {
89-
appId = runnerDbObj.CdWorkflow.Pipeline.AppId
90-
environmentId = runnerDbObj.CdWorkflow.Pipeline.EnvironmentId
91-
}
92-
err = impl.workflowStatusUpdateService.UpdateCdWorkflowStatusLatest(nil, runnerDbObj.CdWorkflow.PipelineId, appId, environmentId, runnerDbObj.Id, runnerDbObj.WorkflowType.String(), int32(updatedBy))
93-
if err != nil {
94-
impl.logger.Errorw("error in updating cd workflow status latest", "err", err, "pipelineId", runnerDbObj.CdWorkflow.PipelineId, "workflowRunnerId", runnerDbObj.Id)
95-
// Don't return error here as the main workflow update was successful
96-
}
97-
9883
return nil
9984
}
10085

pkg/workflow/workflowStatusLatest/WorkflowStatusLatestService.go

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ import (
3030

3131
type WorkflowStatusLatestService interface {
3232
// CI Workflow Status Latest methods
33-
SaveOrUpdateCiWorkflowStatusLatest(tx *pg.Tx, pipelineId, appId, ciWorkflowId int, userId int32) error
33+
SaveCiWorkflowStatusLatest(tx *pg.Tx, pipelineId, ciWorkflowId int, userId int32) error
3434
GetCiWorkflowStatusLatestByPipelineId(pipelineId int) (*CiWorkflowStatusLatest, error)
3535
GetCiWorkflowStatusLatestByAppId(appId int) ([]*CiWorkflowStatusLatest, error)
3636

3737
// CD Workflow Status Latest methods
38-
SaveOrUpdateCdWorkflowStatusLatest(tx *pg.Tx, pipelineId, appId, environmentId, workflowRunnerId int, workflowType string, userId int32) error
38+
SaveCdWorkflowStatusLatest(tx *pg.Tx, pipelineId, appId, environmentId, workflowRunnerId int, workflowType string, userId int32) error
3939
GetCdWorkflowStatusLatestByPipelineIdAndWorkflowType(pipelineId int, workflowType string) (*CdWorkflowStatusLatest, error)
4040
GetCdWorkflowStatusLatestByAppId(appId int) ([]*CdWorkflowStatusLatest, error)
4141
GetCdWorkflowStatusLatestByPipelineId(pipelineId int) ([]*CdWorkflowStatusLatest, error)
@@ -47,19 +47,22 @@ type WorkflowStatusLatestServiceImpl struct {
4747
workflowStatusLatestRepository pipelineConfig.WorkflowStatusLatestRepository
4848
ciWorkflowRepository pipelineConfig.CiWorkflowRepository
4949
cdWorkflowRepository pipelineConfig.CdWorkflowRepository
50+
ciPipelineRepository pipelineConfig.CiPipelineRepository
5051
}
5152

5253
func NewWorkflowStatusLatestServiceImpl(
5354
logger *zap.SugaredLogger,
5455
workflowStatusLatestRepository pipelineConfig.WorkflowStatusLatestRepository,
5556
ciWorkflowRepository pipelineConfig.CiWorkflowRepository,
5657
cdWorkflowRepository pipelineConfig.CdWorkflowRepository,
58+
ciPipelineRepository pipelineConfig.CiPipelineRepository,
5759
) *WorkflowStatusLatestServiceImpl {
5860
return &WorkflowStatusLatestServiceImpl{
5961
logger: logger,
6062
workflowStatusLatestRepository: workflowStatusLatestRepository,
6163
ciWorkflowRepository: ciWorkflowRepository,
6264
cdWorkflowRepository: cdWorkflowRepository,
65+
ciPipelineRepository: ciPipelineRepository,
6366
}
6467
}
6568

@@ -80,8 +83,7 @@ type CdWorkflowStatusLatest struct {
8083
Status string `json:"status"` // Derived from cd_workflow_runner table
8184
}
8285

83-
// CI Workflow Status Latest methods implementation
84-
func (impl *WorkflowStatusLatestServiceImpl) SaveOrUpdateCiWorkflowStatusLatest(tx *pg.Tx, pipelineId, appId, ciWorkflowId int, userId int32) error {
86+
func (impl *WorkflowStatusLatestServiceImpl) SaveCiWorkflowStatusLatest(tx *pg.Tx, pipelineId, ciWorkflowId int, userId int32) error {
8587
// Validate required parameters
8688
if pipelineId <= 0 {
8789
impl.logger.Errorw("invalid pipelineId provided", "pipelineId", pipelineId)
@@ -92,44 +94,22 @@ func (impl *WorkflowStatusLatestServiceImpl) SaveOrUpdateCiWorkflowStatusLatest(
9294
impl.logger.Errorw("invalid ciWorkflowId provided", "ciWorkflowId", ciWorkflowId)
9395
return fmt.Errorf("invalid ciWorkflowId: %d", ciWorkflowId)
9496
}
95-
96-
// If appId is not provided (0), fetch it from the CiPipeline
97-
if appId <= 0 {
98-
ciPipeline, err := impl.ciWorkflowRepository.FindById(ciWorkflowId)
99-
if err != nil {
100-
impl.logger.Errorw("error in fetching ci workflow to get appId", "err", err, "ciWorkflowId", ciWorkflowId)
101-
return err
102-
}
103-
104-
if ciPipeline == nil {
105-
impl.logger.Errorw("ci workflow not found", "ciWorkflowId", ciWorkflowId)
106-
return fmt.Errorf("ci workflow not found with id: %d", ciWorkflowId)
107-
}
108-
109-
// Check if CiPipeline is loaded
110-
if ciPipeline.CiPipeline == nil {
111-
impl.logger.Errorw("ci pipeline not loaded in ci workflow", "ciWorkflowId", ciWorkflowId, "ciPipelineId", ciPipeline.CiPipelineId)
112-
return fmt.Errorf("ci pipeline not loaded for workflow id: %d", ciWorkflowId)
113-
}
114-
115-
appId = ciPipeline.CiPipeline.AppId
116-
if appId <= 0 {
117-
impl.logger.Errorw("invalid appId in ci pipeline", "ciWorkflowId", ciWorkflowId, "ciPipelineId", ciPipeline.CiPipelineId, "appId", appId)
118-
return fmt.Errorf("invalid appId in ci pipeline: %d", appId)
119-
}
120-
121-
impl.logger.Debugw("fetched appId from ci workflow", "ciWorkflowId", ciWorkflowId, "appId", appId)
97+
ciPipeline, err := impl.ciPipelineRepository.FindOneWithAppData(pipelineId)
98+
if err != nil {
99+
impl.logger.Errorw("error in fetching ci pipeline for appId", "ciPipelineId", pipelineId, "err", err)
100+
return err
122101
}
102+
appId := ciPipeline.AppId
123103

124104
// Check if entry exists
125105
existingEntry, err := impl.workflowStatusLatestRepository.GetCiWorkflowStatusLatestByPipelineId(pipelineId)
126-
if err != nil && err != pg.ErrNoRows {
106+
if err != nil && !util2.IsErrNoRows(err) {
127107
impl.logger.Errorw("error in getting ci workflow status latest", "err", err, "pipelineId", pipelineId)
128108
return err
129109
}
130110

131111
now := time.Now()
132-
if err == pg.ErrNoRows {
112+
if util2.IsErrNoRows(err) {
133113
// Create new entry
134114
model := &pipelineConfig.CiWorkflowStatusLatest{
135115
PipelineId: pipelineId,
@@ -207,7 +187,7 @@ func (impl *WorkflowStatusLatestServiceImpl) GetCiWorkflowStatusLatestByAppId(ap
207187
}
208188

209189
// CD Workflow Status Latest methods implementation
210-
func (impl *WorkflowStatusLatestServiceImpl) SaveOrUpdateCdWorkflowStatusLatest(tx *pg.Tx, pipelineId, appId, environmentId, workflowRunnerId int, workflowType string, userId int32) error {
190+
func (impl *WorkflowStatusLatestServiceImpl) SaveCdWorkflowStatusLatest(tx *pg.Tx, pipelineId, appId, environmentId, workflowRunnerId int, workflowType string, userId int32) error {
211191
// Validate required parameters
212192
if pipelineId <= 0 {
213193
impl.logger.Errorw("invalid pipelineId provided", "pipelineId", pipelineId)

pkg/workflow/workflowStatusLatest/WorkflowStatusUpdateService.go

Lines changed: 4 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ import (
2424

2525
type WorkflowStatusUpdateService interface {
2626
// Methods to update latest status tables when workflow status changes
27-
UpdateCiWorkflowStatusLatest(tx *pg.Tx, pipelineId, appId, ciWorkflowId int, userId int32) error
27+
SaveCiWorkflowStatusLatest(tx *pg.Tx, pipelineId, ciWorkflowId int, userId int32) error
2828
UpdateCdWorkflowStatusLatest(tx *pg.Tx, pipelineId, appId, environmentId, workflowRunnerId int, workflowType string, userId int32) error
29-
30-
// Methods to fetch optimized status for trigger view
31-
FetchCiStatusForTriggerViewOptimized(appId int) ([]*pipelineConfig.CiWorkflowStatus, error)
32-
FetchCdStatusForTriggerViewOptimized(appId int) ([]*pipelineConfig.CdWorkflowStatus, error)
3329
}
3430

3531
type WorkflowStatusUpdateServiceImpl struct {
@@ -59,101 +55,10 @@ func NewWorkflowStatusUpdateServiceImpl(
5955
}
6056
}
6157

62-
func (impl *WorkflowStatusUpdateServiceImpl) UpdateCiWorkflowStatusLatest(tx *pg.Tx, pipelineId, appId, ciWorkflowId int, userId int32) error {
63-
return impl.workflowStatusLatestService.SaveOrUpdateCiWorkflowStatusLatest(tx, pipelineId, appId, ciWorkflowId, userId)
58+
func (impl *WorkflowStatusUpdateServiceImpl) SaveCiWorkflowStatusLatest(tx *pg.Tx, pipelineId, ciWorkflowId int, userId int32) error {
59+
return impl.workflowStatusLatestService.SaveCiWorkflowStatusLatest(tx, pipelineId, ciWorkflowId, userId)
6460
}
6561

6662
func (impl *WorkflowStatusUpdateServiceImpl) UpdateCdWorkflowStatusLatest(tx *pg.Tx, pipelineId, appId, environmentId, workflowRunnerId int, workflowType string, userId int32) error {
67-
return impl.workflowStatusLatestService.SaveOrUpdateCdWorkflowStatusLatest(tx, pipelineId, appId, environmentId, workflowRunnerId, workflowType, userId)
68-
}
69-
70-
func (impl *WorkflowStatusUpdateServiceImpl) FetchCiStatusForTriggerViewOptimized(appId int) ([]*pipelineConfig.CiWorkflowStatus, error) {
71-
latestStatuses, err := impl.workflowStatusLatestService.GetCiWorkflowStatusLatestByAppId(appId)
72-
if err != nil {
73-
impl.logger.Errorw("error in getting ci workflow status latest by app id", "err", err, "appId", appId)
74-
// Fallback to old method
75-
return impl.ciWorkflowRepository.FIndCiWorkflowStatusesByAppId(appId)
76-
}
77-
78-
// Convert to the expected format
79-
var ciWorkflowStatuses []*pipelineConfig.CiWorkflowStatus
80-
for _, latestStatus := range latestStatuses {
81-
ciPipeline, err := impl.ciPipelineRepository.FindById(latestStatus.PipelineId)
82-
if err != nil {
83-
impl.logger.Errorw("error in getting ci pipeline", "err", err, "pipelineId", latestStatus.PipelineId)
84-
continue
85-
}
86-
87-
ciWorkflowStatus := &pipelineConfig.CiWorkflowStatus{
88-
CiPipelineId: latestStatus.PipelineId,
89-
CiPipelineName: ciPipeline.Name,
90-
CiStatus: latestStatus.Status,
91-
CiWorkflowId: latestStatus.CiWorkflowId,
92-
StorageConfigured: latestStatus.StorageConfigured,
93-
}
94-
ciWorkflowStatuses = append(ciWorkflowStatuses, ciWorkflowStatus)
95-
}
96-
97-
// If no entries found in latest status table, fallback to old method
98-
if len(ciWorkflowStatuses) == 0 {
99-
impl.logger.Infow("no entries found in ci workflow status latest table, falling back to old method", "appId", appId)
100-
return impl.ciWorkflowRepository.FIndCiWorkflowStatusesByAppId(appId)
101-
}
102-
103-
return ciWorkflowStatuses, nil
104-
}
105-
106-
func (impl *WorkflowStatusUpdateServiceImpl) FetchCdStatusForTriggerViewOptimized(appId int) ([]*pipelineConfig.CdWorkflowStatus, error) {
107-
// First try to get from the optimized latest status table
108-
latestStatuses, err := impl.workflowStatusLatestService.GetCdWorkflowStatusLatestByAppId(appId)
109-
if err != nil {
110-
impl.logger.Errorw("error in getting cd workflow status latest by app id", "err", err, "appId", appId)
111-
// Fallback to old method - would need to implement this based on existing CD status fetching logic
112-
return nil, err
113-
}
114-
115-
// Convert to the expected format
116-
var cdWorkflowStatuses []*pipelineConfig.CdWorkflowStatus
117-
for _, latestStatus := range latestStatuses {
118-
// Get pipeline info
119-
pipeline, err := impl.pipelineRepository.FindById(latestStatus.PipelineId)
120-
if err != nil {
121-
impl.logger.Errorw("error in getting pipeline", "err", err, "pipelineId", latestStatus.PipelineId)
122-
continue
123-
}
124-
125-
var status string
126-
switch latestStatus.WorkflowType {
127-
case "PRE":
128-
status = latestStatus.Status
129-
case "DEPLOY":
130-
status = latestStatus.Status
131-
case "POST":
132-
status = latestStatus.Status
133-
default:
134-
status = latestStatus.Status
135-
}
136-
137-
cdWorkflowStatus := &pipelineConfig.CdWorkflowStatus{
138-
CiPipelineId: pipeline.CiPipelineId,
139-
PipelineId: latestStatus.PipelineId,
140-
PipelineName: pipeline.Name,
141-
WorkflowType: latestStatus.WorkflowType,
142-
WfrId: latestStatus.WorkflowRunnerId,
143-
}
144-
145-
// Set the appropriate status field based on workflow type
146-
switch latestStatus.WorkflowType {
147-
case "PRE":
148-
cdWorkflowStatus.PreStatus = status
149-
case "DEPLOY":
150-
cdWorkflowStatus.DeployStatus = status
151-
case "POST":
152-
cdWorkflowStatus.PostStatus = status
153-
}
154-
155-
cdWorkflowStatuses = append(cdWorkflowStatuses, cdWorkflowStatus)
156-
}
157-
158-
return cdWorkflowStatuses, nil
63+
return impl.workflowStatusLatestService.SaveCdWorkflowStatusLatest(tx, pipelineId, appId, environmentId, workflowRunnerId, workflowType, userId)
15964
}

0 commit comments

Comments
 (0)