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 break
} }
action := step.Run(state) action := step.Run(ctx, state)
defer step.Cleanup(state) defer step.Cleanup(state)
if _, ok := state.GetOk(StateCancelled); ok { if _, ok := state.GetOk(StateCancelled); ok {

View File

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

View File

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

View File

@ -1,9 +1,6 @@
package multistep package multistep
import ( import "sync"
"context"
"sync"
)
// Add context to state bag to prevent changing step signature // Add context to state bag to prevent changing step signature
@ -13,8 +10,6 @@ type StateBag interface {
Get(string) interface{} Get(string) interface{}
GetOk(string) (interface{}, bool) GetOk(string) (interface{}, bool)
Put(string, interface{}) Put(string, interface{})
Context() context.Context
WithContext(context.Context) StateBag
} }
// BasicStateBag implements StateBag by using a normal map underneath // BasicStateBag implements StateBag by using a normal map underneath
@ -22,13 +17,7 @@ type StateBag interface {
type BasicStateBag struct { type BasicStateBag struct {
data map[string]interface{} data map[string]interface{}
l sync.RWMutex l sync.RWMutex
ctx context.Context once sync.Once
}
func NewBasicStateBag() *BasicStateBag {
b := new(BasicStateBag)
b.data = make(map[string]interface{})
return b
} }
func (b *BasicStateBag) Get(k string) interface{} { func (b *BasicStateBag) Get(k string) interface{} {
@ -48,32 +37,11 @@ func (b *BasicStateBag) Put(k string, v interface{}) {
b.l.Lock() b.l.Lock()
defer b.l.Unlock() 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 // Write the data
b.data[k] = v 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
}