Skip to content

Commit

Permalink
feat: run cleanup task periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
rapatao committed Nov 13, 2024
1 parent a471c09 commit fb954ef
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 44 deletions.
13 changes: 12 additions & 1 deletion internal/repository/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ var (
cleanupSQL string
)

func (s *Service) Cleanup(threshold time.Time) error {
func (s *Service) startCleaner() {
ticker := time.NewTicker(s.config.History)

for threshold := range ticker.C {
err := s.cleanup(threshold)
if err != nil {
log.Error().Err(err).Msg("failed to clean history")
}
}
}

func (s *Service) cleanup(threshold time.Time) error {
exec, err := s.conn.Exec(cleanupSQL, threshold)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions internal/repository/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (s *Service) Initialize(container *injector.Container) error {
return err
}

go s.startCleaner()

return nil
}

Expand Down
11 changes: 11 additions & 0 deletions internal/repository/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package repository

import (
"database/sql"
"sendgrid-mock/internal/config"
)

type Service struct {
config config.Config
conn *sql.DB
}
7 changes: 0 additions & 7 deletions internal/repository/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package repository

import (
"context"
"database/sql"
_ "embed"
"encoding/json"
_ "github.com/mattn/go-sqlite3"
"sendgrid-mock/internal/config"
"sendgrid-mock/internal/model"
)

Expand All @@ -18,11 +16,6 @@ var (
insertSQL string
)

type Service struct {
config config.Config
conn *sql.DB
}

func (s *Service) Save(ctx context.Context, message *model.Message) error {
customArgs, err := json.Marshal(message.CustomArgs)
if err != nil {
Expand Down
27 changes: 0 additions & 27 deletions internal/sendgrid/cleaner.go

This file was deleted.

3 changes: 0 additions & 3 deletions internal/sendgrid/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ func (s *Service) Initialize(container *injector.Container) error {

s.event = &event

s.cleaner = make(chan bool, 1)
go s.startCleaner()

return nil
}

Expand Down
7 changes: 3 additions & 4 deletions internal/sendgrid/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ var (
)

type Service struct {
config *config.Config
repo *repository.Service
event *eventsender.Service
cleaner chan bool
config *config.Config
repo *repository.Service
event *eventsender.Service
}

func (s *Service) Routes() []restrouters.Route {
Expand Down
2 changes: 0 additions & 2 deletions internal/sendgrid/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,5 @@ func (s *Service) persist(ctx context.Context, body []byte) (string, error) {
}
}

s.triggerCleaner()

return messageID, err
}

0 comments on commit fb954ef

Please sign in to comment.