Skip to content

Commit

Permalink
feat: configure publish interval with command line flag
Browse files Browse the repository at this point in the history
  • Loading branch information
client-side96 committed May 30, 2024
1 parent 18fdda3 commit 06bedb2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
20 changes: 13 additions & 7 deletions cmd/pi-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import (

var scriptDir string
var addr string
var publishInterval int

func mainLoop(statsService *service.StatsService) {
func mainLoop(env config.Environment, statsService *service.StatsService) {

ticker := time.NewTicker(1 * time.Second)
ticker := time.NewTicker(time.Duration(env.PublishInterval) * time.Second)
done := make(chan bool)

defer func() {
Expand All @@ -36,8 +37,6 @@ func mainLoop(statsService *service.StatsService) {
statsService.PublishToAllClients()
case client := <-statsService.Channel:
statsService.HandleStatsSubscripition(client)
// default:

}
}

Expand All @@ -56,11 +55,18 @@ func main() {
":8000",
"Port the webserver runs on",
)
flag.IntVar(
&publishInterval,
"publish-interval",
1,
"Interval in seconds in which the statistics are published to clients",
)
flag.Parse()

env := config.Environment{
ScriptDir: scriptDir,
Addr: addr,
ScriptDir: scriptDir,
Addr: addr,
PublishInterval: publishInterval,
}

// Low level
Expand All @@ -80,7 +86,7 @@ func main() {

router := api.NewRouter(statsHandler)

go mainLoop(statsService)
go mainLoop(env, statsService)

httpServer := &http.Server{
Addr: env.Addr,
Expand Down
5 changes: 3 additions & 2 deletions internal/config/environment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

type Environment struct {
ScriptDir string
Addr string
ScriptDir string
Addr string
PublishInterval int
}

0 comments on commit 06bedb2

Please sign in to comment.