Skip to content

Commit bc7e274

Browse files
committed
Refactor WorkflowStatusLatestService to derive status from workflow runner tables and remove redundant status parameters. Integrate CI status optimization fallback in CiHandlerImpl.
1 parent b97e7e4 commit bc7e274

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

pkg/pipeline/CiHandler.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"github.com/devtron-labs/devtron/pkg/pipeline/executors"
5050
"github.com/devtron-labs/devtron/pkg/pipeline/types"
5151
"github.com/devtron-labs/devtron/pkg/resourceGroup"
52+
workflowStatusLatest "github.com/devtron-labs/devtron/pkg/workflow/status"
5253
"github.com/devtron-labs/devtron/util/rbac"
5354
"github.com/go-pg/pg"
5455
"go.uber.org/zap"
@@ -97,13 +98,15 @@ type CiHandlerImpl struct {
9798
config *types.CiConfig
9899
k8sCommonService k8sPkg.K8sCommonService
99100
workFlowStageStatusService workflowStatus.WorkFlowStageStatusService
101+
workflowStatusUpdateService workflowStatusLatest.WorkflowStatusUpdateService
100102
}
101103

102104
func NewCiHandlerImpl(Logger *zap.SugaredLogger, ciService CiService, ciPipelineMaterialRepository pipelineConfig.CiPipelineMaterialRepository, gitSensorClient gitSensor.Client, ciWorkflowRepository pipelineConfig.CiWorkflowRepository,
103105
ciArtifactRepository repository.CiArtifactRepository, userService user.UserService, eventClient client.EventClient, eventFactory client.EventFactory, ciPipelineRepository pipelineConfig.CiPipelineRepository,
104106
appListingRepository repository.AppListingRepository, cdPipelineRepository pipelineConfig.PipelineRepository, enforcerUtil rbac.EnforcerUtil, resourceGroupService resourceGroup.ResourceGroupService, envRepository repository2.EnvironmentRepository,
105107
imageTaggingService imageTagging.ImageTaggingService, k8sCommonService k8sPkg.K8sCommonService, appWorkflowRepository appWorkflow.AppWorkflowRepository, customTagService CustomTagService,
106108
workFlowStageStatusService workflowStatus.WorkFlowStageStatusService,
109+
workflowStatusUpdateService workflowStatusLatest.WorkflowStatusUpdateService,
107110
) *CiHandlerImpl {
108111
cih := &CiHandlerImpl{
109112
Logger: Logger,
@@ -126,6 +129,7 @@ func NewCiHandlerImpl(Logger *zap.SugaredLogger, ciService CiService, ciPipeline
126129
appWorkflowRepository: appWorkflowRepository,
127130
k8sCommonService: k8sCommonService,
128131
workFlowStageStatusService: workFlowStageStatusService,
132+
workflowStatusUpdateService: workflowStatusUpdateService,
129133
}
130134
config, err := types.GetCiConfig()
131135
if err != nil {
@@ -644,10 +648,16 @@ func (impl *CiHandlerImpl) stateChanged(status string, podStatus string, msg str
644648
}
645649

646650
func (impl *CiHandlerImpl) FetchCiStatusForTriggerViewV1(appId int) ([]*pipelineConfig.CiWorkflowStatus, error) {
647-
ciWorkflowStatuses, err := impl.ciWorkflowRepository.FIndCiWorkflowStatusesByAppId(appId)
648-
if err != nil && !util.IsErrNoRows(err) {
649-
impl.Logger.Errorw("err in fetching ciWorkflowStatuses from ciWorkflowRepository", "appId", appId, "err", err)
650-
return ciWorkflowStatuses, err
651+
// Try to use the optimized workflow status latest service first
652+
ciWorkflowStatuses, err := impl.workflowStatusUpdateService.FetchCiStatusForTriggerViewOptimized(appId)
653+
if err != nil {
654+
impl.Logger.Errorw("error in fetching ci status from optimized service, falling back to old method", "appId", appId, "err", err)
655+
// Fallback to old method if optimized service fails
656+
ciWorkflowStatuses, err = impl.ciWorkflowRepository.FIndCiWorkflowStatusesByAppId(appId)
657+
if err != nil && !util.IsErrNoRows(err) {
658+
impl.Logger.Errorw("err in fetching ciWorkflowStatuses from ciWorkflowRepository", "appId", appId, "err", err)
659+
return ciWorkflowStatuses, err
660+
}
651661
}
652662

653663
return ciWorkflowStatuses, err

pkg/workflow/status/WorkflowStatusLatestService.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (impl *WorkflowStatusLatestServiceImpl) GetCiWorkflowStatusLatestByAppId(ap
164164
}
165165

166166
// CD Workflow Status Latest methods implementation
167-
func (impl *WorkflowStatusLatestServiceImpl) SaveOrUpdateCdWorkflowStatusLatest(pipelineId, appId, environmentId, workflowRunnerId int, workflowType, status string, userId int32) error {
167+
func (impl *WorkflowStatusLatestServiceImpl) SaveOrUpdateCdWorkflowStatusLatest(pipelineId, appId, environmentId, workflowRunnerId int, workflowType string, userId int32) error {
168168
// Check if entry exists
169169
existingEntry, err := impl.workflowStatusLatestRepository.GetCdWorkflowStatusLatestByPipelineIdAndWorkflowType(pipelineId, workflowType)
170170
if err != nil && err != pg.ErrNoRows {
@@ -181,7 +181,6 @@ func (impl *WorkflowStatusLatestServiceImpl) SaveOrUpdateCdWorkflowStatusLatest(
181181
EnvironmentId: environmentId,
182182
WorkflowType: workflowType,
183183
WorkflowRunnerId: workflowRunnerId,
184-
Status: status,
185184
}
186185
model.CreatedBy = userId
187186
model.CreatedOn = now
@@ -192,7 +191,6 @@ func (impl *WorkflowStatusLatestServiceImpl) SaveOrUpdateCdWorkflowStatusLatest(
192191
} else {
193192
// Update existing entry
194193
existingEntry.WorkflowRunnerId = workflowRunnerId
195-
existingEntry.Status = status
196194
existingEntry.UpdatedBy = userId
197195
existingEntry.UpdatedOn = now
198196

@@ -211,13 +209,20 @@ func (impl *WorkflowStatusLatestServiceImpl) GetCdWorkflowStatusLatestByPipeline
211209
return nil, err
212210
}
213211

212+
// Get status from cd_workflow_runner table
213+
cdWorkflowRunner, err := impl.cdWorkflowRepository.FindBasicWorkflowRunnerById(model.WorkflowRunnerId)
214+
if err != nil {
215+
impl.logger.Errorw("error in getting cd workflow runner", "err", err, "workflowRunnerId", model.WorkflowRunnerId)
216+
return nil, err
217+
}
218+
214219
return &CdWorkflowStatusLatest{
215220
PipelineId: model.PipelineId,
216221
AppId: model.AppId,
217222
EnvironmentId: model.EnvironmentId,
218223
WorkflowType: model.WorkflowType,
219224
WorkflowRunnerId: model.WorkflowRunnerId,
220-
Status: model.Status,
225+
Status: cdWorkflowRunner.Status,
221226
}, nil
222227
}
223228

@@ -230,13 +235,20 @@ func (impl *WorkflowStatusLatestServiceImpl) GetCdWorkflowStatusLatestByAppId(ap
230235

231236
var result []*CdWorkflowStatusLatest
232237
for _, model := range models {
238+
// Get status from cd_workflow_runner table
239+
cdWorkflowRunner, err := impl.cdWorkflowRepository.FindBasicWorkflowRunnerById(model.WorkflowRunnerId)
240+
if err != nil {
241+
impl.logger.Errorw("error in getting cd workflow runner", "err", err, "workflowRunnerId", model.WorkflowRunnerId)
242+
continue // Skip this entry if we can't get the workflow runner
243+
}
244+
233245
result = append(result, &CdWorkflowStatusLatest{
234246
PipelineId: model.PipelineId,
235247
AppId: model.AppId,
236248
EnvironmentId: model.EnvironmentId,
237249
WorkflowType: model.WorkflowType,
238250
WorkflowRunnerId: model.WorkflowRunnerId,
239-
Status: model.Status,
251+
Status: cdWorkflowRunner.Status,
240252
})
241253
}
242254

@@ -252,13 +264,20 @@ func (impl *WorkflowStatusLatestServiceImpl) GetCdWorkflowStatusLatestByPipeline
252264

253265
var result []*CdWorkflowStatusLatest
254266
for _, model := range models {
267+
// Get status from cd_workflow_runner table
268+
cdWorkflowRunner, err := impl.cdWorkflowRepository.FindBasicWorkflowRunnerById(model.WorkflowRunnerId)
269+
if err != nil {
270+
impl.logger.Errorw("error in getting cd workflow runner", "err", err, "workflowRunnerId", model.WorkflowRunnerId)
271+
continue // Skip this entry if we can't get the workflow runner
272+
}
273+
255274
result = append(result, &CdWorkflowStatusLatest{
256275
PipelineId: model.PipelineId,
257276
AppId: model.AppId,
258277
EnvironmentId: model.EnvironmentId,
259278
WorkflowType: model.WorkflowType,
260279
WorkflowRunnerId: model.WorkflowRunnerId,
261-
Status: model.Status,
280+
Status: cdWorkflowRunner.Status,
262281
})
263282
}
264283

pkg/workflow/status/WorkflowStatusUpdateService.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323

2424
type WorkflowStatusUpdateService interface {
2525
// Methods to update latest status tables when workflow status changes
26-
UpdateCiWorkflowStatusLatest(pipelineId, appId, ciWorkflowId int, status string, userId int32) error
27-
UpdateCdWorkflowStatusLatest(pipelineId, appId, environmentId, workflowRunnerId int, workflowType, status string, userId int32) error
26+
UpdateCiWorkflowStatusLatest(pipelineId, appId, ciWorkflowId int, userId int32) error
27+
UpdateCdWorkflowStatusLatest(pipelineId, appId, environmentId, workflowRunnerId int, workflowType string, userId int32) error
2828

2929
// Methods to fetch optimized status for trigger view
3030
FetchCiStatusForTriggerViewOptimized(appId int) ([]*pipelineConfig.CiWorkflowStatus, error)
@@ -58,12 +58,12 @@ func NewWorkflowStatusUpdateServiceImpl(
5858
}
5959
}
6060

61-
func (impl *WorkflowStatusUpdateServiceImpl) UpdateCiWorkflowStatusLatest(pipelineId, appId, ciWorkflowId int, status string, userId int32) error {
62-
return impl.workflowStatusLatestService.SaveOrUpdateCiWorkflowStatusLatest(pipelineId, appId, ciWorkflowId, status, userId)
61+
func (impl *WorkflowStatusUpdateServiceImpl) UpdateCiWorkflowStatusLatest(pipelineId, appId, ciWorkflowId int, userId int32) error {
62+
return impl.workflowStatusLatestService.SaveOrUpdateCiWorkflowStatusLatest(pipelineId, appId, ciWorkflowId, userId)
6363
}
6464

65-
func (impl *WorkflowStatusUpdateServiceImpl) UpdateCdWorkflowStatusLatest(pipelineId, appId, environmentId, workflowRunnerId int, workflowType, status string, userId int32) error {
66-
return impl.workflowStatusLatestService.SaveOrUpdateCdWorkflowStatusLatest(pipelineId, appId, environmentId, workflowRunnerId, workflowType, status, userId)
65+
func (impl *WorkflowStatusUpdateServiceImpl) UpdateCdWorkflowStatusLatest(pipelineId, appId, environmentId, workflowRunnerId int, workflowType string, userId int32) error {
66+
return impl.workflowStatusLatestService.SaveOrUpdateCdWorkflowStatusLatest(pipelineId, appId, environmentId, workflowRunnerId, workflowType, userId)
6767
}
6868

6969
func (impl *WorkflowStatusUpdateServiceImpl) FetchCiStatusForTriggerViewOptimized(appId int) ([]*pipelineConfig.CiWorkflowStatus, error) {

0 commit comments

Comments
 (0)