serverctl/services/entry/pkg/infrastructure/dependencies/cron.go

38 lines
757 B
Go
Raw Normal View History

2022-02-16 16:27:48 +01:00
package dependencies
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/eko/gocache/cache"
"github.com/eko/gocache/store"
"github.com/go-co-op/gocron"
"go.uber.org/zap"
"time"
)
func setupCron(l *zap.Logger, cm *cache.MetricCache, cc *client.Client) {
l.Info("Setting up job scheduler (cron)")
s := gocron.NewScheduler(time.UTC)
s.Every(10).Second().Do(func() {
l.Debug("getting container list")
list, err := cc.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
l.Warn(err.Error())
return
}
err = cm.Set("docker-containers", list, &store.Options{
Cost: 2,
})
if err != nil {
l.Warn(err.Error())
return
}
})
s.StartAsync()
}