diff --git a/cmd/mapserver/config.go b/cmd/mapserver/config.go index c0834063..095dd720 100644 --- a/cmd/mapserver/config.go +++ b/cmd/mapserver/config.go @@ -9,13 +9,13 @@ import ( ) type Config struct { - UpdateAt util.TimeOfDayWrap - UpdateTimer util.DurationWrap - CTLogServerURL string - DBConfig *db.Configuration - + CTLogServerURL string + DBConfig *db.Configuration CertificatePemFile string // A X509 pem certificate PrivateKeyPemFile string // A RSA pem key + + UpdateAt util.TimeOfDayWrap + UpdateTimer util.DurationWrap } func ReadConfigFromFile(filePath string) (*Config, error) { diff --git a/cmd/mapserver/main.go b/cmd/mapserver/main.go index 43e832e3..1616d2a6 100644 --- a/cmd/mapserver/main.go +++ b/cmd/mapserver/main.go @@ -59,14 +59,15 @@ func writeSampleConfig() error { mysql.WithLocalSocket("/var/run/mysqld/mysqld.sock"), ) config := &Config{ - UpdateAt: util.NewTimeOfDay(3, 00, 00, 00), - UpdateTimer: util.DurationWrap{ - Duration: 24 * time.Hour, - }, DBConfig: dbConfig, CTLogServerURL: "https://ct.googleapis.com/logs/xenon2023/", CertificatePemFile: "tests/testdata/servercert.pem", PrivateKeyPemFile: "tests/testdata/serverkey.pem", + + UpdateAt: util.NewTimeOfDay(3, 00, 00, 00), + UpdateTimer: util.DurationWrap{ + Duration: 24 * time.Hour, + }, } return WriteConfigurationToFile(flag.Arg(0), config) @@ -102,7 +103,7 @@ func runWithConfig( // Should update now? if updateNow { - err := server.Update(ctx) + err := server.PruneAndUpdate(ctx) if err != nil { return fmt.Errorf("performing initial update: %w", err) } @@ -111,7 +112,7 @@ func runWithConfig( // Set update cycle timer. util.RunWhen(ctx, config.UpdateAt.NextTimeOfDay(), config.UpdateTimer.Duration, func(ctx context.Context) { - err := server.Update(ctx) + err := server.PruneAndUpdate(ctx) if err != nil { fmt.Printf("ERROR: update returned %s\n", err) } diff --git a/cmd/mapserver/mapserver.go b/cmd/mapserver/mapserver.go index bb0a6f1b..f670429c 100644 --- a/cmd/mapserver/mapserver.go +++ b/cmd/mapserver/mapserver.go @@ -81,7 +81,7 @@ func NewMapServer(ctx context.Context, config *Config) (*MapServer, error) { for { select { case c := <-s.updateChan: - s.update(c) + s.pruneAndUpdate(c) case <-ctx.Done(): // Requested to exit. close(s.updateChan) @@ -93,8 +93,14 @@ func NewMapServer(ctx context.Context, config *Config) (*MapServer, error) { return s, nil } -// Update triggers an update. If an ongoing update is still in process, it blocks. -func (s *MapServer) Update(ctx context.Context) error { +// Listen is responsible to start the listener for the responder. +func (s *MapServer) Listen(ctx context.Context) error { + <-ctx.Done() + return nil +} + +// PruneAndUpdate triggers an update. If an ongoing update is still in process, it blocks. +func (s *MapServer) PruneAndUpdate(ctx context.Context) error { // Signal we want an update. s.updateChan <- ctx @@ -103,15 +109,28 @@ func (s *MapServer) Update(ctx context.Context) error { return err } -func (s *MapServer) update(ctx context.Context) { +func (s *MapServer) pruneAndUpdate(ctx context.Context) { + s.prune(ctx) + s.update(ctx) +} + +func (s *MapServer) prune(ctx context.Context) { getTime := func() string { return time.Now().UTC().Format(time.RFC3339) } - fmt.Printf("======== update started at %s\n", getTime()) + fmt.Printf("======== prune started at %s\n", getTime()) + // deleteme TODO + fmt.Printf("======== prune finished at %s\n\n", getTime()) +} - // time.Sleep(3 * time.Second) +func (s *MapServer) update(ctx context.Context) { + getTime := func() string { + return time.Now().UTC().Format(time.RFC3339) + } + fmt.Printf("======== update started at %s\n", getTime()) if err := s.Updater.StartFetchingRemaining(); err != nil { + s.updateErrChan <- fmt.Errorf("retrieving start and end indices: %w", err) return } @@ -134,8 +153,3 @@ func (s *MapServer) update(ctx context.Context) { // Queue answer in form of an error: s.updateErrChan <- error(nil) } - -func (s *MapServer) Listen(ctx context.Context) error { - <-ctx.Done() - return nil -}