feat: auto start workers

This commit is contained in:
Rogee
2024-12-16 16:59:48 +08:00
parent 66d0cd507c
commit 71c285b892
10 changed files with 133 additions and 5 deletions

View File

@@ -0,0 +1,31 @@
package workers
import (
"backend/modules/commands/store"
"backend/providers/storage"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/contracts"
"git.ipao.vip/rogeecn/atom/utils/opt"
)
func Provide(opts ...opt.Option) error {
if err := container.Container.Provide(func(
cmdStore *store.StoreMedias,
storage *storage.Config,
) (contracts.Initial, error) {
obj := &Controller{
cmdStore: cmdStore,
storage: storage,
}
if err := obj.Prepare(); err != nil {
return nil, err
}
return obj, nil
}, atom.GroupInitial); err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,39 @@
package workers
import (
"time"
"backend/modules/commands/store"
"backend/providers/storage"
_ "git.ipao.vip/rogeecn/atom"
_ "git.ipao.vip/rogeecn/atom/contracts"
"github.com/sirupsen/logrus"
)
// @provider:except contracts.Initial atom.GroupInitial
type Controller struct {
log *logrus.Entry `inject:"false"`
cmdStore *store.StoreMedias
storage *storage.Config
}
func (c *Controller) Prepare() error {
c.log = logrus.WithField("module", "workers.worker")
c.log.Info("start workers")
go c.store()
return nil
}
func (c *Controller) store() {
ticker := time.NewTicker(time.Minute * 5)
for range ticker.C {
c.log.WithField("action", "store").Info("start to run store")
if err := c.cmdStore.RunE(c.storage.Path); err != nil {
c.log.WithField("action", "store").WithError(err).Error("run store cmd failed")
}
}
}