diff --git a/main.go b/main.go index 369b9b0..7c3d3ec 100644 --- a/main.go +++ b/main.go @@ -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() @@ -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) } @@ -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) } @@ -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) } diff --git a/state.go b/state.go index bad3f60..db4d7c1 100644 --- a/state.go +++ b/state.go @@ -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 { @@ -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 } @@ -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 }