From 3bf9c249f6f96610e626f4feae08114ac182dc58 Mon Sep 17 00:00:00 2001 From: Alexander Trost Date: Sat, 18 Jan 2025 21:24:28 +0100 Subject: [PATCH] fix: tweak dbsync timestamp type Signed-off-by: Alexander Trost --- pkg/dbsync/state.go | 11 ++++--- pkg/dbsync/sync.go | 17 ++++++++++- pkg/dbsync/sync_users.go | 3 +- pkg/dbsync/sync_vehicles.go | 3 +- pkg/discord/commands/fivenet.go | 6 ++-- pkg/discord/commands/sync.go | 54 +++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 pkg/discord/commands/sync.go diff --git a/pkg/dbsync/state.go b/pkg/dbsync/state.go index 8b3dad801..71cde352a 100644 --- a/pkg/dbsync/state.go +++ b/pkg/dbsync/state.go @@ -28,10 +28,10 @@ type DBSyncState struct { type TableSyncState struct { dss *DBSyncState - LastCheck time.Time `yaml:"lastCheck"` - Offset uint64 `yaml:"offset"` - LastID *string `yaml:"lastId"` - SyncedUp bool `yaml:"syncedUp"` + LastCheck *time.Time `yaml:"lastCheck"` + Offset uint64 `yaml:"offset"` + LastID *string `yaml:"lastId"` + SyncedUp bool `yaml:"syncedUp"` } func NewDBSyncState(logger *zap.Logger, filepath string) *DBSyncState { @@ -95,7 +95,8 @@ func (s *DBSyncState) Save() error { } func (s *TableSyncState) Set(offset uint64, lastId *string) { - s.LastCheck = time.Now() + now := time.Now() + s.LastCheck = &now s.Offset = offset s.LastID = lastId diff --git a/pkg/dbsync/sync.go b/pkg/dbsync/sync.go index 4c694ed15..099269efb 100644 --- a/pkg/dbsync/sync.go +++ b/pkg/dbsync/sync.go @@ -214,8 +214,23 @@ func (s *Sync) run(ctx context.Context) error { s.logger.Error("error during users sync", zap.Error(err)) } + select { + case <-ctx.Done(): + return + + case <-time.After(2 * time.Second): + } + } + }() + + // Owned Vehicles data sync loop + s.wg.Add(1) + go func() { + defer s.wg.Done() + + for { if err := s.vehicles.Sync(ctx); err != nil { - s.logger.Error("error during users sync", zap.Error(err)) + s.logger.Error("error during vehicles sync", zap.Error(err)) } select { diff --git a/pkg/dbsync/sync_users.go b/pkg/dbsync/sync_users.go index 15560c3f0..fce2f74e0 100644 --- a/pkg/dbsync/sync_users.go +++ b/pkg/dbsync/sync_users.go @@ -4,7 +4,6 @@ import ( "context" "strconv" "strings" - "time" "github.com/fivenet-app/fivenet/gen/go/proto/resources/sync" "github.com/fivenet-app/fivenet/gen/go/proto/resources/users" @@ -39,7 +38,7 @@ func (s *usersSync) Sync(ctx context.Context) error { // Ensure to zero the last check time if the data hasn't fully synced yet if !s.state.SyncedUp { - s.state.LastCheck = time.Time{} + s.state.LastCheck = nil } sQuery := s.cfg.Tables.Users.DBSyncTable diff --git a/pkg/dbsync/sync_vehicles.go b/pkg/dbsync/sync_vehicles.go index 107814f31..5b0405802 100644 --- a/pkg/dbsync/sync_vehicles.go +++ b/pkg/dbsync/sync_vehicles.go @@ -2,7 +2,6 @@ package dbsync import ( "context" - "time" "github.com/fivenet-app/fivenet/gen/go/proto/resources/sync" "github.com/fivenet-app/fivenet/gen/go/proto/resources/vehicles" @@ -36,7 +35,7 @@ func (s *vehiclesSync) Sync(ctx context.Context) error { // Ensure to zero the last check time if the data hasn't fully synced yet if !s.state.SyncedUp { - s.state.LastCheck = time.Time{} + s.state.LastCheck = nil } sQuery := s.cfg.Tables.Vehicles diff --git a/pkg/discord/commands/fivenet.go b/pkg/discord/commands/fivenet.go index 9e4949560..4d4e32e5e 100644 --- a/pkg/discord/commands/fivenet.go +++ b/pkg/discord/commands/fivenet.go @@ -16,7 +16,7 @@ func init() { CommandsFactories["fivenet"] = NewFivenetCommand } -type FiveNetCommand struct { +type FivenetCommand struct { l *lang.I18n url string @@ -26,7 +26,7 @@ func NewFivenetCommand(router *cmdroute.Router, cfg *config.Config, p CommandPar lEN := p.L.I18n("en") lDE := p.L.I18n("de") - router.Add("fivenet", &FiveNetCommand{ + router.Add("fivenet", &FivenetCommand{ l: p.L, url: cfg.HTTP.PublicURL, }) @@ -47,7 +47,7 @@ func NewFivenetCommand(router *cmdroute.Router, cfg *config.Config, p CommandPar nil } -func (c *FiveNetCommand) HandleCommand(ctx context.Context, cmd cmdroute.CommandData) *api.InteractionResponseData { +func (c *FivenetCommand) HandleCommand(ctx context.Context, cmd cmdroute.CommandData) *api.InteractionResponseData { localizer := c.l.I18n(string(cmd.Event.Locale)) return &api.InteractionResponseData{ diff --git a/pkg/discord/commands/sync.go b/pkg/discord/commands/sync.go new file mode 100644 index 000000000..b90526da7 --- /dev/null +++ b/pkg/discord/commands/sync.go @@ -0,0 +1,54 @@ +package commands + +import ( + "context" + + "github.com/diamondburned/arikawa/v3/api" + "github.com/diamondburned/arikawa/v3/api/cmdroute" + "github.com/diamondburned/arikawa/v3/discord" + "github.com/fivenet-app/fivenet/pkg/config" + "github.com/fivenet-app/fivenet/pkg/lang" + "github.com/nicksnyder/go-i18n/v2/i18n" +) + +func init() { + CommandsFactories["sync"] = NewSyncCommand +} + +type SyncCommand struct { + l *lang.I18n + + url string +} + +func NewSyncCommand(router *cmdroute.Router, cfg *config.Config, p CommandParams) (api.CreateCommandData, error) { + lEN := p.L.I18n("en") + lDE := p.L.I18n("de") + + router.Add("sync", &SyncCommand{ + l: p.L, + url: cfg.HTTP.PublicURL, + }) + + return api.CreateCommandData{ + Type: discord.ChatInputCommand, + Name: "sync", + Description: lEN.MustLocalize(&i18n.LocalizeConfig{ + MessageID: "discord.commands.fivenet.desc", + }), + DescriptionLocalizations: discord.StringLocales{ + discord.German: lDE.MustLocalize(&i18n.LocalizeConfig{ + MessageID: "discord.commands.fivenet.desc", + }), + }, + DefaultMemberPermissions: discord.NewPermissions(discord.PermissionSendMessages), + }, + nil +} + +func (c *SyncCommand) HandleCommand(ctx context.Context, cmd cmdroute.CommandData) *api.InteractionResponseData { + localizer := c.l.I18n(string(cmd.Event.Locale)) + _ = localizer + + return &api.InteractionResponseData{} +}