Skip to content

Commit e079108

Browse files
committed
chore: added debug and info logs in workers
1 parent 44eda15 commit e079108

File tree

7 files changed

+80
-12
lines changed

7 files changed

+80
-12
lines changed

core/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ func (c *Context) RemovePeerIfExists(ctx context.Context, id string) error {
2323
return fmt.Errorf("removing peer %q from service: %w", id, err)
2424
}
2525

26-
log.Info("Peer has been removed from service", "id", id)
26+
log.Info("Peer has been removed from service", "peer_id", id)
2727
return nil
2828
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/cosmos/cosmos-sdk v0.47.17
88
github.com/gin-contrib/cors v1.7.6
99
github.com/gin-gonic/gin v1.10.1
10-
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc.6.0.20250823130500-cadee865b3b9
10+
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc.6.0.20250823134443-82024aa6258c
1111
github.com/sentinel-official/sentinelhub/v12 v12.0.0-rc.11
1212
github.com/spf13/cobra v1.9.1
1313
github.com/spf13/pflag v1.0.7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ github.com/secure-io/siv-go v0.0.0-20180922214919-5ff40651e2c4 h1:zOjq+1/uLzn/Xo
511511
github.com/secure-io/siv-go v0.0.0-20180922214919-5ff40651e2c4/go.mod h1:aI+8yClBW+1uovkHw6HM01YXnYB8vohtB9C83wzx34E=
512512
github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb h1:XfLJSPIOUX+osiMraVgIrMR27uMXnRJWGm1+GL8/63U=
513513
github.com/seiflotfy/cuckoofilter v0.0.0-20220411075957-e3b120b3f5fb/go.mod h1:bR6DqgcAl1zTcOX8/pE2Qkj9XO00eCNqmKb7lXP8EAg=
514-
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc.6.0.20250823130500-cadee865b3b9 h1:lk44o2OQ2Y8dZkucsNe/0HWcsxkxLco27Xq4jZU408I=
515-
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc.6.0.20250823130500-cadee865b3b9/go.mod h1:jatyUZIc/7I7xmLI6fmbhzMjJ+si04XR2f0CksWtiXw=
514+
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc.6.0.20250823134443-82024aa6258c h1:x24atM/uRoF+1e5XeArHF8RVG8gBJssQRdoLphc4zcc=
515+
github.com/sentinel-official/sentinel-go-sdk v1.0.0-rc.6.0.20250823134443-82024aa6258c/go.mod h1:jatyUZIc/7I7xmLI6fmbhzMjJ+si04XR2f0CksWtiXw=
516516
github.com/sentinel-official/sentinelhub/v12 v12.0.0-rc.11 h1:B3ylnKZSLP9aDYnS2f98Gx/QTohPuD36tjl75kYJSMs=
517517
github.com/sentinel-official/sentinelhub/v12 v12.0.0-rc.11/go.mod h1:LXcOAEffAn4Pw58xhoYN9j8ZVuM02OCgPNTI6lkGG2E=
518518
github.com/shirou/gopsutil/v4 v4.25.7 h1:bNb2JuqKuAu3tRlPv5piSmBZyMfecwQ+t/ILq+1JqVM=

workers/geoip.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/sentinel-official/sentinel-go-sdk/libs/cron"
9+
logger "github.com/sentinel-official/sentinel-go-sdk/libs/log"
910

