diff --git a/README.md b/README.md index 4ee98009..e0e91693 100644 --- a/README.md +++ b/README.md @@ -41,18 +41,19 @@ publisher: http_port: 8080 -cutoff_delta: 5m +poll_rate: 5m timer: false ``` -`cutoff_delta` string formatted for [duration parser](https://golang.org/pkg/time/#ParseDuration). -`timer` will configure interal polling of the `enabled_feeds` at the given `cutoff_delta` period. To specify this configuration file, define its path in your environment under the `PACKAGE_FEEDS_CONFIG_PATH` variable. +`poll_rate` string formatted for [duration parser](https://golang.org/pkg/time/#ParseDuration).This is used as an initial value to generate a cutoff point for feed events relative to the given time at execution, with subsequent events using the previous time at execution as the cutoff point. +`timer` will configure interal polling of the `enabled_feeds` at the given `poll_rate` period. To specify this configuration file, define its path in your environment under the `PACKAGE_FEEDS_CONFIG_PATH` variable. ## Legacy Configuration Legacy configuration methods are still supported. By default, without a configuration file all feeds will be enabled. The environment variable `OSSMALWARE_TOPIC_URL` can be used to select the GCP pubsub publisher and `PORT` will configure the port for the HTTP server. -The default `cutoff_delta` is 5 minutes, this is used to generate a cutoff point for feed events relative to the given time at execution. +The default `poll_rate` is 5 minutes, it is assumed that an external service is dispatching requests to the configured HTTP server at this frequency. + # Contributing diff --git a/cmd/scheduled-feed/main.go b/cmd/scheduled-feed/main.go index c74042ed..bcab7fa4 100644 --- a/cmd/scheduled-feed/main.go +++ b/cmd/scheduled-feed/main.go @@ -21,11 +21,13 @@ import ( type FeedHandler struct { scheduler *scheduler.Scheduler pub publisher.Publisher - delta time.Duration + pollRate time.Duration + lastPoll time.Time } func (handler *FeedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - cutoff := time.Now().UTC().Add(-handler.delta) + cutoff := handler.getCutoff() + handler.lastPoll = time.Now().UTC() pkgs, errs := handler.scheduler.Poll(cutoff) if len(errs) > 0 { for _, err := range errs { @@ -62,6 +64,16 @@ func (handler *FeedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +func (handler FeedHandler) getCutoff() time.Time { + var cutoff time.Time + if handler.lastPoll.IsZero() { + cutoff = time.Now().UTC().Add(-handler.pollRate) + } else { + cutoff = handler.lastPoll + } + return cutoff +} + func main() { configPath, useConfig := os.LookupEnv("PACKAGE_FEEDS_CONFIG_PATH") var err error @@ -92,19 +104,19 @@ func main() { sched := scheduler.New(feeds) log.Printf("listening on port %v", appConfig.HTTPPort) - delta, err := time.ParseDuration(appConfig.CutoffDelta) + pollRate, err := time.ParseDuration(appConfig.PollRate) if err != nil { log.Fatal(err) } handler := &FeedHandler{ scheduler: sched, pub: pub, - delta: delta, + pollRate: pollRate, } if appConfig.Timer { cronjob := cron.New() - crontab := fmt.Sprintf("@every %s", delta.String()) + crontab := fmt.Sprintf("@every %s", pollRate.String()) log.Printf("Running a timer %s", crontab) err := cronjob.AddFunc(crontab, func() { cronRequest(appConfig.HTTPPort) }) if err != nil { diff --git a/config/config_test.go b/config/config_test.go index c81dd303..bf2b1caf 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -22,7 +22,7 @@ publisher: endpoint: "https://foobaz.com" http_port: 8080 -cutoff_delta: 5m +poll_rate: 5m timer: true ` TestConfigStrUnknownFeedType = ` diff --git a/config/scheduledfeed.go b/config/scheduledfeed.go index 41d54627..6e7ffbd1 100644 --- a/config/scheduledfeed.go +++ b/config/scheduledfeed.go @@ -167,9 +167,9 @@ func Default() *ScheduledFeedConfig { PubConfig: PublisherConfig{ Type: stdout.PublisherType, }, - HTTPPort: 8080, - CutoffDelta: "5m", - Timer: false, + HTTPPort: 8080, + PollRate: "5m", + Timer: false, } config.applyEnvVars() return config diff --git a/config/structs.go b/config/structs.go index 0992a7ca..e79cfd2e 100644 --- a/config/structs.go +++ b/config/structs.go @@ -4,7 +4,7 @@ type ScheduledFeedConfig struct { PubConfig PublisherConfig `yaml:"publisher"` EnabledFeeds []string `yaml:"enabled_feeds"` HTTPPort int `yaml:"http_port,omitempty"` - CutoffDelta string `yaml:"cutoff_delta"` + PollRate string `yaml:"poll_rate"` Timer bool `yaml:"timer"` }