pass context through step.run again

This commit is contained in:
Matthew Hooker 2018-01-19 19:55:27 -08:00
parent 030b5fd4f0
commit 62e3d1362f
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
4 changed files with 13 additions and 42 deletions

View File

@ -70,7 +70,7 @@ func (b *BasicRunner) Run(state StateBag) {
break
}
action := step.Run(state)
action := step.Run(ctx, state)
defer step.Cleanup(state)
if _, ok := state.GetOk(StateCancelled); ok {

View File

@ -1,6 +1,7 @@
package multistep
import (
"context"
"fmt"
"reflect"
"sync"
@ -113,7 +114,7 @@ type debugStepPause struct {
PauseFn DebugPauseFn
}
func (s *debugStepPause) Run(state StateBag) StepAction {
func (s *debugStepPause) Run(_ context.Context, state StateBag) StepAction {
s.PauseFn(DebugLocationAfterRun, s.StepName, state)
return ActionContinue
}

View File

@ -2,6 +2,8 @@
// discrete steps.
package multistep
import "context"
// A StepAction determines the next step to take regarding multi-step actions.
type StepAction uint
@ -26,7 +28,7 @@ type Step interface {
//
// The return value determines whether multi-step sequences continue
// or should halt.
Run(StateBag) StepAction
Run(context.Context, StateBag) StepAction
// Cleanup is called in reverse order of the steps that have run
// and allow steps to clean up after themselves. Do not assume if this

View File

@ -1,9 +1,6 @@
package multistep
import (
"context"
"sync"
)
import "sync"
// Add context to state bag to prevent changing step signature
@ -13,8 +10,6 @@ type StateBag interface {
Get(string) interface{}
GetOk(string) (interface{}, bool)
Put(string, interface{})
Context() context.Context
WithContext(context.Context) StateBag
}
// BasicStateBag implements StateBag by using a normal map underneath
@ -22,13 +17,7 @@ type StateBag interface {
type BasicStateBag struct {
data map[string]interface{}
l sync.RWMutex
ctx context.Context
}
func NewBasicStateBag() *BasicStateBag {
b := new(BasicStateBag)
b.data = make(map[string]interface{})
return b
once sync.Once
}
func (b *BasicStateBag) Get(k string) interface{} {
@ -48,32 +37,11 @@ func (b *BasicStateBag) Put(k string, v interface{}) {
b.l.Lock()
defer b.l.Unlock()
// Make sure the map is initialized one time, on write
b.once.Do(func() {
b.data = make(map[string]interface{})
})
// Write the data
b.data[k] = v
}
func (b *BasicStateBag) Context() context.Context {
if b.ctx != nil {
return b.ctx
}
return context.Background()
}
// WithContext returns a copy of BasicStateBag with the provided context
// We copy the state bag
func (b *BasicStateBag) WithContext(ctx context.Context) *BasicStateBag {
if ctx == nil {
panic("nil context")
}
// read lock because copying is a read operation
b.l.RLock()
defer b.l.RUnlock()
b2 := NewBasicStateBag()
for k, v := range b.data {
b2.data[k] = v
}
b2.ctx = ctx
return b2
}