40 lines
694 B
Go
40 lines
694 B
Go
package cron
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/robfig/cron/v3"
|
|
"go.uber.org/fx"
|
|
"go.uber.org/zap"
|
|
|
|
"epic-ent/internal/config"
|
|
)
|
|
|
|
func RegisterJobs(lc fx.Lifecycle, cfg *config.Config, logger *zap.Logger) {
|
|
if !cfg.Cron.Enabled {
|
|
return
|
|
}
|
|
|
|
loc, err := time.LoadLocation(cfg.Cron.Timezone)
|
|
if err != nil {
|
|
logger.Warn("invalid timezone, fallback to Local", zap.Error(err))
|
|
loc = time.Local
|
|
}
|
|
|
|
c := cron.New(cron.WithLocation(loc))
|
|
|
|
lc.Append(fx.Hook{
|
|
OnStart: func(ctx context.Context) error {
|
|
c.Start()
|
|
logger.Info("cron started")
|
|
return nil
|
|
},
|
|
OnStop: func(ctx context.Context) error {
|
|
c.Stop()
|
|
logger.Info("cron stopped")
|
|
return nil
|
|
},
|
|
})
|
|
}
|