diff --git a/main.go b/main.go index 20287cd..36d5989 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "net/http" "strings" "os" + "context" + "time" ) // ELIGIBLE_APPOINTMENTS is a comma-separated list of appointment type @@ -57,6 +59,26 @@ func main() { } srv := NewServer(NewClient(apiKey, dryRun), store, secret, loadEligible()) + // Prime the cache: a fresh container starts empty. + { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) + if err := srv.SyncRoster(ctx); err != nil { + log.Printf("initial sync failed: %v", err) + } + cancel() + } + + go func() { + t := time.NewTicker(time.Hour) + defer t.Stop() + for range t.C { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) + if err := srv.SyncRoster(ctx); err != nil { + log.Printf("scheduled sync failed: %v", err) + } + cancel() + } + }() log.Println("listening on :8080") log.Fatal(http.ListenAndServe(":8080", srv.Routes())) }