Skip to content

Commit 06b3545

Browse files
feat: vacuum database with cron
1 parent 4888a2a commit 06b3545

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

cmd/smtpbridge/main.go

+10
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ func run(flags *flag.FlagSet) lieut.Executor {
144144
}
145145
}
146146

147+
{
148+
job := cron.NewDatabaseVacuum(app)
149+
trigger := quartz.NewSimpleTrigger(24 * time.Hour)
150+
if err := scheduler.ScheduleJob(ctx, job, trigger); err != nil {
151+
return err
152+
}
153+
154+
job.Execute(ctx)
155+
}
156+
147157
return nil
148158
}))
149159

cron/cron.go

+27
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,30 @@ func (r Healthcheck) Execute(ctx context.Context) {
9595
func (r Healthcheck) Key() int {
9696
return quartz.HashCode(r.Description())
9797
}
98+
99+
// DatabaseVacuum
100+
type DatabaseVacuum struct {
101+
app core.App
102+
}
103+
104+
func NewDatabaseVacuum(app core.App) DatabaseVacuum {
105+
return DatabaseVacuum{
106+
app: app,
107+
}
108+
}
109+
110+
func (DatabaseVacuum) Description() string {
111+
return "database-vacuum"
112+
}
113+
114+
func (r DatabaseVacuum) Execute(ctx context.Context) {
115+
err := r.app.DatabaseVacuum(ctx)
116+
if err != nil {
117+
log.Err(err).Msg("Failed to vacuum database")
118+
return
119+
}
120+
}
121+
122+
func (r DatabaseVacuum) Key() int {
123+
return quartz.HashCode(r.Description())
124+
}

internal/app/app.go

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ type App struct {
3838
webTestFileStore WebTestFileStore
3939
}
4040

41+
// DatabaseVacuum implements core.App.
42+
func (a App) DatabaseVacuum(ctx context.Context) error {
43+
return repo.Vacuum(ctx, a.db)
44+
}
45+
4146
func (a App) RuleEndpointsGet(ctx context.Context, id int64) (models.RuleEndpoints, error) {
4247
return repo.RuleEndpointsGet(ctx, a.db, id)
4348
}

internal/core/app.go

+1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@ type App interface {
5151
TraceDrop(ctx context.Context) error
5252
TraceList(ctx context.Context, page pagination.Page, req models.DTOTraceListRequest) (models.DTOTraceListResult, error)
5353
Tracer(source string) trace.Tracer
54+
DatabaseVacuum(ctx context.Context) error
5455
}

0 commit comments

Comments
 (0)