Skip to content

Commit

Permalink
ref: check directive script modtime at script run time
Browse files Browse the repository at this point in the history
Rather than checking it while parsing inventory once and storing it, the
modtime will now be checked each time the script is run for validity.
  • Loading branch information
tjhop committed Jan 18, 2023
1 parent 4284ef2 commit 76ce5fa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
27 changes: 4 additions & 23 deletions internal/inventory/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ import (
// script has a modification time within the last 24h.
// - ID: string idenitfying the directive script (generally the file path to the script)
// - Script: embedded `Script` object
// - ModTime: the modification time of the script
type DirectiveScript struct {
id string
script Script
modTime time.Time
}

// String is a stringer to return the module ID
Expand All @@ -37,10 +35,10 @@ func (d DirectiveScript) Run(ctx context.Context) error {
"directive": d,
})

// TODO: fix this. currently, mtime is only checked when modules are
// parsed. it should be moved to be checked during runtime.
recentThreshold, _ := time.ParseDuration("24h")
if float64(time.Now().Unix()-d.modTime.Unix()) < recentThreshold.Seconds() {
threshold, _ := time.ParseDuration("24h")
modTime := utils.GetFileModifiedTime(d.script.Path).Unix()
recent := time.Now().Unix() - modTime
if float64(recent) < threshold.Seconds() {
logger.Info("Directive recently modified, running now")

if err := d.script.Run(ctx); err != nil {
Expand Down Expand Up @@ -93,29 +91,12 @@ func (i *Inventory) ParseDirectives() error {
for _, file := range files {
if !file.IsDir() {
scriptPath := filepath.Join(path, file.Name())
info, err := file.Info()
if err != nil {
log.WithFields(log.Fields{
"path": scriptPath,
"error": err,
}).Error("Failed to get file info")

// inventory counts haven't been altered, no need to update here
metricInventoryReloadFailedTotal.With(prometheus.Labels{
"inventory": commonLabels["inventory"],
"component": commonLabels["component"],
"hostname": commonLabels["hostname"],
}).Inc()

return err
}

dirScripts = append(dirScripts, DirectiveScript{
script: Script{
ID: file.Name(),
Path: scriptPath,
},
modTime: info.ModTime(),
})
}
}
Expand Down
14 changes: 14 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/fs"
"os"
"path/filepath"
"time"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -36,6 +37,19 @@ func GetFilesInDirectory(path string) ([]fs.DirEntry, error) {
return files, nil
}

// GetFileModifiedTime accepts a path to a file/directory. It returns the
// file's last modified time, or an empty time literal in the event of failure
// (read: if this function fails, it returns the epoch timestamp -- use
// `time.IsZero()` to check if return is the zero time instant for failures).
func GetFileModifiedTime(path string) time.Time {
file, err := os.Stat(path)
if err != nil {
return time.Time{}
}

return file.ModTime()
}

// IsFileExecutableToAll accepts an `fs.DirEntry` struct and
// simply returns true if the given file is executable to all
// (ie, `--x--x--x`), else false.
Expand Down

0 comments on commit 76ce5fa

Please sign in to comment.