Skip to content

Commit

Permalink
bugfix: send stats and error handling on schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
brondum committed Dec 8, 2020
1 parent feca4dd commit 29f15d9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
11 changes: 3 additions & 8 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@ var version = config.EnvVars.Version
func SetupRouter() {
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()

appConfig, err := config.ReadConfig(config.EnvVars.Config)
if err != nil {
logger.Fatal().Err(err).Msg("failed to read github app config")
}

server, err := baseapp.NewServer(
appConfig.Server,
config.AppConfig.Server,
baseapp.DefaultParams(logger, fmt.Sprintf("%s.", appName))...,
)
if err != nil {
logger.Panic().Err(err)
}

cc, err := githubapp.NewDefaultCachingClientCreator(
appConfig.Github,
config.AppConfig.Github,
githubapp.WithClientUserAgent(fmt.Sprintf("%s/%s", appName, version)),
githubapp.WithClientTimeout(10*time.Second),
githubapp.WithClientCaching(false, func() httpcache.Cache { return httpcache.NewMemoryCache() }),
Expand All @@ -46,7 +41,7 @@ func SetupRouter() {
logger.Panic().Err(err)
}

webhookHandler := githubapp.NewEventDispatcher([]githubapp.EventHandler{&ConfigCheckHandler{ClientCreator: cc}}, appConfig.Github.App.WebhookSecret, githubapp.WithScheduler(
webhookHandler := githubapp.NewEventDispatcher([]githubapp.EventHandler{&ConfigCheckHandler{ClientCreator: cc}}, config.AppConfig.Github.App.WebhookSecret, githubapp.WithScheduler(
githubapp.AsyncScheduler(),
))
server.Mux().Handle(pat.Post("/"), webhookHandler)
Expand Down
14 changes: 8 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ type RepoConfig struct {
Labels []string `yaml:"labels,omitempty"`
}

// AppConfig contains global app config
var AppConfig Config

// ReadConfig reads a yaml config file
func ReadConfig(path string) (*Config, error) {
var c Config
func ReadConfig(path string) error {

bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "failed reading server config file: %s", path)
return errors.Wrapf(err, "failed reading server config file: %s", path)
}

if err := yaml.UnmarshalStrict(bytes, &c); err != nil {
return nil, errors.Wrap(err, "failed parsing configuration file")
if err := yaml.UnmarshalStrict(bytes, &AppConfig); err != nil {
return errors.Wrap(err, "failed parsing configuration file")
}

return &c, nil
return nil
}

// ReadRepoConfig reads a yaml file
Expand Down
45 changes: 25 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
log.Fatal().Err(err).Msg("failed to read env config")
}

appConfig, err := config.ReadConfig(config.EnvVars.Config)
err = config.ReadConfig(config.EnvVars.Config)
if err != nil {
log.Fatal().Err(err).Msg("failed to read github app config")
}
Expand All @@ -38,31 +38,36 @@ func main() {
log.Error().Err(err).Msg("failed to register dogstatsd client")
}

//schedule checks
schedule := gocron.NewScheduler(time.UTC)
_, err = schedule.Every(1).Day().At("22:00").Do(runDependabot)
if err != nil {
log.Fatal().Err(err).Msg("failed to create schedule")
}
schedule.StartAsync()

// start webhook
api.SetupRouter()

}

func runDependabot() {
// create clients
clients, err := gh.GetOrganizationClients(appConfig.Github)
clients, err := gh.GetOrganizationClients(config.AppConfig.Github)
if err != nil {
log.Fatal().Err(err).Msg("failed to register organization client")
}

// send stats to dd
go datadog.Gauge("organizations", float64(len(clients)), nil)

//schedule checks
schedule := gocron.NewScheduler(time.UTC)
schedule.Every(1).Day().At("22:00").Do(func() {
for _, client := range clients {
wg.Add(1)
client := client
go func() {
defer wg.Done()
dependabot.Start(context.Background(), client)
}()
}
wg.Wait()
})
schedule.StartAsync()

// start webhook
api.SetupRouter()

for _, client := range clients {
wg.Add(1)
client := client
go func() {
defer wg.Done()
dependabot.Start(context.Background(), client)
}()
}
wg.Wait()
}

0 comments on commit 29f15d9

Please sign in to comment.