Skip to content

Commit

Permalink
Merge pull request #734 from ystia/bugfix/GH-733-action-no-stop
Browse files Browse the repository at this point in the history
Bugfix/gh 733 action no stop
  • Loading branch information
laurentganne committed May 7, 2021
2 parents 3716cda + 6ce2383 commit 7292c4c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## UNRELEASED

### BUG FIXES

* Workflow with asynchronous action never stops after another step failure ([GH-733](https://github.com/ystia/yorc/issues/733))

## 4.2.0-milestone.1 (May 06, 2021)

### ENHANCEMENTS
Expand Down
3 changes: 3 additions & 0 deletions prov/scheduling/scheduler/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,8 @@ func TestRunConsulSchedulingPackageTests(t *testing.T) {
t.Run("testUnregisterAction", func(t *testing.T) {
testUnregisterAction(t, client)
})
t.Run("testUpdateActionData", func(t *testing.T) {
testUpdateActionData(t, client)
})
})
}
28 changes: 28 additions & 0 deletions prov/scheduling/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/hashicorp/consul/api"
"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

"github.com/ystia/yorc/v4/events"
"github.com/ystia/yorc/v4/helper/consulutil"
Expand Down Expand Up @@ -368,3 +369,30 @@ func testUnregisterAction(t *testing.T, client *api.Client) {
require.NotNil(t, kvp, "kvp is nil")
require.Equal(t, "true", string(kvp.Value), "unregisterFlag is not set to true")
}

func testUpdateActionData(t *testing.T, client *api.Client) {
t.Parallel()
deploymentID := "dep-" + t.Name()
ti := 1 * time.Second
actionType := "test-action"
action := &prov.Action{ActionType: actionType, Data: map[string]string{"key1": "val1", "key2": "val2", "key3": "val3"}}
id, err := scheduling.RegisterAction(client, deploymentID, ti, action)
assert.NilError(t, err, "Failed to register action")

err = scheduling.UpdateActionData(client, id, "key2", "newVal")
assert.NilError(t, err, "Failed to update action data")

testSched := scheduler{cc: client}
newAction, err := testSched.buildScheduledAction(id)
assert.NilError(t, err, "Failed to build action")

val := newAction.Data["key2"]
assert.Equal(t, val, "newVal", "Unexpected value for action key updated")

// Check the update of an unregistered action, should fail
err = testSched.unregisterAction(id)
assert.NilError(t, err, "Failed to unregister action")

err = scheduling.UpdateActionData(client, id, "key3", "newVal")
assert.ErrorContains(t, err, "unregistered")
}
15 changes: 12 additions & 3 deletions prov/scheduling/scheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/hashicorp/consul/api"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
uuid "github.com/satori/go.uuid"

"github.com/ystia/yorc/v4/helper/consulutil"
"github.com/ystia/yorc/v4/log"
Expand Down Expand Up @@ -104,7 +104,16 @@ func UnregisterAction(client *api.Client, id string) error {
// UpdateActionData updates the value of a given data within an action
func UpdateActionData(client *api.Client, id, key, value string) error {

//TODO check if action exists
scaKeyPath := path.Join(consulutil.SchedulingKVPrefix, "actions", id, "data", key)
// check if action still exists
actionIdPrefix := path.Join(consulutil.SchedulingKVPrefix, "actions", id)
kvp, _, err := client.KV().Get(path.Join(actionIdPrefix, "deploymentID"), nil)
if err != nil {
return err
}
if kvp == nil {
return errors.Errorf("Action with ID %s is unregistered", id)
}

scaKeyPath := path.Join(actionIdPrefix, "data", key)
return errors.Wrapf(consulutil.StoreConsulKeyAsString(scaKeyPath, value), "Failed to update data %q for action %q", key, id)
}
5 changes: 5 additions & 0 deletions tasks/workflow/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ func (w *worker) runAction(ctx context.Context, t *taskExecution) error {
}
}
wasCancelled := new(bool)
taskFailure := new(bool)
if action.AsyncOperation.TaskID != "" {
ctx = operations.SetOperationLogFields(ctx, action.AsyncOperation.Operation)
ctx = events.AddLogOptionalFields(ctx, events.LogOptionalFields{
Expand All @@ -467,6 +468,7 @@ func (w *worker) runAction(ctx context.Context, t *taskExecution) error {
tasks.UpdateTaskStepWithStatus(action.AsyncOperation.TaskID, action.AsyncOperation.StepName, tasks.TaskStepStatusCANCELED)
})
tasks.MonitorTaskFailure(ctx, action.AsyncOperation.TaskID, func() {
*taskFailure = true
// Unregister this action asap to prevent new schedulings
scheduling.UnregisterAction(w.consulClient, action.ID)

Expand Down Expand Up @@ -495,6 +497,9 @@ func (w *worker) runAction(ctx context.Context, t *taskExecution) error {
if deregister || *wasCancelled {
scheduling.UnregisterAction(w.consulClient, action.ID)
w.endAction(ctx, t, action, *wasCancelled, err)
} else if *taskFailure {
err = errors.Errorf("Stopped on task failure")
w.endAction(ctx, t, action, *wasCancelled, err)
}
if err != nil {
return err
Expand Down

0 comments on commit 7292c4c

Please sign in to comment.