always re-fetch calendar in development
2 files changed, 26 insertions(+), 3 deletions(-)
M internal/calendar/calendar.go → internal/calendar/calendar.go
@@ -24,6 +24,7 @@ type Options struct { URL config.URL Timezone config.Timezone + Cache bool } type Calendar struct {@@ -63,13 +64,25 @@ }, } } -func (c *Calendar) FetchIfNeeded(ctx context.Context) error { +func (c *Calendar) ValidCache() (bool, error) { + if !c.opts.Cache { + return false, nil + } + stat, err := cache.Root.Stat(Filename) if err != nil && !errors.Is(err, fs.ErrNotExist) { - return errors.WithMessage(err, "could not stat calendar file") + return false, errors.WithMessage(err, "could not stat calendar file") } - if stat == nil || time.Since(stat.ModTime()) > Refresh || stat.Size() == 0 { + return stat == nil || time.Since(stat.ModTime()) > Refresh || stat.Size() == 0, nil +} + +func (c *Calendar) FetchIfNeeded(ctx context.Context) error { + var err error + + if valid, err := c.ValidCache(); err != nil { + return err + } else if !valid { err := c.fetch(ctx) if err != nil { return err@@ -203,6 +216,15 @@ return dates } func (c *Calendar) Availability(weeks int) ([]*CalendarDate, error) { + if !c.opts.Cache { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + err := c.FetchIfNeeded(ctx) + cancel() + if err != nil { + return nil, err + } + } + cds := make([]*CalendarDate, 0, weeks*7) dates := c.Weeks(weeks)
M internal/website/website.go → internal/website/website.go
@@ -144,6 +144,7 @@ website.calendar = calendar.New(&calendar.Options{ URL: opts.CalendarURL, Timezone: cfg.Timezone, + Cache: !opts.Development, }, log.Named("calendar")) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) if err := website.calendar.FetchIfNeeded(ctx); err != nil {