-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: persist last inventory submission time (#31)
* fix: adding support of proper 24 hours inventory resend Co-authored-by: jhvaras <jhvaras@gmail.com> Co-authored-by: Cristian Ciutea <cristi.ciutea@gmail.com> Co-authored-by: noly <noeliaddf@gmail.com> Co-authored-by: Carlos <croman@newrelic.com>
- Loading branch information
1 parent
d35cbe7
commit 6503df0
Showing
35 changed files
with
996 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package delta | ||
|
||
import ( | ||
"fmt" | ||
"github.com/newrelic/infrastructure-agent/pkg/entity" | ||
"github.com/newrelic/infrastructure-agent/pkg/helpers" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
) | ||
|
||
type EntityIDPersist interface { | ||
GetEntityID() (entity.ID, error) | ||
UpdateEntityID(id entity.ID) error | ||
} | ||
|
||
// EntityIDFilePersist will store on the given file the EntityID in order to persist it between agent restarts. | ||
type EntityIDFilePersist struct { | ||
readFile func(path string) (entity.ID, error) | ||
writeFile func(content entity.ID, path string) error | ||
filePath string | ||
lastEntityID entity.ID | ||
} | ||
|
||
// NewEntityIDFilePersist create a new instance of EntityIDFilePersist. | ||
func NewEntityIDFilePersist(dataDir string, fileName string) *EntityIDFilePersist { | ||
return &EntityIDFilePersist{ | ||
readFile: readFileFn, | ||
writeFile: writeFileFn, | ||
filePath: filepath.Join(dataDir, lastEntityIDFolder, helpers.SanitizeFileName(fileName)), | ||
} | ||
} | ||
|
||
// GetEntityID will return entityID from memory or disk. | ||
func (e *EntityIDFilePersist) GetEntityID() (entity.ID, error) { | ||
var err error | ||
if e.lastEntityID == entity.EmptyID { | ||
e.lastEntityID, err = e.readFile(e.filePath) | ||
} | ||
|
||
return e.lastEntityID, err | ||
} | ||
|
||
// UpdateEntityID will store the entityID on memory and disk. | ||
func (e *EntityIDFilePersist) UpdateEntityID(id entity.ID) error { | ||
e.lastEntityID = id | ||
|
||
return e.writeFile(id, e.filePath) | ||
} | ||
|
||
func readFileFn(filePath string) (entity.ID, error) { | ||
// Check if there is an already stored value on disk. | ||
if _, err := os.Stat(filePath); os.IsNotExist(err) { | ||
return entity.EmptyID, nil | ||
} | ||
|
||
buf, err := ioutil.ReadFile(filePath) | ||
|
||
if err != nil { | ||
return entity.EmptyID, fmt.Errorf("cannot read file persisted entityID, file: '%s', error: %v", filePath, err) | ||
} | ||
|
||
value, err := strconv.ParseInt(string(buf), 10, 64) | ||
if err != nil { | ||
return entity.EmptyID, fmt.Errorf("cannot parse entityID from file content: '%s', error: %v", buf, err) | ||
} | ||
|
||
return entity.ID(value), nil | ||
} | ||
|
||
func writeFileFn(content entity.ID, filePath string) error { | ||
dir := filepath.Dir(filePath) | ||
|
||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
if mkDirErr := os.MkdirAll(dir, DATA_DIR_MODE); mkDirErr != nil { | ||
return fmt.Errorf("cannot persist entityID, agent data directory: '%s' does not exist and cannot be created: %v", | ||
dir, mkDirErr) | ||
} | ||
} | ||
|
||
return ioutil.WriteFile(filePath, []byte(content.String()), DATA_FILE_MODE) | ||
} |
Oops, something went wrong.