Ensuring collectd config file is synced to disk
This was causing weird parse errors when the agent would start up collectd due
to the config file only being partially written.
@@ -13,13 +13,12 @@ import (
"errors"
"fmt"
"log"
+ "os"
"sync"
"time"
"path/filepath"
- "io/ioutil"
-
cfg "github.com/signalfx/neo-agent/config"
"github.com/signalfx/neo-agent/plugins"
"github.com/signalfx/neo-agent/plugins/monitors/collectd/config"
@@ -207,10 +206,24 @@ func (collectd *Collectd) writePlugins(plugins []*config.Plugin) error {
return err
}
- if err := ioutil.WriteFile(collectd.confFile, []byte(config), 0644); err != nil {
+ f, err := os.Create(collectd.confFile)
+ if err != nil {
+ return fmt.Errorf("failed to truncate collectd config: %s", err)
+ }
+ defer f.Close()
+
+ _, err = f.Write([]byte(config))
+ if err != nil {
return fmt.Errorf("failed to write collectd config: %s", err)
}
+ // We need to sync here since collectd might be restarted very quickly
+ // after writing this.
+ err = f.Sync()
+ if err != nil {
+ return fmt.Errorf("failed to sync collectd config file to disk: %s", err)
+ }
+
return nil
}
