feat: auto start workers
This commit is contained in:
31
backend/modules/workers/provider.gen.go
Executable file
31
backend/modules/workers/provider.gen.go
Executable 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
|
||||
}
|
||||
39
backend/modules/workers/workers.go
Normal file
39
backend/modules/workers/workers.go
Normal 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")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user