diff --git a/internal/config/module.go b/internal/config/module.go index bda0215..5d4c6a4 100644 --- a/internal/config/module.go +++ b/internal/config/module.go @@ -8,6 +8,7 @@ import ( "github.com/capcom6/go-infra-fx/http" "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers" "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/auth" + "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/devices" "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/messages" "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/push" "go.uber.org/fx" @@ -79,4 +80,9 @@ var Module = fx.Module( ProcessedLifetime: 30 * 24 * time.Hour, //TODO: make it configurable } }), + fx.Provide(func(cfg Config) devices.Config { + return devices.Config{ + UnusedLifetime: 365 * 24 * time.Hour, //TODO: make it configurable + } + }), ) diff --git a/internal/sms-gateway/modules/devices/config.go b/internal/sms-gateway/modules/devices/config.go new file mode 100644 index 0000000..7495b3e --- /dev/null +++ b/internal/sms-gateway/modules/devices/config.go @@ -0,0 +1,7 @@ +package devices + +import "time" + +type Config struct { + UnusedLifetime time.Duration +} diff --git a/internal/sms-gateway/modules/devices/module.go b/internal/sms-gateway/modules/devices/module.go index e15b3ff..59ac8b2 100644 --- a/internal/sms-gateway/modules/devices/module.go +++ b/internal/sms-gateway/modules/devices/module.go @@ -1,10 +1,18 @@ package devices import ( + "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/cleaner" "go.uber.org/fx" "go.uber.org/zap" ) +type FxResult struct { + fx.Out + + Service *Service + AsCleaner cleaner.Cleanable `group:"cleaners"` +} + var Module = fx.Module( "devices", fx.Decorate(func(log *zap.Logger) *zap.Logger { @@ -14,7 +22,11 @@ var Module = fx.Module( newDevicesRepository, fx.Private, ), - fx.Provide( - NewService, - ), + fx.Provide(func(p ServiceParams) FxResult { + svc := NewService(p) + return FxResult{ + Service: svc, + AsCleaner: svc, + } + }), ) diff --git a/internal/sms-gateway/modules/devices/repository.go b/internal/sms-gateway/modules/devices/repository.go index 55f6b79..cfe826b 100644 --- a/internal/sms-gateway/modules/devices/repository.go +++ b/internal/sms-gateway/modules/devices/repository.go @@ -1,6 +1,7 @@ package devices import ( + "context" "errors" "time" @@ -58,6 +59,15 @@ func (r *repository) UpdateLastSeen(id string) error { return r.db.Model(&models.Device{}).Where("id", id).Update("last_seen", time.Now()).Error } +func (r *repository) removeUnused(ctx context.Context, since time.Time) (int64, error) { + res := r.db. + WithContext(ctx). + Where("updated_at < ?", since). + Delete(&models.Device{}) + + return res.RowsAffected, res.Error +} + func newDevicesRepository(db *gorm.DB) *repository { return &repository{ db: db, diff --git a/internal/sms-gateway/modules/devices/service.go b/internal/sms-gateway/modules/devices/service.go index dceac96..f44de74 100644 --- a/internal/sms-gateway/modules/devices/service.go +++ b/internal/sms-gateway/modules/devices/service.go @@ -1,6 +1,9 @@ package devices import ( + "context" + "time" + "github.com/capcom6/sms-gateway/internal/sms-gateway/models" "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/db" "go.uber.org/fx" @@ -10,6 +13,8 @@ import ( type ServiceParams struct { fx.In + Config Config + Devices *repository IDGen db.IDGen @@ -18,6 +23,8 @@ type ServiceParams struct { } type Service struct { + config Config + devices *repository idGen db.IDGen @@ -49,8 +56,16 @@ func (s *Service) UpdateLastSeen(deviceId string) error { return s.devices.UpdateLastSeen(deviceId) } +func (s *Service) Clean(ctx context.Context) error { + n, err := s.devices.removeUnused(ctx, time.Now().Add(-s.config.UnusedLifetime)) + + s.logger.Info("Cleaned unused devices", zap.Int64("count", n)) + return err +} + func NewService(params ServiceParams) *Service { return &Service{ + config: params.Config, devices: params.Devices, idGen: params.IDGen, logger: params.Logger.Named("service"),