Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(subtask): do not auto-resume parent task of subtask if parent resolution is paused #367

Merged
merged 3 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,14 +1185,22 @@ func TestResolveSubTask(t *testing.T) {
assert.Equal(t, step.StateDone, v.State, "not valid state for step %s", k)
}

subtask, err = task.LoadFromPublicID(dbp, subtaskPublicID)
require.Nil(t, err)
assert.Equal(t, task.StateDone, subtask.State)
parentTaskToResume, err := subtask.ShouldResumeParentTask(dbp)
require.Nil(t, err)
require.NotNil(t, parentTaskToResume)
assert.Equal(t, res.TaskID, parentTaskToResume.ID)

// checking if the parent task is picked up after that the subtask is resolved.
// need to sleep a bit because the parent task is resumed asynchronously
ti := time.Second
i := time.Duration(0)
for i < ti {
res, err = resolution.LoadFromPublicID(dbp, res.PublicID)
require.Nil(t, err)
if res.State != resolution.StateError {
if res.State != resolution.StateWaiting {
break
}

Expand All @@ -1216,6 +1224,56 @@ func TestResolveSubTask(t *testing.T) {
assert.Equal(t, resolution.StateDone, res.State)
}

func TestResolveSubTaskParentTaskPaused(t *testing.T) {
dbp, err := zesty.NewDBProvider(utask.DBName)
require.Nil(t, err)

tt, err := templateFromYAML(dbp, "variables.yaml")
require.Nil(t, err)
tt.Normalize()
assert.Equal(t, "variableeval", tt.Name)
require.Nil(t, tt.Valid())

err = dbp.DB().Insert(tt)
require.Nil(t, err)

res, err := createResolution("subtask.yaml", map[string]interface{}{}, nil)
require.Nil(t, err, "failed to create resolution: %s", err)

res, err = runResolution(res)
require.Nil(t, err)
require.NotNil(t, res)
assert.Equal(t, resolution.StateWaiting, res.State)

subtaskCreationOutput := res.Steps["subtaskCreation"].Output.(map[string]interface{})
subtaskPublicID := subtaskCreationOutput["id"].(string)

// pausing parent task
res.SetState(resolution.StatePaused)
res.Update(dbp)

subtask, err := task.LoadFromPublicID(dbp, subtaskPublicID)
require.Nil(t, err)
assert.Equal(t, task.StateTODO, subtask.State)

subtaskResolution, err := resolution.Create(dbp, subtask, nil, "", false, nil)
require.Nil(t, err)

subtaskResolution, err = runResolution(subtaskResolution)
require.Nil(t, err)
assert.Equal(t, task.StateDone, subtaskResolution.State)
for k, v := range subtaskResolution.Steps {
assert.Equal(t, step.StateDone, v.State, "not valid state for step %s", k)
}

subtask, err = task.LoadFromPublicID(dbp, subtaskPublicID)
require.Nil(t, err)
assert.Equal(t, task.StateDone, subtask.State)
parentTaskToResume, err := subtask.ShouldResumeParentTask(dbp)
require.Nil(t, parentTaskToResume)
require.Nil(t, err)
}

func TestResolveCallback(t *testing.T) {
res, err := createResolution("callback.yaml", map[string]interface{}{}, nil)
require.NoError(t, err)
Expand Down
11 changes: 11 additions & 0 deletions models/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/ovh/utask/engine/input"
"github.com/ovh/utask/engine/values"
"github.com/ovh/utask/models"
"github.com/ovh/utask/models/resolution"
"github.com/ovh/utask/models/tasktemplate"
"github.com/ovh/utask/pkg/constants"
"github.com/ovh/utask/pkg/notify"
Expand Down Expand Up @@ -795,5 +796,15 @@ func (t *Task) ShouldResumeParentTask(dbp zesty.DBProvider) (*Task, error) {
return nil, nil
}

r, err := resolution.LoadFromPublicID(dbp, *parentTask.Resolution)
if err != nil {
return nil, err
}

switch r.State {
case resolution.StateCrashed, resolution.StatePaused:
return nil, nil
}

return parentTask, nil
}