Skip to content

Commit

Permalink
sync temp state file after write
Browse files Browse the repository at this point in the history
  • Loading branch information
yhlee-aws committed Dec 28, 2018
1 parent 684a5de commit 32d3e33
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions agent/statemanager/state_manager_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"io/ioutil"
"os"
"path/filepath"

"github.com/cihub/seelog"
)

/*
Expand Down Expand Up @@ -62,17 +64,25 @@ func (manager *basicStateManager) writeFile(data []byte) error {
// actually move it atomically; cross-device renaming will error out.
tmpfile, err := ioutil.TempFile(manager.statePath, "tmp_ecs_agent_data")
if err != nil {
log.Error("Error saving state; could not create temp file to save state", "err", err)
seelog.Errorf("Error saving state; could not create temp file to save state, err: %v", err)
return err
}
_, err = tmpfile.Write(data)
if err != nil {
log.Error("Error saving state; could not write to temp file to save state", "err", err)
seelog.Errorf("Error saving state; could not write to temp file to save state, err: %v", err)
return err
}

// flush temp state file to disk
err = tmpfile.Sync()
if err != nil {
seelog.Errorf("Error flusing state file, err: %v", err)
return err
}

err = os.Rename(tmpfile.Name(), filepath.Join(manager.statePath, ecsDataFile))
if err != nil {
log.Error("Error saving state; could not move to data file", "err", err)
seelog.Errorf("Error saving state; could not move to data file, err: %v", err)
}
return err
}

0 comments on commit 32d3e33

Please sign in to comment.