add timer for data updater worker
This commit is contained in:
parent
7f144124e8
commit
ae64ee452e
54
internal/workers/data_updater/data_updater.go
Normal file
54
internal/workers/data_updater/data_updater.go
Normal file
@ -0,0 +1,54 @@
|
||||
package data_updater
|
||||
|
||||
import (
|
||||
"amocrm/internal/repository"
|
||||
"amocrm/pkg/amoClient"
|
||||
"context"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Deps struct {
|
||||
AmoClient *amoClient.Amo
|
||||
Repo *repository.Repository
|
||||
Logger *zap.Logger
|
||||
}
|
||||
|
||||
type DataUpdater struct {
|
||||
amoClient *amoClient.Amo
|
||||
repo *repository.Repository
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewDataUpdaterWC(deps Deps) *DataUpdater {
|
||||
return &DataUpdater{
|
||||
amoClient: deps.AmoClient,
|
||||
repo: deps.Repo,
|
||||
logger: deps.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *DataUpdater) Start(ctx context.Context) {
|
||||
nextStart := calculateTime()
|
||||
ticker := time.NewTicker(time.Second * time.Duration(nextStart))
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
wc.processTasks(ctx)
|
||||
nextStart = calculateTime()
|
||||
ticker.Reset(time.Second * time.Duration(nextStart))
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *DataUpdater) processTasks(ctx context.Context) {
|
||||
|
||||
}
|
||||
|
||||
func (wc *DataUpdater) Stop(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
20
internal/workers/data_updater/timer.go
Normal file
20
internal/workers/data_updater/timer.go
Normal file
@ -0,0 +1,20 @@
|
||||
package data_updater
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func calculateTime() int64 {
|
||||
now := time.Now()
|
||||
|
||||
targetTime := time.Date(now.Year(), now.Month(), now.Day(), 4, 0, 0, 0, now.Location())
|
||||
|
||||
if now.After(targetTime) {
|
||||
targetTime = targetTime.AddDate(0, 0, 1)
|
||||
}
|
||||
|
||||
toTarget := targetTime.Sub(now)
|
||||
sec := int64(toTarget.Seconds())
|
||||
|
||||
return sec
|
||||
}
|
@ -16,21 +16,21 @@ type Deps struct {
|
||||
Logger *zap.Logger
|
||||
}
|
||||
|
||||
type Refresh struct {
|
||||
type Token struct {
|
||||
amoClient *amoClient.Amo
|
||||
repo *repository.Repository
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewRefreshWC(deps Deps) *Refresh {
|
||||
return &Refresh{
|
||||
func NewRefreshWC(deps Deps) *Token {
|
||||
return &Token{
|
||||
amoClient: deps.AmoClient,
|
||||
repo: deps.Repo,
|
||||
logger: deps.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *Refresh) Start(ctx context.Context) {
|
||||
func (wc *Token) Start(ctx context.Context) {
|
||||
ticker := time.NewTicker(5 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
|
||||
@ -45,7 +45,7 @@ func (wc *Refresh) Start(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (wc Refresh) processTasks(ctx context.Context) {
|
||||
func (wc *Token) processTasks(ctx context.Context) {
|
||||
tokens, err := wc.repo.CheckExpired(ctx)
|
||||
if err != nil {
|
||||
wc.logger.Error("error fetch expired tokens in mongo", zap.Error(err))
|
||||
@ -75,6 +75,6 @@ func (wc Refresh) processTasks(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *Refresh) Stop(ctx context.Context) error {
|
||||
func (wc *Token) Stop(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user