1011
"github.com/sentinel-official/sentinel-dvpnx/core"
1112
)
@@ -15,19 +16,22 @@ const NameGeoIPLocation = "geoip_location"
1516
// NewGeoIPLocationWorker creates a worker to periodically update the GeoIP location in the context.
1617
// This worker fetches the GeoIP location and updates the context at regular intervals.
1718
func NewGeoIPLocationWorker(c *core.Context, interval time.Duration) cron.Worker {
19+
log := logger.With("module", "workers", "name", NameGeoIPLocation)
20+
1821
// Handler function that fetches the GeoIP location and updates the context.
1922
handlerFunc := func(ctx context.Context) error {
2023
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
2124
defer cancel()
2225

2326
// Fetch the GeoIP location using the GeoIP client.
24-
location, err := c.GeoIPClient().Get(ctx, "")
27+
loc, err := c.GeoIPClient().Get(ctx, "")
2528
if err != nil {
2629
return fmt.Errorf("getting GeoIP location: %w", err)
2730
}
2831

2932
// Update the context with the fetched location.
30-
c.SetLocation(location)
33+
log.Debug("Updating context", "city", loc.City, "country", loc.Country)
34+
c.SetLocation(loc)
3135

3236
return nil
3337
}

workers/rpc.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/sentinel-official/sentinel-go-sdk/libs/cron"
12+
logger "github.com/sentinel-official/sentinel-go-sdk/libs/log"
1213

1314
"github.com/sentinel-official/sentinel-dvpnx/core"
1415
)
@@ -20,6 +21,7 @@ const NameBestRPCAddr = "best_rpc_addr"
2021
// sorts them in ascending order of latency, and updates the context.
2122
func NewBestRPCAddrWorker(c *core.Context, interval time.Duration) cron.Worker {
2223
client := &http.Client{Timeout: 5 * time.Second}
24+
log := logger.With("module", "workers", "name", NameBestRPCAddr)
2325

2426
// Handler function that measures RPC address latencies and updates the context.
2527
handlerFunc := func(ctx context.Context) error {
@@ -84,7 +86,7 @@ func NewBestRPCAddrWorker(c *core.Context, interval time.Duration) cron.Worker {
8486
return latencies[addrs[i]] < latencies[addrs[j]]
8587
})
8688

87-
// Update the context with the sorted list of RPC addresses.
89+
log.Debug("Updating context", "addrs", addrs)
8890
c.SetRPCAddrs(addrs)
8991

9092
return nil

workers/session.go

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"cosmossdk.io/math"
99
"github.com/cosmos/cosmos-sdk/types"
1010
"github.com/sentinel-official/sentinel-go-sdk/libs/cron"
11+
logger "github.com/sentinel-official/sentinel-go-sdk/libs/log"
1112
"github.com/sentinel-official/sentinelhub/v12/types/v1"
1213

1314
"github.com/sentinel-official/sentinel-dvpnx/core"
@@ -25,6 +26,8 @@ const (
2526
// This worker retrieves session data from the database, validates it against the blockchain,
2627
// and broadcasts any updates as transactions.
2728
func NewSessionUsageSyncWithBlockchainWorker(c *core.Context, interval time.Duration) cron.Worker {
29+
log := logger.With("module", "workers", "name", NameSessionUsageSyncWithBlockchain)
30+
2831
handlerFunc := func(ctx context.Context) error {
2932
// Retrieve session records from the database.
3033
query := map[string]interface{}{
@@ -44,14 +47,25 @@ func NewSessionUsageSyncWithBlockchainWorker(c *core.Context, interval time.Dura
4447
return fmt.Errorf("querying session %d from blockchain: %w", item.GetID(), err)
4548
}
4649
if session == nil {
50+
log.Debug("Skipping session",
51+
"id", item.GetID(), "peer_id", item.GetPeerID(), "cause", "nil session",
52+
)
4753
continue
4854
}
4955
if session.GetUploadBytes().Equal(item.GetRxBytes()) {
56+
log.Debug("Skipping session",
57+
"id", item.GetID(), "peer_id", item.GetPeerID(), "cause", "already up-to-date",
58+
)
5059
continue
5160
}
5261

5362
// Generate an update message for the session.
5463
msg := item.MsgUpdateSessionRequest()
64+
log.Debug("Adding session to update list",
65+
"id", item.GetID(), "peer_id", item.GetPeerID(), "download_bytes", msg.DownloadBytes,
66+
"duration", msg.Duration, "upload_bytes", msg.UploadBytes,
67+
)
68+
5569
msgs = append(msgs, msg)
5670
}
5771

@@ -73,6 +87,8 @@ func NewSessionUsageSyncWithBlockchainWorker(c *core.Context, interval time.Dura
7387
// NewSessionUsageSyncWithDatabaseWorker creates a worker that updates session usage in the database.
7488
// This worker fetches usage data from the peer service and updates the corresponding database records.
7589
func NewSessionUsageSyncWithDatabaseWorker(c *core.Context, interval time.Duration) cron.Worker {
90+
log := logger.With("module", "workers", "name", NameSessionUsageSyncWithDatabase)
91+
7692
handlerFunc := func(ctx context.Context) error {
7793
// Fetch peer usage statistics from the service.
7894
items, err := c.Service().PeerStatistics()
@@ -81,14 +97,22 @@ func NewSessionUsageSyncWithDatabaseWorker(c *core.Context, interval time.Durati
8197
}
8298

8399
// Update the database with the fetched statistics.
84-
for id, item := range items {
100+
for peerID, item := range items {
101+
if time.Since(item.UpdatedAt) > interval {
102+
log.Debug("Skipping session",
103+
"id", 0, "peer_id", peerID, "cause", "already up-to-date",
104+
"updated_at", item.UpdatedAt,
105+
)
106+
continue
107+
}
108+
85109
// Convert usage statistics to strings for database storage.
86110
rxBytes := math.NewInt(item.RxBytes).String()
87111
txBytes := math.NewInt(item.TxBytes).String()
88112

89113
// Define query to find the session by peer id.
90114
query := map[string]interface{}{
91-
"peer_id": id,
115+
"peer_id": peerID,
92116
}
93117

94118
// Define updates to apply to the session record.
@@ -97,9 +121,11 @@ func NewSessionUsageSyncWithDatabaseWorker(c *core.Context, interval time.Durati
97121
"tx_bytes": txBytes,
98122
}
99123

100-
// Update the session in the database.
124+
log.Debug("Updating session in database",
125+
"id", 0, "peer_id", peerID, "rx_bytes", rxBytes, "tx_bytes", txBytes,
126+
)
101127
if _, err := operations.SessionFindOneAndUpdate(c.Database(), query, updates); err != nil {
102-
return fmt.Errorf("updating session for peer %q in database: %w", id, err)
128+
return fmt.Errorf("updating session for peer %q in database: %w", peerID, err)
103129
}
104130
}
105131

@@ -115,6 +141,8 @@ func NewSessionUsageSyncWithDatabaseWorker(c *core.Context, interval time.Durati
115141
// NewSessionUsageValidateWorker creates a worker that validates session usage limits and removes peers if necessary.
116142
// This worker checks if sessions exceed their maximum byte or duration limits and removes peers accordingly.
117143
func NewSessionUsageValidateWorker(c *core.Context, interval time.Duration) cron.Worker {
144+
log := logger.With("module", "workers", "name", NameSessionUsageValidate)
145+
118146
handlerFunc := func(ctx context.Context) error {
119147
// Retrieve session records from the database.
120148
query := map[string]interface{}{
@@ -133,22 +161,35 @@ func NewSessionUsageValidateWorker(c *core.Context, interval time.Duration) cron
133161
// Check if the session exceeds the maximum allowed bytes.
134162
maxBytes := item.GetMaxBytes()
135163
if !maxBytes.IsZero() && item.GetTotalBytes().GTE(maxBytes) {
164+
log.Debug("Marking peer for removing from service",
165+
"id", item.GetID(), "peer_id", item.GetPeerID(), "cause", "exceeds max bytes",
166+
"total_bytes", item.GetTotalBytes(), "max_bytes", item.GetMaxBytes(),
167+
)
136168
removePeer = true
137169
}
138170

139171
// Check if the session exceeds the maximum allowed duration.
140172
maxDuration := item.GetMaxDuration()
141173
if maxDuration != 0 && item.GetDuration() >= maxDuration {
174+
log.Debug("Marking peer for removing from service",
175+
"id", item.GetID(), "peer_id", item.GetPeerID(), "cause", "exceeds max duration",
176+
"duration", item.GetDuration(), "max_duration", maxDuration,
177+
)
142178
removePeer = true
143179
}
144180

145181
// Ensure that only sessions of the current service type are validated.
146182
if item.GetServiceType() != c.Service().Type() {
183+
log.Debug("Skipping peer",
184+
"id", item.GetID(), "peer_id", item.GetPeerID(), "cause", "invalid service type",
185+
"got", item.GetServiceType(), "expected", c.Service().Type(),
186+
)
147187
removePeer = false
148188
}
149189

150190
// If the session exceeded any limits, remove the associated peer.
151191
if removePeer {
192+
log.Info("Removing peer from service", "id", item.GetID(), "peer_id", item.GetPeerID())
152193
if err := c.RemovePeerIfExists(ctx, item.GetPeerID()); err != nil {
153194
return fmt.Errorf("removing peer %q for session %d from service: %w", item.GetPeerID(), item.GetID(), err)
154195
}
@@ -167,6 +208,8 @@ func NewSessionUsageValidateWorker(c *core.Context, interval time.Duration) cron
167208
// NewSessionValidateWorker creates a worker that validates session status and removes peers if necessary.
168209
// This worker ensures sessions are active and consistent between the database and blockchain.
169210
func NewSessionValidateWorker(c *core.Context, interval time.Duration) cron.Worker {
211+
log := logger.With("module", "workers", "name", NameSessionValidate)
212+
170213
handlerFunc := func(ctx context.Context) error {
171214
// Retrieve session records from the database.
172215
query := map[string]interface{}{
@@ -189,20 +232,32 @@ func NewSessionValidateWorker(c *core.Context, interval time.Duration) cron.Work
189232

190233
// Remove peer if the session is missing on the blockchain.
191234
if session == nil {
235+
log.Debug("Marking peer for removing from service",
236+
"id", item.GetID(), "peer_id", item.GetPeerID(), "cause", "nil session",
237+
)
192238
removePeer = true
193239
}
194240
// Remove peer if the session status is not active.
195241
if session != nil && !session.GetStatus().Equal(v1.StatusActive) {
242+
log.Debug("Marking peer for removing from service",
243+
"id", item.GetID(), "peer_id", item.GetPeerID(), "cause", "invalid session status",
244+
"got", session.GetStatus(), "expected", v1.StatusActive,
245+
)
196246
removePeer = true
197247
}
198248

199249
// Ensure that only sessions of the current service type are validated.
200250
if item.GetServiceType() != c.Service().Type() {
251+
log.Debug("Skipping peer",
252+
"id", item.GetID(), "peer_id", item.GetPeerID(), "cause", "invalid service type",
253+
"got", item.GetServiceType(), "expected", c.Service().Type(),
254+
)
201255
removePeer = false
202256
}
203257

204258
// Remove the associated peer if validation fails.
205259
if removePeer {
260+
log.Info("Removing peer from service", "id", item.GetID(), "peer_id", item.GetPeerID())
206261
if err := c.RemovePeerIfExists(ctx, item.GetPeerID()); err != nil {
207262
return fmt.Errorf("removing peer %q for session %d from service: %w", item.GetPeerID(), item.GetID(), err)
208263
}
@@ -212,6 +267,9 @@ func NewSessionValidateWorker(c *core.Context, interval time.Duration) cron.Work
212267

213268
// Delete session if the session is missing on the blockchain.
214269
if session == nil {
270+
log.Debug("Marking session for deleting from database",
271+
"id", item.GetID(), "peer_id", item.GetPeerID(), "cause", "nil session",
272+
)
215273
deleteSession = true
216274
}
217275

@@ -221,6 +279,7 @@ func NewSessionValidateWorker(c *core.Context, interval time.Duration) cron.Work
221279
"id": item.GetID(),
222280
}
223281

282+
log.Info("Deleting session from database", "id", item.GetID(), "peer_id", item.GetPeerID())
224283
if _, err := operations.SessionFindOneAndDelete(c.Database(), query); err != nil {
225284
return fmt.Errorf("deleting session %d from database: %w", item.GetID(), err)
226285
}

workers/speedtest.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/sentinel-official/sentinel-go-sdk/libs/cron"
9+
logger "github.com/sentinel-official/sentinel-go-sdk/libs/log"
910
"github.com/sentinel-official/sentinel-go-sdk/libs/speedtest"
1011

1112
"github.com/sentinel-official/sentinel-dvpnx/core"
@@ -16,6 +17,8 @@ const NameSpeedtest = "speedtest"
1617
// NewSpeedtestWorker creates a worker that performs periodic speed tests and updates the context with the results.
1718
// This worker measures the download and upload speeds and sets the results in the application's context.
1819
func NewSpeedtestWorker(c *core.Context, interval time.Duration) cron.Worker {
20+
log := logger.With("module", "workers", "name", NameSpeedtest)
21+
1922
// Handler function that performs the speed test and updates the context.
2023
handlerFunc := func(ctx context.Context) error {
2124
// Run the speed test to measure download and upload speeds.
@@ -24,7 +27,7 @@ func NewSpeedtestWorker(c *core.Context, interval time.Duration) cron.Worker {
2427
return fmt.Errorf("running speed test: %w", err)
2528
}
2629

27-
// Update the context with the obtained speed test results.
30+
log.Debug("Updating context", "dl_speed", dlSpeed, "ul_speed", ulSpeed)
2831
c.SetSpeedtestResults(dlSpeed, ulSpeed)
2932

3033
return nil

0 commit comments

Comments
 (0)