ensure reasonable ordering of declarations in source
1 file changed, 87 insertions(+), 87 deletions(-)
changed files
M domain/content/fetcher/fetcher.go → domain/content/fetcher/fetcher.go
@@ -23,12 +23,6 @@ "github.com/Southclaws/fault/fmsg" "github.com/google/renameio/v2" ) -var ( - files = []string{"config.toml", "site.db"} - numericFilename = regexp.MustCompile("[0-9]{3,}") - timeout = 10 * time.Second -) - type Fetcher struct { options *Options log *log.Logger@@ -43,6 +37,12 @@ FetchURL config.URL Listener events.Listener } +var ( + files = []string{"config.toml", "site.db"} + numericFilename = regexp.MustCompile("[0-9]{3,}") + timeout = 10 * time.Second +) + func New(log *log.Logger, options *Options) Fetcher { return Fetcher{ log: log,@@ -51,6 +51,87 @@ updater: options.Listener, } } +func (f *Fetcher) CleanOldRevisions() error { + contents, err := os.ReadDir(f.options.Root) + if err != nil { + return fault.Wrap(err, fmsg.With("could not read root directory")) + } + for _, file := range contents { + name := file.Name() + if name == "current" { + continue + } + if numericFilename.MatchString(name) { + v, err := strconv.ParseUint(name, 10, 64) + if err != nil { + return fault.Wrap( + err, + fmsg.With(fmt.Sprintf("could not parse numeric filename %s", name)), + ) + } + if v < f.current-1 { + err := os.RemoveAll(filepath.Join(f.options.Root, name)) + if err != nil { + return fault.Wrap(err, fmsg.With("could not remove folder")) + } + } + } + } + + return nil +} + +func (f *Fetcher) Subscribe() (<-chan string, error) { + err := f.checkFolder() + if err != nil { + return nil, err + } + + var root string + f.current, err = f.getCurrentVersion() + if err != nil { + f.log.Warn("could not get current version", "error", err) + } + + if !f.options.RedisEnabled { + root = f.path(f.current) + } else { + runID, err := f.initialiseStorage() + if err != nil { + return nil, err + } + root = f.path(runID) + } + + ch := make(chan string, 1) + go func() { + var err error + var attempt uint + for { + err = f.connect(root, ch) + if err == nil { + return + } + + next := expBackoff(attempt) + attempt++ + f.log.Warn( + "could not connect to update listener", + "error", + err, + "attempt", + attempt, + "next_try", + next, + ) + + <-time.After(next) + } + }() + + return ch, nil +} + func (f *Fetcher) getArtefacts(run uint64) error { runID := strconv.FormatUint(run, 10) f.log.Debug("getting artefacts", "run_id", runID)@@ -93,36 +174,6 @@ if len(badFiles) > 0 { return fault.Wrap( fault.Newf("unexpected files in root directory: %s", strings.Join(badFiles, ", ")), ) - } - - return nil -} - -func (f *Fetcher) CleanOldRevisions() error { - contents, err := os.ReadDir(f.options.Root) - if err != nil { - return fault.Wrap(err, fmsg.With("could not read root directory")) - } - for _, file := range contents { - name := file.Name() - if name == "current" { - continue - } - if numericFilename.MatchString(name) { - v, err := strconv.ParseUint(name, 10, 64) - if err != nil { - return fault.Wrap( - err, - fmsg.With(fmt.Sprintf("could not parse numeric filename %s", name)), - ) - } - if v < f.current-1 { - err := os.RemoveAll(filepath.Join(f.options.Root, name)) - if err != nil { - return fault.Wrap(err, fmsg.With("could not remove folder")) - } - } - } } return nil@@ -208,57 +259,6 @@ return latest, nil } return f.current, nil -} - -func (f *Fetcher) Subscribe() (<-chan string, error) { - err := f.checkFolder() - if err != nil { - return nil, err - } - - var root string - f.current, err = f.getCurrentVersion() - if err != nil { - f.log.Warn("could not get current version", "error", err) - } - - if !f.options.RedisEnabled { - root = f.path(f.current) - } else { - runID, err := f.initialiseStorage() - if err != nil { - return nil, err - } - root = f.path(runID) - } - - ch := make(chan string, 1) - go func() { - var err error - var attempt uint - for { - err = f.connect(root, ch) - if err == nil { - return - } - - next := expBackoff(attempt) - attempt++ - f.log.Warn( - "could not connect to update listener", - "error", - err, - "attempt", - attempt, - "next_try", - next, - ) - - <-time.After(next) - } - }() - - return ch, nil } func (f *Fetcher) connect(root string, ch chan string) error {