update lct for sharding
Only enable non-guild specific shedules on shard with ID `0`.
package inits
import (
+ "github.com/bwmarrin/discordgo"
"github.com/robfig/cron/v3"
"github.com/sarulabs/di/v2"
"github.com/sirupsen/logrus"
@@ -14,6 +15,7 @@ import (
"github.com/zekroTJA/shinpuru/internal/services/verification"
"github.com/zekroTJA/shinpuru/internal/util/antiraid"
"github.com/zekroTJA/shinpuru/internal/util/static"
+ "github.com/zekroTJA/shinpuru/pkg/discordutil"
"github.com/zekroTJA/shinpuru/pkg/twitchnotify"
)
@@ -26,11 +28,17 @@ func InitLTCTimer(container di.Container) lctimer.LifeCycleTimer {
gl := container.Get(static.DiGuildLog).(guildlog.Logger)
vs := container.Get(static.DiVerification).(verification.Provider)
bd := container.Get(static.DiBirthday).(*birthday.BirthdayService)
+ s := container.Get(static.DiDiscordSession).(*discordgo.Session)
+
+ shardID, shardTotal := discordutil.GetShardOfSession(s)
lct := &lctimer.CronLifeCycleTimer{C: cron.New(cron.WithSeconds())}
lctSchedule(lct, "refresh token cleanup",
func() string {
+ if shardTotal > 1 && shardID != 0 {
+ return ""
+ }
return cfg.Config().Schedules.RefreshTokenCleanup
},
func() {
@@ -52,6 +60,9 @@ func InitLTCTimer(container di.Container) lctimer.LifeCycleTimer {
lctSchedule(lct, "twitch notify",
func() string {
+ if shardTotal > 1 && shardID != 0 {
+ return ""
+ }
return "@every 60s"
},
func() {
@@ -65,6 +76,9 @@ func InitLTCTimer(container di.Container) lctimer.LifeCycleTimer {
lctSchedule(lct, "report expiration",
func() string {
+ if shardTotal > 1 && shardID != 0 {
+ return ""
+ }
return cfg.Config().Schedules.ReportsExpiration
},
func() {
@@ -82,11 +96,17 @@ func InitLTCTimer(container di.Container) lctimer.LifeCycleTimer {
lctSchedule(lct, "verification kick routine",
func() string {
+ if shardTotal > 1 && shardID != 0 {
+ return ""
+ }
return cfg.Config().Schedules.VerificationKick
}, vs.KickRoutine)
lctSchedule(lct, "antiraid joinlog flush",
func() string {
+ if shardTotal > 1 && shardID != 0 {
+ return ""
+ }
return "@every 1h"
}, antiraid.FlushExpired(db, gl))
@@ -102,6 +122,9 @@ func InitLTCTimer(container di.Container) lctimer.LifeCycleTimer {
func lctSchedule(lct lctimer.LifeCycleTimer, name string, specGetter func() string, job func()) {
spec := specGetter()
+ if spec == "" {
+ return
+ }
_, err := lct.Schedule(spec, job)
if err != nil {
logrus.WithError(err).WithField("name", name).Fatalf("LCT :: failed scheduling job")
