68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
|
package privilegewc
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"github.com/themakers/hlog"
|
||
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
|
||
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type CheckWorkerConfig struct {
|
||
|
TickerInterval time.Duration
|
||
|
DefaultData model.DefaultData
|
||
|
Logger hlog.Logger
|
||
|
ErrChan chan<- error
|
||
|
}
|
||
|
|
||
|
type CheckWorker struct {
|
||
|
config CheckWorkerConfig
|
||
|
privilegeDAL *dal.DAL
|
||
|
}
|
||
|
|
||
|
func NewCheckWorker(config CheckWorkerConfig, privilegeDAL *dal.DAL) *CheckWorker {
|
||
|
return &CheckWorker{
|
||
|
config: config,
|
||
|
privilegeDAL: privilegeDAL,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (w *CheckWorker) Start(ctx context.Context) {
|
||
|
ticker := time.NewTicker(w.config.TickerInterval)
|
||
|
defer ticker.Stop()
|
||
|
|
||
|
for {
|
||
|
select {
|
||
|
case <-ticker.C:
|
||
|
fmt.Println("CHECK")
|
||
|
w.performScheduledTasks(ctx)
|
||
|
case <-ctx.Done():
|
||
|
fmt.Println("Check worker terminated")
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// TODO: Maybe one query?
|
||
|
func (w *CheckWorker) performScheduledTasks(ctx context.Context) {
|
||
|
fmt.Println("CHEC0")
|
||
|
w.deleteExpired(ctx)
|
||
|
}
|
||
|
|
||
|
func (w *CheckWorker) deleteExpired(ctx context.Context) {
|
||
|
expiredData, err := w.privilegeDAL.AccountRepo.GetExpired(ctx, w.config.DefaultData.UnlimID)
|
||
|
if err != nil {
|
||
|
w.config.Logger.Module("Error getting expired quizUnlimTime records")
|
||
|
w.config.ErrChan <- err
|
||
|
}
|
||
|
|
||
|
for _, data := range expiredData {
|
||
|
err := w.privilegeDAL.AccountRepo.DeletePrivilegeByID(ctx, data.ID)
|
||
|
if err != nil {
|
||
|
w.config.Logger.Module("Error deleting expired quizUnlimTime record")
|
||
|
w.config.ErrChan <- err
|
||
|
}
|
||
|
}
|
||
|
}
|