Skip to content

Commit

Permalink
fix(api): get pipeline and node from workflow run instead of db (#3672)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored and yesnault committed Dec 3, 2018
1 parent 9b9c9a1 commit b34e71a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
9 changes: 9 additions & 0 deletions engine/api/workflow/dao_run.go
Expand Up @@ -271,6 +271,15 @@ func LoadRunByIDAndProjectKey(db gorp.SqlExecutor, projectkey string, id int64,
return loadRun(db, loadOpts, query, projectkey, id)
}

// LoadRunByNodeRunID returns a specific run
func LoadRunByNodeRunID(db gorp.SqlExecutor, nodeRunID int64, loadOpts LoadRunOptions) (*sdk.WorkflowRun, error) {
query := fmt.Sprintf(`select %s
from workflow_run
join workflow_node_run on workflow_node_run.workflow_run_id = workflow_run.id
where workflow_node_run.id = $1`, wfRunfields)
return loadRun(db, loadOpts, query, nodeRunID)
}

// LoadRunByID loads run by ID
func LoadRunByID(db gorp.SqlExecutor, id int64, loadOpts LoadRunOptions) (*sdk.WorkflowRun, error) {
query := fmt.Sprintf(`select %s
Expand Down
40 changes: 35 additions & 5 deletions engine/api/workflow_queue.go
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/ovh/cds/engine/api/metrics"
"github.com/ovh/cds/engine/api/observability"
"github.com/ovh/cds/engine/api/permission"
"github.com/ovh/cds/engine/api/pipeline"
"github.com/ovh/cds/engine/api/project"
"github.com/ovh/cds/engine/api/repositoriesmanager"
"github.com/ovh/cds/engine/api/services"
Expand Down Expand Up @@ -546,6 +545,7 @@ func (api *API) postWorkflowJobServiceLogsHandler() service.AsynchronousHandler

globalErr := &sdk.MultiError{}
errorOccured := false
var wr *sdk.WorkflowRun
for _, log := range logs {
nodeRunJob, errJob := workflow.LoadNodeJobRun(db, api.Cache, log.WorkflowNodeJobRunID)
if errJob != nil {
Expand All @@ -555,14 +555,44 @@ func (api *API) postWorkflowJobServiceLogsHandler() service.AsynchronousHandler
}
log.WorkflowNodeRunID = nodeRunJob.WorkflowNodeRunID

pip, errL := pipeline.LoadByNodeRunID(db, log.WorkflowNodeRunID)
if errL != nil {
if wr == nil {
var errW error
wr, errW = workflow.LoadRunByNodeRunID(db, log.WorkflowNodeRunID, workflow.LoadRunOptions{DisableDetailledNodeRun: true})
if errW != nil {
errorOccured = true
globalErr.Append(fmt.Errorf("postWorkflowJobServiceLogsHandler> Cannot load workflow run by node run id %d : %v", log.WorkflowNodeRunID, errW))
continue
}
}

var nr *sdk.WorkflowNodeRun
for _, nruns := range wr.WorkflowNodeRuns {
if len(nruns) > 0 && nruns[0].ID == log.WorkflowNodeRunID {
nr = &nruns[0]
break
}
}

if nr == nil {
errorOccured = true
globalErr.Append(fmt.Errorf("postWorkflowJobServiceLogsHandler> Cannot get pipeline for node run id %d : %v", log.WorkflowNodeRunID, errL))
globalErr.Append(fmt.Errorf("postWorkflowJobServiceLogsHandler> Cannot load workflow node run %d", log.WorkflowNodeRunID))
continue
}

if pip == nil {
var pip sdk.Pipeline
if wr.Version < 2 {
n := wr.Workflow.GetNode(nr.WorkflowNodeID)
if n != nil {
pip = wr.Workflow.Pipelines[n.PipelineID]
}
} else {
n := wr.Workflow.WorkflowData.NodeByID(nr.WorkflowNodeID)
if n != nil && n.Context != nil && n.Context.PipelineID != 0 {
pip = wr.Workflow.Pipelines[n.Context.PipelineID]
}
}

if pip.ID == 0 {
errorOccured = true
globalErr.Append(fmt.Errorf("postWorkflowJobServiceLogsHandler> Cannot get pipeline for node run id %d : Not found", log.WorkflowNodeRunID))
continue
Expand Down

0 comments on commit b34e71a

Please sign in to comment.