Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expired demos query archive status #564

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions internal/demo/demo_respository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func (r *demoRepository) ExpiredDemos(ctx context.Context, limit uint64) ([]doma
Builder().
Select("d.demo_id", "d.title", "d.asset_id").
From("demo d").
Where(sq.Eq{"d.archive": false}).
OrderBy("d.demo_id DESC").
LeftJoin("report r on d.demo_id = r.demo_id").
Where("r.demo_id > 0").
OrderBy("d.demo_id").
Offset(limit))
if errRow != nil {
return nil, r.db.DBErr(errRow)
Expand Down Expand Up @@ -154,28 +155,6 @@ func (r *demoRepository) GetDemos(ctx context.Context) ([]domain.DemoFile, error
}

func (r *demoRepository) SaveDemo(ctx context.Context, demoFile *domain.DemoFile) error {
// Find open reports and if any are returned, mark the demo as archived so that it does not get auto
// deleted during cleanup.
// Reports can happen mid-game which is why this is checked when the demo is saved and not during the report where
// we have no completed demo instance/id yet.
reportRow, reportRowErr := r.db.QueryRowBuilder(ctx, r.db.
Builder().
Select("count(report_id)").
From("report").
Where(sq.Eq{"demo_id": demoFile.DemoID}))
if reportRowErr != nil {
return errors.Join(reportRowErr, domain.ErrReportCountQuery)
}

var count int
if errScan := reportRow.Scan(&count); errScan != nil && !errors.Is(errScan, domain.ErrNoResult) {
return r.db.DBErr(errScan)
}

if count > 0 {
demoFile.Archive = true
}

var err error
if demoFile.DemoID > 0 {
err = r.updateDemo(ctx, demoFile)
Expand Down
4 changes: 2 additions & 2 deletions internal/demo/demo_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewDemoUsecase(bucket domain.Bucket, repository domain.DemoRepository, asse
}
}

func (d demoUsecase) OldestDemo(ctx context.Context) (domain.DemoInfo, error) {
func (d demoUsecase) oldest(ctx context.Context) (domain.DemoInfo, error) {
demos, errDemos := d.repository.ExpiredDemos(ctx, 1)
if errDemos != nil {
return domain.DemoInfo{}, errDemos
Expand Down Expand Up @@ -86,7 +86,7 @@ func (d demoUsecase) truncateBySpace(ctx context.Context, root string, maxAllowe
return count, size, nil
}

oldestDemo, errOldest := d.OldestDemo(ctx)
oldestDemo, errOldest := d.oldest(ctx)
if errOldest != nil {
if errors.Is(errOldest, domain.ErrNoResult) {
return count, size, nil
Expand Down
1 change: 0 additions & 1 deletion internal/domain/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type DemoUsecase interface {
GetDemoByName(ctx context.Context, demoName string, demoFile *DemoFile) error
GetDemos(ctx context.Context) ([]DemoFile, error)
CreateFromAsset(ctx context.Context, asset Asset, serverID int) (*DemoFile, error)
OldestDemo(ctx context.Context) (DemoInfo, error)
TriggerCleanup()
}

Expand Down
Loading