Skip to content

Commit

Permalink
Extract cutoff delta to configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
tom--pollard committed Apr 13, 2021
1 parent 42ece6a commit a187fcd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ publisher:
url: "gcppubsub://foobar.com"
http_port: 8080
cutoff_delta: 5m
```

To specify this configuration file, define its path in your environment under the `PACKAGE_FEEDS_CONFIG_PATH` variable.
`cutoff_delta` string formatted for [duration parser](https://golang.org/pkg/time/#ParseDuration). 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.

# Contributing

Expand Down
12 changes: 8 additions & 4 deletions cmd/scheduled-feed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
"net/http"
"os"
"time"
"strings"
"time"

"github.com/ossf/package-feeds/config"
"github.com/ossf/package-feeds/feeds/scheduler"
Expand All @@ -16,16 +16,15 @@ import (
log "github.com/sirupsen/logrus"
)

const delta = 5 * time.Minute

// FeedHandler is a handler that fetches new packages from various feeds
type FeedHandler struct {
scheduler *scheduler.Scheduler
pub publisher.Publisher
delta time.Duration
}

func (handler *FeedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cutoff := time.Now().UTC().Add(-delta)
cutoff := time.Now().UTC().Add(-handler.delta)
pkgs, errs := handler.scheduler.Poll(cutoff)
if len(errs) > 0 {
for _, err := range errs {
Expand Down Expand Up @@ -87,9 +86,14 @@ func main() {
sched := scheduler.New(feeds)

log.Printf("listening on port %v", appConfig.HttpPort)
delta, err := time.ParseDuration(appConfig.CutoffDelta)
if err != nil {
log.Fatal(err)
}
handler := &FeedHandler{
scheduler: sched,
pub: pub,
delta: delta,
}
http.Handle("/", handler)
if err := http.ListenAndServe(fmt.Sprintf(":%v", appConfig.HttpPort), nil); err != nil {
Expand Down
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ publisher:
endpoint: "https://foobaz.com"
http_port: 8080
cutoff_delta: 5m
`
TestConfigStrUnknownFeedType = `
enabled_feeds:
Expand Down
3 changes: 2 additions & 1 deletion config/scheduledfeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ func Default() *ScheduledFeedConfig {
PubConfig: PublisherConfig{
Type: stdout.PublisherType,
},
HttpPort: 8080,
HttpPort: 8080,
CutoffDelta: "5m",
}
config.applyEnvVars()
return config
Expand Down
1 change: 1 addition & 0 deletions config/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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"`
}

type PublisherConfig struct {
Expand Down

0 comments on commit a187fcd

Please sign in to comment.