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

Fixed #477 : Corrected state for get and get by id command. #482

Merged
merged 3 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 21 additions & 9 deletions grpc-server/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ import (
wkf "github.com/tinkerbell/tink/workflow"
)

var state = map[int32]workflow.State{
0: workflow.State_STATE_PENDING,
1: workflow.State_STATE_RUNNING,
2: workflow.State_STATE_FAILED,
3: workflow.State_STATE_TIMEOUT,
4: workflow.State_STATE_SUCCESS,
}

const errFailedToGetTemplate = "failed to get template with ID %s"

// CreateWorkflow implements workflow.CreateWorkflow
Expand Down Expand Up @@ -115,11 +107,12 @@ func (s *server) GetWorkflow(ctx context.Context, in *workflow.GetRequest) (*wor
if err != nil {
return &workflow.Workflow{}, err
}

wf := &workflow.Workflow{
Id: w.ID,
Template: w.Template,
Hardware: w.Hardware,
State: state[w.State],
State: getWorkflowState(s.db, ctx, in.Id),
Data: data,
}
l := s.logger.With("workflowID", w.ID)
Expand Down Expand Up @@ -181,6 +174,7 @@ func (s *server) ListWorkflows(_ *workflow.Empty, stream workflow.WorkflowServic
Hardware: w.Hardware,
CreatedAt: w.CreatedAt,
UpdatedAt: w.UpdatedAt,
State: getWorkflowState(s.db, stream.Context(), w.ID),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're making 2 calls to getWorkflowState, this means 2 DB calls. You could call getWorkflowState just once (1 DB call) and store it to a variable for use in both locations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is possible to call getWorkflowState once and store it because the workflow state will change during the execution of a workflow which makes it dynamic. Therefore we have to fetch the latest value of a workflow state.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, apologies. You are right. For some reason I thought I saw two calls in a single function.

}
return stream.Send(wf)
})
Expand Down Expand Up @@ -279,3 +273,21 @@ func (s *server) ShowWorkflowEvents(req *workflow.GetRequest, stream workflow.Wo
metrics.CacheHits.With(labels).Inc()
return nil
}

// This function will provide the workflow state on the basis of the state of the actions
// For e.g. : If an action has Failed or Timeout then the workflow state will also be
// considered as Failed/Timeout. And If an action is successful then the workflow state
// will be considered as Running until the last action of the workflow is executed successfully.

func getWorkflowState(db db.Database, ctx context.Context, id string) workflow.State {
parauliya marked this conversation as resolved.
Show resolved Hide resolved
wfCtx, _ := db.GetWorkflowContexts(ctx, id)
if wfCtx.CurrentActionState != workflow.State_STATE_SUCCESS {
return wfCtx.CurrentActionState
} else {
if wfCtx.GetCurrentActionIndex() == wfCtx.GetTotalNumberOfActions()-1 {
return workflow.State_STATE_SUCCESS
} else {
return workflow.State_STATE_RUNNING
}
}
}
40 changes: 40 additions & 0 deletions grpc-server/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestGetWorkflow(t *testing.T) {
args struct {
db mock.DB
wfTemplate, wfHardware string
state workflow.State
}
want struct {
expectedError bool
Expand All @@ -136,10 +137,19 @@ func TestGetWorkflow(t *testing.T) {
Template: templateID,
Hardware: hw}, nil
},
GetWorkflowContextsFunc: func(ctx context.Context, wfID string) (*workflow.WorkflowContext, error) {
return &workflow.WorkflowContext{
WorkflowId: wfID,
CurrentActionState: workflow.State_STATE_SUCCESS,
CurrentActionIndex: 0,
TotalNumberOfActions: 1,
}, nil
},
GetTemplateFunc: func(ctx context.Context, fields map[string]string, deleted bool) (string, string, string, error) {
return "", "", templateData, nil
},
},
state: workflow.State_STATE_SUCCESS,
wfTemplate: templateID,
wfHardware: hw,
},
Expand All @@ -159,6 +169,35 @@ func TestGetWorkflow(t *testing.T) {
expectedError: true,
},
},
"GetWorkflowState": {
args: args{
db: mock.DB{
GetWorkflowFunc: func(ctx context.Context, workflowID string) (db.Workflow, error) {
return db.Workflow{
ID: workflowID,
Template: templateID,
Hardware: hw}, nil
},
GetWorkflowContextsFunc: func(ctx context.Context, wfID string) (*workflow.WorkflowContext, error) {
return &workflow.WorkflowContext{
WorkflowId: wfID,
CurrentActionState: workflow.State_STATE_SUCCESS,
CurrentActionIndex: 0,
TotalNumberOfActions: 2,
}, nil
},
GetTemplateFunc: func(ctx context.Context, fields map[string]string, deleted bool) (string, string, string, error) {
return "", "", templateData, nil
},
},
state: workflow.State_STATE_RUNNING,
wfTemplate: templateID,
wfHardware: hw,
},
want: want{
expectedError: false,
},
},
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand All @@ -175,6 +214,7 @@ func TestGetWorkflow(t *testing.T) {
assert.True(t, tc.want.expectedError)
return
}
assert.Equal(t, tc.args.state, res.State)
assert.NoError(t, err)
assert.NotEmpty(t, res)
assert.False(t, tc.want.expectedError)
Expand Down