atomic write service state and checks files, fixes #1221
This commit is contained in:
parent
94c14275c0
commit
40d1c279dd
|
@ -675,12 +675,11 @@ func (a *Agent) persistService(service *structs.NodeService) error {
|
||||||
if err := os.MkdirAll(filepath.Dir(svcPath), 0700); err != nil {
|
if err := os.MkdirAll(filepath.Dir(svcPath), 0700); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fh, err := os.OpenFile(svcPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
tempSvcPath := svcPath + ".tmp"
|
||||||
if err != nil {
|
if err := ioutil.WriteFile(tempSvcPath, encoded, 0600); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer fh.Close()
|
if err := os.Rename(tempSvcPath, svcPath); err != nil {
|
||||||
if _, err := fh.Write(encoded); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -1092,8 +1091,14 @@ func (a *Agent) persistCheckState(check *CheckTTL, status, output string) error
|
||||||
|
|
||||||
// Write the state to the file
|
// Write the state to the file
|
||||||
file := filepath.Join(dir, checkIDHash(check.CheckID))
|
file := filepath.Join(dir, checkIDHash(check.CheckID))
|
||||||
if err := ioutil.WriteFile(file, buf, 0600); err != nil {
|
// Create temp file in same folder, to make more likely atomic
|
||||||
return fmt.Errorf("failed writing file %q: %s", file, err)
|
tempFile := file + ".tmp"
|
||||||
|
|
||||||
|
if err := ioutil.WriteFile(tempFile, buf, 0600); err != nil {
|
||||||
|
return fmt.Errorf("failed writing temp file %q: %s", tempFile, err)
|
||||||
|
}
|
||||||
|
if err := os.Rename(tempFile, file); err != nil {
|
||||||
|
return fmt.Errorf("failed to rename temp file from %q to %q: %s", tempFile, file, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue