init
This commit is contained in:
40
internal/daemon/daemon.go
Normal file
40
internal/daemon/daemon.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.jthan.io/jonathan/gobar/internal/config"
|
||||
"git.jthan.io/jonathan/gobar/internal/collectors"
|
||||
"git.jthan.io/jonathan/gobar/internal/output"
|
||||
)
|
||||
|
||||
func Run(ctx context.Context, cfg config.Config) error {
|
||||
ticker := time.NewTicker(cfg.Interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
log.Printf("daemon started (interval=%s)", cfg.Interval)
|
||||
|
||||
for {
|
||||
select {
|
||||
|
||||
case <-ctx.Done():
|
||||
log.Println("daemon shutting down")
|
||||
return nil
|
||||
|
||||
case <-ticker.C:
|
||||
runOnce(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runOnce(ctx context.Context) {
|
||||
m, err := collectors.Collect()
|
||||
if err != nil {
|
||||
log.Println("collect error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
output.Print(m)
|
||||
}
|
||||
Reference in New Issue
Block a user