Skip to content

Commit

Permalink
fix: tweak dbsync timestamp type
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Trost <[email protected]>
  • Loading branch information
galexrt committed Jan 18, 2025
1 parent 54231a3 commit 3bf9c24
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
11 changes: 6 additions & 5 deletions pkg/dbsync/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
17 changes: 16 additions & 1 deletion pkg/dbsync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/dbsync/sync_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/dbsync/sync_vehicles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/discord/commands/fivenet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func init() {
CommandsFactories["fivenet"] = NewFivenetCommand
}

type FiveNetCommand struct {
type FivenetCommand struct {
l *lang.I18n

url string
Expand All @@ -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,
})
Expand All @@ -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{
Expand Down
54 changes: 54 additions & 0 deletions pkg/discord/commands/sync.go
Original file line number Diff line number Diff line change
@@ -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{}
}

0 comments on commit 3bf9c24

Please sign in to comment.