Skip to content

Commit

Permalink
Merge pull request #58 from milescrabill/fix-state
Browse files Browse the repository at this point in the history
Fix race conditions involving State
  • Loading branch information
milescrabill committed Apr 6, 2016
2 parents 740e3fd + 8349f6e commit e4a9d08
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 28 deletions.
4 changes: 1 addition & 3 deletions aws/autoscaling.go
Expand Up @@ -114,9 +114,7 @@ func NewAutoScalingGroup(region string, asg *autoscaling.Group) *AutoScalingGrou
a.reaperState = state.NewStateWithTag(a.Tag(reaperTag))
} else {
// initial state
a.reaperState = state.NewStateWithUntilAndState(
time.Now().Add(config.Notifications.FirstStateDuration.Duration),
state.FirstState)
a.reaperState = state.NewState()
}

return &a
Expand Down
10 changes: 7 additions & 3 deletions aws/awsresource.go
Expand Up @@ -52,6 +52,10 @@ func (a *AWSResource) SetReaperState(newState *state.State) {
a.reaperState = newState
}

func (a *AWSResource) SetUpdated(b bool) {
a.reaperState.Updated = b
}

// Owner extracts useful information out of the Owner tag which should
// be parsable by mail.ParseAddress
func (a *AWSResource) Owner() *mail.Address {
Expand Down Expand Up @@ -83,7 +87,8 @@ func (a *AWSResource) IncrementState() bool {

switch a.reaperState.State {
default:
// shouldn't ever be hit, but if it is
fallthrough
case state.InitialState:
// set state to the FirstState
newState = state.FirstState
until = until.Add(config.Notifications.FirstStateDuration.Duration)
Expand All @@ -108,11 +113,10 @@ func (a *AWSResource) IncrementState() bool {

if newState != a.reaperState.State {
updated = true
a.reaperState = state.NewStateWithUntilAndState(until, newState)
log.Notice("Updating state for %s. New state: %s.", a.ReapableDescriptionTiny(), newState.String())
}

a.reaperState = state.NewStateWithUntilAndState(until, newState)

return updated
}

Expand Down
4 changes: 1 addition & 3 deletions aws/cloudformation.go
Expand Up @@ -62,9 +62,7 @@ func NewCloudformation(region string, stack *cloudformation.Stack) *Cloudformati
a.reaperState = state.NewStateWithTag(a.AWSResource.Tag(reaperTag))
} else {
// initial state
a.reaperState = state.NewStateWithUntilAndState(
time.Now().Add(config.Notifications.FirstStateDuration.Duration),
state.FirstState)
a.reaperState = state.NewState()
}

return &a
Expand Down
4 changes: 1 addition & 3 deletions aws/instance.go
Expand Up @@ -92,9 +92,7 @@ func NewInstance(region string, instance *ec2.Instance) *Instance {
i.reaperState = state.NewStateWithTag(i.Tag(reaperTag))
} else {
// initial state
i.reaperState = state.NewStateWithUntilAndState(
time.Now().Add(config.Notifications.FirstStateDuration.Duration),
state.FirstState)
i.reaperState = state.NewState()
}

return &i
Expand Down
4 changes: 1 addition & 3 deletions aws/securitygroup.go
Expand Up @@ -43,9 +43,7 @@ func NewSecurityGroup(region string, sg *ec2.SecurityGroup) *SecurityGroup {
s.reaperState = state.NewStateWithTag(s.AWSResource.Tag(reaperTag))
} else {
// initial state
s.reaperState = state.NewStateWithUntilAndState(
time.Now().Add(config.Notifications.FirstStateDuration.Duration),
state.FirstState)
s.reaperState = state.NewState()
}

return &s
Expand Down
8 changes: 4 additions & 4 deletions reaper/reaper.go
Expand Up @@ -671,7 +671,7 @@ func reapSecurityGroup(s *reaperaws.SecurityGroup) {
// update the internal state
if time.Now().After(s.ReaperState().Until) {
// if we updated the state, mark it as having been updated
s.ReaperState().SetUpdated(s.IncrementState())
s.SetUpdated(s.IncrementState())
}
log.Notice(fmt.Sprintf("Reapable SecurityGroup discovered: %s.", s.ReapableDescription()))
reapables.Put(s.Region, s.ID, s)
Expand All @@ -681,7 +681,7 @@ func reapCloudformation(c *reaperaws.Cloudformation) {
// update the internal state
if time.Now().After(c.ReaperState().Until) {
// if we updated the state, mark it as having been updated
c.ReaperState().SetUpdated(c.IncrementState())
c.SetUpdated(c.IncrementState())
}
log.Notice(fmt.Sprintf("Reapable Cloudformation discovered: %s.", c.ReapableDescription()))
reapables.Put(c.Region, c.ID, c)
Expand All @@ -691,7 +691,7 @@ func reapInstance(i *reaperaws.Instance) {
// update the internal state
if time.Now().After(i.ReaperState().Until) {
// if we updated the state, mark it as having been updated
i.ReaperState().SetUpdated(i.IncrementState())
i.SetUpdated(i.IncrementState())
}
log.Notice(fmt.Sprintf("Reapable Instance discovered: %s.", i.ReapableDescription()))
reapables.Put(i.Region, i.ID, i)
Expand All @@ -701,7 +701,7 @@ func reapAutoScalingGroup(a *reaperaws.AutoScalingGroup) {
// update the internal state
if time.Now().After(a.ReaperState().Until) {
// if we updated the state, mark it as having been updated
a.ReaperState().SetUpdated(a.IncrementState())
a.SetUpdated(a.IncrementState())
}
log.Notice(fmt.Sprintf("Reapable AutoScalingGroup discovered: %s.", a.ReapableDescription()))
reapables.Put(a.Region, a.ID, a)
Expand Down
11 changes: 4 additions & 7 deletions state/state.go
Expand Up @@ -6,7 +6,8 @@ import (
)

const (
FirstState StateEnum = iota
InitialState StateEnum = iota
FirstState
SecondState
ThirdState
FinalState
Expand Down Expand Up @@ -45,18 +46,14 @@ type State struct {
Until time.Time
}

func (s *State) SetUpdated(b bool) {
s.Updated = b
}

func (s *State) String() string {
return s.State.String() + s.reaperTagSeparator + s.Until.Format(s.reaperTagTimeFormat)
}

func NewState() *State {
// default
return &State{
State: FirstState,
State: InitialState,
Until: time.Now(),
reaperTagSeparator: "|",
reaperTagTimeFormat: "2006-01-02 03:04PM MST",
Expand All @@ -66,7 +63,7 @@ func NewState() *State {
func NewStateWithUntil(until time.Time) *State {
// default
return &State{
State: FirstState,
State: InitialState,
Until: until,
reaperTagSeparator: "|",
reaperTagTimeFormat: "2006-01-02 03:04PM MST",
Expand Down
4 changes: 2 additions & 2 deletions state/stateenum_string.go
Expand Up @@ -4,9 +4,9 @@ package state

import "fmt"

const _StateEnum_name = "FirstStateSecondStateThirdStateFinalStateIgnoreState"
const _StateEnum_name = "InitialStateFirstStateSecondStateThirdStateFinalStateIgnoreState"

var _StateEnum_index = [...]uint8{0, 10, 21, 31, 41, 52}
var _StateEnum_index = [...]uint8{0, 12, 22, 33, 43, 53, 64}

func (i StateEnum) String() string {
if i < 0 || i >= StateEnum(len(_StateEnum_index)-1) {
Expand Down

0 comments on commit e4a9d08

Please sign in to comment.