Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lastPoll time as cutoff on subsequent polls #94

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
tom--pollard marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -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
Expand Down Expand Up @@ -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 {
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 @@ -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
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