Skip to content

Commit

Permalink
Use lastPoll time as cutoff on subsequent polls
Browse files Browse the repository at this point in the history
This also renames the `cutoff_delta` configuration option to
`poll_rate`, updating the README to better reflect its behaviour
  • Loading branch information
tom--pollard committed Apr 28, 2021
1 parent 4d23df3 commit a9c41c1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 17 additions & 5 deletions cmd/scheduled-feed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -59,6 +61,16 @@ func (handler *FeedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("%d packages processed", processed)))
}

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
Expand Down Expand Up @@ -89,19 +101,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)
cronjob.AddFunc(crontab, func() { cronrequest(appConfig.HttpPort) })
cronjob.Start()
Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ publisher:
endpoint: "https://foobaz.com"
http_port: 8080
cutoff_delta: 5m
poll_rate: 5m
timer: true
`
TestConfigStrUnknownFeedType = `
Expand Down
6 changes: 3 additions & 3 deletions config/scheduledfeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,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
Expand Down
2 changes: 1 addition & 1 deletion config/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down

0 comments on commit a9c41c1

Please sign in to comment.