File tree 5 files changed +53
-3
lines changed
sms-gateway/modules/devices
5 files changed +53
-3
lines changed Original file line number Diff line number Diff line change 8
8
"github.com/capcom6/go-infra-fx/http"
9
9
"github.com/capcom6/sms-gateway/internal/sms-gateway/handlers"
10
10
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/auth"
11
+ "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/devices"
11
12
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/messages"
12
13
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/push"
13
14
"go.uber.org/fx"
@@ -79,4 +80,9 @@ var Module = fx.Module(
79
80
ProcessedLifetime : 30 * 24 * time .Hour , //TODO: make it configurable
80
81
}
81
82
}),
83
+ fx .Provide (func (cfg Config ) devices.Config {
84
+ return devices.Config {
85
+ UnusedLifetime : 365 * 24 * time .Hour , //TODO: make it configurable
86
+ }
87
+ }),
82
88
)
Original file line number Diff line number Diff line change
1
+ package devices
2
+
3
+ import "time"
4
+
5
+ type Config struct {
6
+ UnusedLifetime time.Duration
7
+ }
Original file line number Diff line number Diff line change 1
1
package devices
2
2
3
3
import (
4
+ "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/cleaner"
4
5
"go.uber.org/fx"
5
6
"go.uber.org/zap"
6
7
)
7
8
9
+ type FxResult struct {
10
+ fx.Out
11
+
12
+ Service * Service
13
+ AsCleaner cleaner.Cleanable `group:"cleaners"`
14
+ }
15
+
8
16
var Module = fx .Module (
9
17
"devices" ,
10
18
fx .Decorate (func (log * zap.Logger ) * zap.Logger {
@@ -14,7 +22,11 @@ var Module = fx.Module(
14
22
newDevicesRepository ,
15
23
fx .Private ,
16
24
),
17
- fx .Provide (
18
- NewService ,
19
- ),
25
+ fx .Provide (func (p ServiceParams ) FxResult {
26
+ svc := NewService (p )
27
+ return FxResult {
28
+ Service : svc ,
29
+ AsCleaner : svc ,
30
+ }
31
+ }),
20
32
)
Original file line number Diff line number Diff line change 1
1
package devices
2
2
3
3
import (
4
+ "context"
4
5
"errors"
5
6
"time"
6
7
@@ -58,6 +59,15 @@ func (r *repository) UpdateLastSeen(id string) error {
58
59
return r .db .Model (& models.Device {}).Where ("id" , id ).Update ("last_seen" , time .Now ()).Error
59
60
}
60
61
62
+ func (r * repository ) removeUnused (ctx context.Context , since time.Time ) (int64 , error ) {
63
+ res := r .db .
64
+ WithContext (ctx ).
65
+ Where ("updated_at < ?" , since ).
66
+ Delete (& models.Device {})
67
+
68
+ return res .RowsAffected , res .Error
69
+ }
70
+
61
71
func newDevicesRepository (db * gorm.DB ) * repository {
62
72
return & repository {
63
73
db : db ,
Original file line number Diff line number Diff line change 1
1
package devices
2
2
3
3
import (
4
+ "context"
5
+ "time"
6
+
4
7
"github.com/capcom6/sms-gateway/internal/sms-gateway/models"
5
8
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/db"
6
9
"go.uber.org/fx"
@@ -10,6 +13,8 @@ import (
10
13
type ServiceParams struct {
11
14
fx.In
12
15
16
+ Config Config
17
+
13
18
Devices * repository
14
19
15
20
IDGen db.IDGen
@@ -18,6 +23,8 @@ type ServiceParams struct {
18
23
}
19
24
20
25
type Service struct {
26
+ config Config
27
+
21
28
devices * repository
22
29
23
30
idGen db.IDGen
@@ -49,8 +56,16 @@ func (s *Service) UpdateLastSeen(deviceId string) error {
49
56
return s .devices .UpdateLastSeen (deviceId )
50
57
}
51
58
59
+ func (s * Service ) Clean (ctx context.Context ) error {
60
+ n , err := s .devices .removeUnused (ctx , time .Now ().Add (- s .config .UnusedLifetime ))
61
+
62
+ s .logger .Info ("Cleaned unused devices" , zap .Int64 ("count" , n ))
63
+ return err
64
+ }
65
+
52
66
func NewService (params ServiceParams ) * Service {
53
67
return & Service {
68
+ config : params .Config ,
54
69
devices : params .Devices ,
55
70
idGen : params .IDGen ,
56
71
logger : params .Logger .Named ("service" ),
You can’t perform that action at this time.
0 commit comments