Skip to content

Commit

Permalink
[devices] add clean method
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Nov 9, 2024
1 parent ca9b09e commit 87e95ab
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
6 changes: 6 additions & 0 deletions internal/config/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}),
)
7 changes: 7 additions & 0 deletions internal/sms-gateway/modules/devices/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package devices

import "time"

type Config struct {
UnusedLifetime time.Duration
}
18 changes: 15 additions & 3 deletions internal/sms-gateway/modules/devices/module.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
}
}),
)
10 changes: 10 additions & 0 deletions internal/sms-gateway/modules/devices/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package devices

import (
"context"
"errors"
"time"

Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions internal/sms-gateway/modules/devices/service.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -10,6 +13,8 @@ import (
type ServiceParams struct {
fx.In

Config Config

Devices *repository

IDGen db.IDGen
Expand All @@ -18,6 +23,8 @@ type ServiceParams struct {
}

type Service struct {
config Config

devices *repository

idGen db.IDGen
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 87e95ab

Please sign in to comment.