From 076c0d5afb299fedc5ee61a13bfec7f86055fc89 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 28 Sep 2024 11:58:41 +0300 Subject: can schedule entry --- internal/oi/oi.go | 17 +++++++++++++++++ internal/schedule/schedule.go | 7 ++++++- internal/schedule/stats.go | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) (limited to 'internal') diff --git a/internal/oi/oi.go b/internal/oi/oi.go index fceafd2..27112bf 100644 --- a/internal/oi/oi.go +++ b/internal/oi/oi.go @@ -1,10 +1,14 @@ package oi import ( + "errors" "fmt" "io" "os" "path/filepath" + "time" + + "golang.org/x/exp/rand" ) func EnsureDirExists(dir string) error { @@ -57,6 +61,19 @@ func ReadDirSlurp(dir string, filter func(file os.DirEntry) bool) ([]string, err return files, nil } +func ReadDirRandomEntry(dir string, filter func(file os.DirEntry) bool) (string, error) { + files, err := ReadDirSlurp(dir, filter) + if err != nil { + return "", err + } + if len(files) == 0 { + return "", errors.New("no entry/file found") + } + + rand.Seed(uint64(time.Now().UnixNano())) + return files[rand.Intn(len(files))], nil +} + func IsRegular(path string) bool { stat, err := os.Stat(path) if err != nil { diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go index 17c9588..d8b7143 100644 --- a/internal/schedule/schedule.go +++ b/internal/schedule/schedule.go @@ -4,9 +4,11 @@ import ( "errors" "fmt" "log" + "os" "strings" "codeberg.org/snonux/gos/internal/config" + "codeberg.org/snonux/gos/internal/oi" ) var NothingToSchedule = errors.New("nothing to schedule") @@ -24,5 +26,8 @@ func Run(args config.Args, platform string) (string, error) { return "", NothingToSchedule } - return "", NothingToSchedule + // Schedule random qeued entry for platform + return oi.ReadDirRandomEntry(dir, func(file os.DirEntry) bool { + return strings.HasSuffix(file.Name(), ".queued") + }) } diff --git a/internal/schedule/stats.go b/internal/schedule/stats.go index aac3737..b36fd54 100644 --- a/internal/schedule/stats.go +++ b/internal/schedule/stats.go @@ -117,6 +117,7 @@ func pastTime(duration time.Duration) time.Time { return nowTime().Add(-duration) } +// TODO: Maybe introduce types.Entry struct, which encapsulates all the parsing func parseEntryPath(filePath string) (time.Time, error) { // Format: foobarbaz.something.here.txt.STAMP.{posted,queued} // We want to get the STAMP! -- cgit v1.2.3