Skip to content

Commit

Permalink
Add manual cleanup trigger and more logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jun 2, 2024
1 parent e0ae2f5 commit c68a4ed
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 117 deletions.
2 changes: 2 additions & 0 deletions frontend/src/api/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export const apiGetDemos = async () => {
const resp = await apiCall<DemoFile[]>('/api/demos', 'POST', undefined);
return resp.map(transformCreatedOnDate);
};

export const apiGetDemoCleanup = async () => await apiCall('/api/demos/cleanup');
28 changes: 26 additions & 2 deletions frontend/src/routes/_admin.admin.settings.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PropsWithChildren, ReactNode, useState } from 'react';
import AddModeratorIcon from '@mui/icons-material/AddModerator';
import BugReportIcon from '@mui/icons-material/BugReport';
import CleaningServicesIcon from '@mui/icons-material/CleaningServices';
import DeveloperBoardIcon from '@mui/icons-material/DeveloperBoard';
import EmergencyRecordingIcon from '@mui/icons-material/EmergencyRecording';
import GradingIcon from '@mui/icons-material/Grading';
Expand All @@ -19,10 +20,11 @@ import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import Grid from '@mui/material/Unstable_Grid2';
import { useForm } from '@tanstack/react-form';
import { useMutation } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { createFileRoute, useNavigate } from '@tanstack/react-router';
import { zodValidator } from '@tanstack/zod-form-adapter';
import { z } from 'zod';
import { apiGetDemoCleanup } from '../api';
import { apiGetSettings, apiSaveSettings, Config } from '../api/admin.ts';
import { ContainerWithHeaderAndButtons } from '../component/ContainerWithHeaderAndButtons.tsx';
import { Title } from '../component/Title';
Expand Down Expand Up @@ -533,6 +535,9 @@ const FiltersSection = ({ tab, settings, mutate }: { tab: tabs; settings: Config
};

const DemosSection = ({ tab, settings, mutate }: { tab: tabs; settings: Config; mutate: (s: Config) => void }) => {
const queryClient = useQueryClient();
const { sendFlash } = useUserFlashCtx();

const { Field, Subscribe, handleSubmit, reset } = useForm({
onSubmit: async ({ value }) => {
mutate({ ...settings, demo: value });
Expand All @@ -547,6 +552,15 @@ const DemosSection = ({ tab, settings, mutate }: { tab: tabs; settings: Config;
}
});

const onCleanup = async () => {
try {
await queryClient.fetchQuery({ queryKey: ['demoCleanup'], queryFn: apiGetDemoCleanup });
sendFlash('success', 'Cleanup started');
} catch (e) {
sendFlash('error', 'Cleanup failed to start');
}
};

return (
<TabSection
tab={'demo'}
Expand All @@ -562,14 +576,24 @@ const DemosSection = ({ tab, settings, mutate }: { tab: tabs; settings: Config;
}}
>
<Grid container spacing={2}>
<Grid xs={12}>
<Button
startIcon={<CleaningServicesIcon />}
variant={'contained'}
color={'secondary'}
onClick={onCleanup}
>
Start Cleanup
</Button>
</Grid>
<Grid xs={12}>
<Field
name={'demo_cleanup_enabled'}
validators={{
onChange: z.boolean()
}}
children={(props) => {
return <CheckboxSimple {...props} label={'Enable Demo Cleanup'} />;
return <CheckboxSimple {...props} label={'Enable Scheduled Demo Cleanup'} />;
}}
/>
<SubHeading>
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ export const createThemeByMode = (mode: PaletteMode) => {
styleOverrides: {
body: darkScrollbar()
}
},
MuiButton: {
variants: [
{
props: { variant: 'contained' },
style: readableFonts
}
]
}
// MuiButton: {
// variants: [
// {
// props: { variant: 'contained' },
// style: tf2Fonts
// }
// ]
// }
},
typography: {
fontFamily: ['"Helvetica Neue"', 'Helvetica', 'Roboto', 'Arial', 'sans-serif'].join(','),
Expand Down
7 changes: 3 additions & 4 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 4 additions & 19 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions internal/asset/asset_repository_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,28 @@ func (l localRepository) Put(ctx context.Context, asset domain.Asset, body io.Re
return asset, nil
}

func (l localRepository) Delete(ctx context.Context, assetID uuid.UUID) error {
func (l localRepository) Delete(ctx context.Context, assetID uuid.UUID) (int64, error) {
asset, errAsset := l.getAssetByUUID(ctx, assetID)
if errAsset != nil {
return errAsset
return 0, errAsset
}

query := l.db.Builder().Delete("asset").Where(sq.Eq{"asset_id": assetID})

if errExec := l.db.ExecDeleteBuilder(ctx, query); errExec != nil {
return l.db.DBErr(errExec)
return 0, l.db.DBErr(errExec)
}

assetPath, errAssetPath := l.genAssetPath(asset.HashString())
if errAssetPath != nil {
return errAssetPath
return 0, errAssetPath
}

if errRemove := os.Remove(assetPath); errRemove != nil {
return errors.Join(errRemove, domain.ErrDeleteAssetFile)
return 0, errors.Join(errRemove, domain.ErrDeleteAssetFile)
}

return nil
return asset.Size, nil
}

func (l localRepository) Init(_ context.Context) error {
Expand Down
18 changes: 13 additions & 5 deletions internal/asset/asset_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"strings"
"time"

"github.com/dustin/go-humanize"
"github.com/gabriel-vasile/mimetype"
"github.com/gofrs/uuid/v5"
"github.com/leighmacdonald/gbans/internal/domain"
Expand Down Expand Up @@ -47,6 +49,9 @@ func (s assetUsecase) Create(ctx context.Context, author steamid.SteamID, bucket
return domain.Asset{}, errPut
}

slog.Debug("Created new asset",
slog.String("name", asset.Name), slog.String("asset_id", asset.AssetID.String()))

return newAsset, nil
}

Expand All @@ -63,16 +68,19 @@ func (s assetUsecase) Get(ctx context.Context, uuid uuid.UUID) (domain.Asset, io
return asset, reader, nil
}

func (s assetUsecase) Delete(ctx context.Context, assetID uuid.UUID) error {
func (s assetUsecase) Delete(ctx context.Context, assetID uuid.UUID) (int64, error) {
if assetID.IsNil() {
return domain.ErrUUIDInvalid
return 0, domain.ErrUUIDInvalid
}

if err := s.assetRepository.Delete(ctx, assetID); err != nil {
return err
size, err := s.assetRepository.Delete(ctx, assetID)
if err != nil {
return 0, err
}

return nil
slog.Debug("Removed demo asset", slog.String("asset_id", assetID.String()), slog.String("size", humanize.Bytes(uint64(size))))

return size, nil
}

func generateFileHash(file io.Reader) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/demo/demo_respository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ 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.NotEq{"d.archive": true}).
OrderBy("d.created_on asc").
Where(sq.Eq{"d.archive": false}).
OrderBy("d.demo_id DESC").
Offset(limit))
if errRow != nil {
return nil, r.db.DBErr(errRow)
Expand Down
10 changes: 10 additions & 0 deletions internal/demo/demo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ func NewDemoHandler(engine *gin.Engine, du domain.DemoUsecase) {
}

engine.POST("/api/demos", handler.onAPIPostDemosQuery())

engine.GET("/api/demos/cleanup", handler.onAPIGetCleanup())
}

func (h demoHandler) onAPIGetCleanup() gin.HandlerFunc {
return func(ctx *gin.Context) {
h.du.TriggerCleanup()

ctx.JSON(http.StatusOK, gin.H{})
}
}

func (h demoHandler) onAPIPostDemosQuery() gin.HandlerFunc {
Expand Down
Loading

0 comments on commit c68a4ed

Please sign in to comment.