Skip to content

Commit

Permalink
feat: use cron intervals (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Feb 5, 2023
1 parent fb729cb commit 83903dd
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 37 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ linters:
- gosec
- testpackage
- gci
- musttag
9 changes: 7 additions & 2 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
state-path = "cosmos-proposals-checker-state.json"
# A path to a file that will store mutes of this app. Omitting it will disable the mute functionality.
mutes-path = "cosmos-proposals-checker-mutes.json"
# Interval to check and notify, in seconds. Defaults to 3600 (1 hour)
interval = 60
# Interval to check and notify.
# Can be a cron-like interval (like "0 * * * *" for every hour),
# "@hourly"/"@daily" for hourly/daily intervals, or "@every 1h"
# for specifying intervals more granularly.
# See https://pkg.go.dev/github.com/robfig/cron?utm_source=godoc#hdr-CRON_Expression_Format for examples.
# Defaults to "@hourly"
interval = "@hourly"

# Logging configuration
[log]
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/BurntSushi/toml v1.1.0
github.com/mcuadros/go-defaults v1.2.0
github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.26.1
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
Expand Down
88 changes: 54 additions & 34 deletions pkg/app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package pkg

import (
"time"

configPkg "main/pkg/config"
"main/pkg/logger"
mutesManager "main/pkg/mutes_manager"
Expand All @@ -12,18 +10,23 @@ import (
"main/pkg/reporters/telegram"
"main/pkg/state/generator"
"main/pkg/state/manager"

cron "github.com/robfig/cron/v3"
"github.com/rs/zerolog"
)

type App struct {
ConfigPath string
Logger *zerolog.Logger
Config *configPkg.Config
StateManager *manager.StateManager
MutesManager *mutesManager.MutesManager
ReportGenerator *reportPkg.ReportGenerator
StateGenerator *generator.StateGenerator
Reporters []reportersPkg.Reporter
}

func NewApp(configPath string) *App {
return &App{ConfigPath: configPath}
}

func (a *App) Start() {
config, err := configPkg.GetConfig(a.ConfigPath)
config, err := configPkg.GetConfig(configPath)
if err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not load config")
}
Expand All @@ -35,47 +38,64 @@ func (a *App) Start() {
log := logger.GetLogger(config.LogConfig)

stateManager := manager.NewStateManager(config.StatePath, log)
stateManager.Load()

mutesManager := mutesManager.NewMutesManager(config.MutesPath, log)
mutesManager.Load()

reportGenerator := reportPkg.NewReportGenerator(stateManager, log, config.Chains)
stateGenerator := generator.NewStateGenerator(log, config.Chains)

reporters := []reportersPkg.Reporter{
pagerduty.NewPagerDutyReporter(config.PagerDutyConfig, log),
telegram.NewTelegramReporter(config.TelegramConfig, mutesManager, stateGenerator, log),
return &App{
Logger: log,
Config: config,
StateManager: stateManager,
MutesManager: mutesManager,
ReportGenerator: reportGenerator,
StateGenerator: stateGenerator,
Reporters: []reportersPkg.Reporter{
pagerduty.NewPagerDutyReporter(config.PagerDutyConfig, log),
telegram.NewTelegramReporter(config.TelegramConfig, mutesManager, stateGenerator, log),
},
}
}

func (a *App) Start() {
a.StateManager.Load()
a.MutesManager.Load()

for _, reporter := range reporters {
for _, reporter := range a.Reporters {
reporter.Init()
if reporter.Enabled() {
log.Info().Str("name", reporter.Name()).Msg("Init reporter")
a.Logger.Info().Str("name", reporter.Name()).Msg("Init reporter")
}
}

for {
newState := stateGenerator.GetState(stateManager.State)
report := reportGenerator.GenerateReport(stateManager.State, newState)
c := cron.New()
if _, err := c.AddFunc(a.Config.Interval, func() {
a.Report()
}); err != nil {
a.Logger.Fatal().Err(err).Msg("Error processing cron pattern")
}
c.Start()
a.Logger.Info().Str("interval", a.Config.Interval).Msg("Scheduled proposals reporting")

if report.Empty() {
log.Debug().Msg("Empty report, not sending.")
time.Sleep(time.Second * time.Duration(config.Interval))
continue
}
select {}
}

log.Debug().Int("len", len(report.Entries)).Msg("Got non-empty report")
func (a *App) Report() {
newState := a.StateGenerator.GetState(a.StateManager.State)
report := a.ReportGenerator.GenerateReport(a.StateManager.State, newState)

for _, reporter := range reporters {
if reporter.Enabled() {
log.Debug().Str("name", reporter.Name()).Msg("Sending report...")
if err := reporter.SendReport(report); err != nil {
log.Error().Err(err).Str("name", reporter.Name()).Msg("Failed to send report")
}
if report.Empty() {
a.Logger.Debug().Msg("Empty report, not sending.")
return
}

a.Logger.Debug().Int("len", len(report.Entries)).Msg("Got non-empty report")

for _, reporter := range a.Reporters {
if reporter.Enabled() {
a.Logger.Debug().Str("name", reporter.Name()).Msg("Sending report...")
if err := reporter.SendReport(report); err != nil {
a.Logger.Error().Err(err).Str("name", reporter.Name()).Msg("Failed to send report")
}
}

time.Sleep(time.Second * time.Duration(config.Interval))
}
}
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Config struct {
StatePath string `toml:"state-path"`
MutesPath string `toml:"mutes-path"`
Chains types.Chains `toml:"chains"`
Interval int64 `toml:"interval" default:"3600"`
Interval string `toml:"interval" default:"* * * * *"`
}

type PagerDutyConfig struct {
Expand Down

0 comments on commit 83903dd

Please sign in to comment.