Skip to content

Commit

Permalink
fix(api): fix stop node run and workflow run (#1223)
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Coenen <benjamin.coenen@corp.ovh.com>
  • Loading branch information
bnjjj authored and sguiheux committed Oct 3, 2017
1 parent c80399b commit de947e6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
7 changes: 7 additions & 0 deletions engine/api/workflow/execute_node_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ func StopWorkflowNodeRun(db *gorp.DbMap, store cache.Store, proj *sdk.Project, n
if err := UpdateNodeJobRunStatus(tx, store, proj, njr, sdk.StatusStopped); err != nil {
return sdk.WrapError(err, "StopWorkflowNodeRun> Cannot update node job run")
}

nodeRun.Status = sdk.StatusStopped.String()
nodeRun.LastModified = time.Now()
nodeRun.Done = time.Now()
if err := UpdateNodeRun(tx, &nodeRun); err != nil {
return sdk.WrapError(err, "StopWorkflowNodeRun> Cannot update node run")
}
}

if err := tx.Commit(); err != nil {
Expand Down
16 changes: 10 additions & 6 deletions engine/api/workflow/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// processWorkflowRun triggers workflow node for every workflow.
// It contains all the logic for triggers and joins processing.
func processWorkflowRun(db gorp.SqlExecutor, store cache.Store, p *sdk.Project, w *sdk.WorkflowRun, hookEvent *sdk.WorkflowNodeRunHookEvent, manual *sdk.WorkflowNodeRunManual, startingFromNode *int64) error {
var nodesRunFailed, nodesRunBuilding, nodesRunSuccess int
var nodesRunFailed, nodesRunStopped, nodesRunBuilding, nodesRunSuccess int
t0 := time.Now()
w.Status = string(sdk.StatusBuilding)
log.Debug("processWorkflowRun> Begin [#%d]%s", w.Number, w.Workflow.Name)
Expand Down Expand Up @@ -67,7 +67,7 @@ func processWorkflowRun(db gorp.SqlExecutor, store cache.Store, p *sdk.Project,

// Only the last subversion
if maxsn == nodeRun.SubNumber {
updateNodesRunStatus(nodeRun.Status, &nodesRunSuccess, &nodesRunBuilding, &nodesRunFailed)
updateNodesRunStatus(nodeRun.Status, &nodesRunSuccess, &nodesRunBuilding, &nodesRunFailed, &nodesRunStopped)
}

//Trigger only if the node is over (successfull or not)
Expand Down Expand Up @@ -245,7 +245,7 @@ func processWorkflowRun(db gorp.SqlExecutor, store cache.Store, p *sdk.Project,
}
}

w.Status = getWorkflowRunStatus(nodesRunSuccess, nodesRunBuilding, nodesRunFailed)
w.Status = getWorkflowRunStatus(nodesRunSuccess, nodesRunBuilding, nodesRunFailed, nodesRunStopped)

if err := updateWorkflowRun(db, w); err != nil {
return sdk.WrapError(err, "processWorkflowRun>")
Expand Down Expand Up @@ -439,29 +439,33 @@ func AddWorkflowRunInfo(run *sdk.WorkflowRun, infos ...sdk.SpawnMsg) {
}
}

// getWorkflowRunStatus return the status depending on number of workflowNodeRuns in success, building and fail
func getWorkflowRunStatus(nodesRunSuccess, nodesRunBuilding, nodesRunFailed int) string {
// getWorkflowRunStatus return the status depending on number of workflowNodeRuns in success, building, stopped and fail
func getWorkflowRunStatus(nodesRunSuccess, nodesRunBuilding, nodesRunFailed, nodesRunStopped int) string {
switch {
case nodesRunBuilding > 0:
return string(sdk.StatusBuilding)
case nodesRunFailed > 0:
return string(sdk.StatusFail)
case nodesRunSuccess > 0:
return string(sdk.StatusSuccess)
case nodesRunStopped > 0:
return string(sdk.StatusStopped)
default:
return string(sdk.StatusNeverBuilt)
}
}

// updateNodesRunStatus is useful to compute number of nodeRun in success, building and fail
func updateNodesRunStatus(status string, success, building, fail *int) {
func updateNodesRunStatus(status string, success, building, fail, stop *int) {
switch status {
case string(sdk.StatusSuccess):
*success++
case string(sdk.StatusBuilding), string(sdk.StatusWaiting):
*building++
case string(sdk.StatusFail):
*fail++
case string(sdk.StatusStopped):
*stop++
}
}

Expand Down
19 changes: 19 additions & 0 deletions engine/api/workflow/resync_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,22 @@ func resyncNode(db gorp.SqlExecutor, n *sdk.WorkflowNode) error {
}
return nil
}

//ResyncWorkflowRunStatus resync the status of wofkflow if you stop a node run when workflow run is building
func ResyncWorkflowRunStatus(db gorp.SqlExecutor, wr sdk.WorkflowRun) error {
var success, building, failed, stopped int
for _, wnrs := range wr.WorkflowNodeRuns {
for _, wnr := range wnrs {
if wr.LastSubNumber == wnr.SubNumber {
updateNodesRunStatus(wnr.Status, &success, &building, &failed, &stopped)
}
}
}

newStatus := getWorkflowRunStatus(success, building, failed, stopped)
if newStatus != wr.Status {
return UpdateWorkflowRunStatus(db, wr.ID, newStatus)
}

return nil
}
27 changes: 24 additions & 3 deletions engine/api/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ func (api *API) stopWorkflowRunHandler() Handler {

for _, wn := range run.WorkflowNodeRuns {
for _, wnr := range wn {
if wnr.SubNumber == run.LastSubNumber && (wnr.Status == sdk.StatusBuilding.String() || wnr.Status == sdk.StatusChecking.String()) {
if wnr.SubNumber == run.LastSubNumber &&
(wnr.Status == sdk.StatusBuilding.String() || wnr.Status == sdk.StatusChecking.String() || wnr.Status == sdk.StatusWaiting.String()) {
if errS := workflow.StopWorkflowNodeRun(api.mustDB(), api.Cache, proj, wnr, stopInfos); errS != nil {
return sdk.WrapError(errS, "stopWorkflowRunHandler> Unable to stop workflow node run %d", wnr.ID)
}
Expand Down Expand Up @@ -292,15 +293,35 @@ func (api *API) stopWorkflowNodeRunHandler() Handler {
return sdk.WrapError(err, "stopWorkflowNodeRunHandler> Unable to load last workflow run")
}

wr, errLw := workflow.LoadRun(api.mustDB(), key, name, number)
if errLw != nil {
return sdk.WrapError(errLw, "stopWorkflowNodeRunHandler> Unable to load workflow run %s", name)
}

tx, errTx := api.mustDB().Begin()
if errTx != nil {
return sdk.WrapError(errTx, "stopWorkflowNodeRunHandler> Unable to create transaction")
}
defer tx.Rollback()

stopInfos := sdk.SpawnInfo{
APITime: time.Now(),
RemoteTime: time.Now(),
Message: sdk.SpawnMsg{ID: sdk.MsgWorkflowNodeStop.ID, Args: []interface{}{getUser(ctx).Username}},
}
if errS := workflow.StopWorkflowNodeRun(api.mustDB(), api.Cache, p, *nodeRun, stopInfos); errS != nil {
return sdk.WrapError(errS, "stopWorkflowNodeRunHandler> Unable to stop workflow node run")
}

errS := workflow.StopWorkflowNodeRun(api.mustDB(), api.Cache, p, *nodeRun, stopInfos)
if errR := workflow.ResyncWorkflowRunStatus(tx, *wr); errR != nil {
return sdk.WrapError(errR, "stopWorkflowNodeRunHandler> Unable to resync workflow run status")
}

return errS
if errC := tx.Commit(); errC != nil {
return sdk.WrapError(errC, "stopWorkflowNodeRunHandler> Unable to commit")
}

return nil
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<span>v{{workflowRun.num}}.{{workflowRun.last_subnumber}}</span>
</div>
<div class="five wide column">
<div class="right floated">
<div class="right floated"
*ngIf="workflowRun.status !== pipelineStatusEnum.BUILDING && workflowRun.status !== pipelineStatusEnum.WAITING">
<i class="checked calendar icon"></i>
<span>{{workflowRun.last_execution | amCalendar}}</span>
</div>
Expand All @@ -32,17 +33,15 @@
<div class="ui grid">
<div class="row">
<div class="five wide column">
<button class="ui labeled icon button"
<button class="ui icon button"
*ngIf="direction === 'TB'"
(click)="changeDirection()">
<i class="resize horizontal icon"></i>
{{ 'workflow_orientation_horizontal' | translate }}
</button>
<button class="ui labeled icon button"
<button class="ui icon button"
*ngIf="direction === 'LR'"
(click)="changeDirection()">
<i class="resize vertical icon"></i>
{{ 'workflow_orientation_vertical' | translate }}
</button>
</div>
<div class="six wide column centered">
Expand Down

0 comments on commit de947e6

Please sign in to comment.