Skip to content
This repository has been archived by the owner on Jan 9, 2018. It is now read-only.

Commit

Permalink
Save log stream name to state and reset if changed
Browse files Browse the repository at this point in the history
  • Loading branch information
eyj committed Oct 5, 2016
1 parent 082af68 commit 18ea048
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ func run(configFilename string) error {
return fmt.Errorf("Failed to open %s: %s", config.StateFilename, err)
}

lastBootId, nextSeq := state.LastState()
streamName, lastBootId, nextSeq := state.LastState()
if streamName != "" && streamName != config.LogStreamName {
log.Printf("LogStreamName changed from %s to %s; resetting state.", streamName, config.LogStreamName)
lastBootId = ""
nextSeq = ""
}

awsSession := config.NewAWSSession()

Expand Down Expand Up @@ -106,7 +111,7 @@ func run(configFilename string) error {
skip = 1
}

err = state.SetState(bootId, nextSeq)
err = state.SetState(config.LogStreamName, bootId, nextSeq)
if err != nil {
return fmt.Errorf("Failed to write state: %s", err)
}
Expand All @@ -126,7 +131,7 @@ func run(configFilename string) error {
return fmt.Errorf("Failed to write to cloudwatch: %s", err)
}

err = state.SetState(bootId, nextSeq)
err = state.SetState(config.LogStreamName, bootId, nextSeq)
if err != nil {
return fmt.Errorf("Failed to write state: %s", err)
}
Expand All @@ -135,7 +140,7 @@ func run(configFilename string) error {

// We fall out here when interrupted by a signal.
// Last chance to write the state.
err = state.SetState(bootId, nextSeq)
err = state.SetState(config.LogStreamName, bootId, nextSeq)
if err != nil {
return fmt.Errorf("Failed to write state on exit: %s", err)
}
Expand Down
19 changes: 10 additions & 9 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
)

const stateFormat = "%s\n%s\n"
const stateFormat = "%s\n%s\n%s\n"
const mapSize = 64

type State struct {
Expand All @@ -14,7 +14,7 @@ type State struct {

func OpenState(fn string) (State, error) {
s := State{}
f, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE, 0700)
f, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return s, err
}
Expand All @@ -30,26 +30,27 @@ func (s State) Sync() error {
return s.file.Sync()
}

func (s State) LastState() (string, string) {
func (s State) LastState() (string, string, string) {
var streamName string
var bootId string
var seqToken string
_, err := s.file.Seek(0, 0)
if err != nil {
return "", ""
return "", "", ""
}
n, err := fmt.Fscanf(s.file, stateFormat, &bootId, &seqToken)
n, err := fmt.Fscanf(s.file, stateFormat, &streamName, &bootId, &seqToken)
if err != nil || n < 2 {
return "", ""
return "", "", ""
}
return bootId, seqToken
return streamName, bootId, seqToken
}

func (s State) SetState(bootId, seqToken string) error {
func (s State) SetState(streamName, bootId, seqToken string) error {
_, err := s.file.Seek(0, 0)
if err != nil {
return err
}
_, err = fmt.Fprintf(s.file, stateFormat, bootId, seqToken)
_, err = fmt.Fprintf(s.file, stateFormat, streamName, bootId, seqToken)
if err != nil {
return err
}
Expand Down

0 comments on commit 18ea048

Please # to comment.