@@ -3,11 +3,11 @@ package checker
33import (
44 "context"
55 "fmt"
6- "log"
76 "time"
87
98 "aproxy/internal/database"
109 "aproxy/internal/database/models/model"
10+ "aproxy/internal/logger"
1111 "aproxy/pkg/scraper"
1212)
1313
@@ -18,6 +18,7 @@ type DBChecker struct {
1818 checkInterval time.Duration
1919 batchSize int
2020 batchDelay time.Duration
21+ logger * logger.Logger
2122}
2223
2324// NewDBChecker creates a new database-backed checker
@@ -28,6 +29,7 @@ func NewDBChecker(dbService *database.Service, checkInterval time.Duration, batc
2829 checkInterval : checkInterval ,
2930 batchSize : batchSize ,
3031 batchDelay : batchDelay ,
32+ logger : logger .New ("db-checker" ),
3133 }
3234}
3335
@@ -39,6 +41,7 @@ func NewDBCheckerWithConfig(dbService *database.Service, checkerConfig CheckerCo
3941 checkInterval : checkInterval ,
4042 batchSize : batchSize ,
4143 batchDelay : batchDelay ,
44+ logger : logger .New ("db-checker" ),
4245 }
4346}
4447
@@ -48,7 +51,7 @@ func (c *DBChecker) CheckProxiesWithCaching(ctx context.Context, proxies []scrap
4851 return nil
4952 }
5053
51- log . Printf ("Checking %d proxies with caching (skip if checked within %v)" , len (proxies ), c .checkInterval )
54+ c . logger . InfoBg ("Checking %d proxies with caching (skip if checked within %v)" , len (proxies ), c .checkInterval )
5255
5356 // Get addresses of all scraped proxies
5457 addresses := make ([]string , len (proxies ))
@@ -62,12 +65,12 @@ func (c *DBChecker) CheckProxiesWithCaching(ctx context.Context, proxies []scrap
6265 // Get existing proxies from database
6366 existingProxies , err := c .dbService .GetProxiesByAddresses (ctx , addresses )
6467 if err != nil {
65- log . Printf ("Failed to get existing proxies: %v" , err )
68+ c . logger . WarnBg ("Failed to get existing proxies: %v" , err )
6669 // Fall back to checking all proxies
6770 return c .Checker .CheckProxies (ctx , proxies )
6871 }
6972
70- log . Printf ("Found %d existing proxies in database" , len (existingProxies ))
73+ c . logger . InfoBg ("Found %d existing proxies in database" , len (existingProxies ))
7174
7275 // Separate new proxies that need to be inserted vs existing ones
7376 var newProxies []scraper.Proxy
@@ -88,7 +91,7 @@ func (c *DBChecker) CheckProxiesWithCaching(ctx context.Context, proxies []scrap
8891 for _ , proxy := range newProxies {
8992 dbProxy , err := c .dbService .UpsertProxy (ctx , proxy )
9093 if err != nil {
91- log . Printf ("Failed to upsert new proxy %s: %v" , proxy .Address (), err )
94+ c . logger . WarnBg ("Failed to upsert new proxy %s: %v" , proxy .Address (), err )
9295 continue
9396 }
9497 dbProxies = append (dbProxies , dbProxy )
@@ -123,7 +126,7 @@ func (c *DBChecker) CheckProxiesWithCaching(ctx context.Context, proxies []scrap
123126 }
124127 }
125128
126- log . Printf ("Found %d proxies that need checking (out of %d total)" , len (proxiesToCheck ), len (dbProxies ))
129+ c . logger . InfoBg ("Found %d proxies that need checking (out of %d total)" , len (proxiesToCheck ), len (dbProxies ))
127130
128131 if len (proxiesToCheck ) == 0 {
129132 // All proxies have been checked recently, return cached results
@@ -146,7 +149,7 @@ func (c *DBChecker) CheckProxiesWithCaching(ctx context.Context, proxies []scrap
146149 proxyAddr := result .Proxy .Address ()
147150 dbProxy , exists := proxyMap [proxyAddr ]
148151 if ! exists {
149- log . Printf ("No database proxy found for %s" , proxyAddr )
152+ c . logger . WarnBg ("No database proxy found for %s" , proxyAddr )
150153 continue
151154 }
152155
@@ -188,9 +191,9 @@ func (c *DBChecker) CheckProxiesWithCaching(ctx context.Context, proxies []scrap
188191 // Use longer timeout for database operations
189192 updateCtx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
190193 if err := c .dbService .BatchUpdateProxyHealth (updateCtx , batchUpdates ); err != nil {
191- log . Printf ("Failed to batch update proxy health (batch %d-%d): %v" , i , end - 1 , err )
194+ c . logger . WarnBg ("Failed to batch update proxy health (batch %d-%d): %v" , i , end - 1 , err )
192195 } else {
193- log . Printf ("Successfully updated %d proxy health records to database" , len (batchUpdates ))
196+ c . logger . InfoBg ("Successfully updated %d proxy health records to database" , len (batchUpdates ))
194197 }
195198 cancel ()
196199 }
@@ -348,14 +351,14 @@ func (c *DBChecker) checkProxiesProgressive(ctx context.Context, proxies []scrap
348351 var allResults []CheckResult
349352 totalBatches := (len (proxies ) + c .batchSize - 1 ) / c .batchSize
350353
351- log . Printf ("Checking %d proxies in %d batches (batch size: %d, delay: %v)" ,
354+ c . logger . InfoBg ("Checking %d proxies in %d batches (batch size: %d, delay: %v)" ,
352355 len (proxies ), totalBatches , c .batchSize , c .batchDelay )
353356
354357 for i := 0 ; i < len (proxies ); i += c .batchSize {
355358 // Check for cancellation before starting each batch
356359 select {
357360 case <- ctx .Done ():
358- log . Printf ("Context cancelled before batch %d/%d, stopping progressive checking" , i / c .batchSize + 1 , totalBatches )
361+ c . logger . WarnBg ("Context cancelled before batch %d/%d, stopping progressive checking" , i / c .batchSize + 1 , totalBatches )
359362 return allResults
360363 default :
361364 // Continue with batch
@@ -369,7 +372,7 @@ func (c *DBChecker) checkProxiesProgressive(ctx context.Context, proxies []scrap
369372 batch := proxies [i :end ]
370373 batchNum := i / c .batchSize + 1
371374
372- log . Printf ("Checking batch %d/%d (%d proxies)" , batchNum , totalBatches , len (batch ))
375+ c . logger . InfoBg ("Checking batch %d/%d (%d proxies)" , batchNum , totalBatches , len (batch ))
373376
374377 // Check batch using original checker
375378 batchResults := c .Checker .CheckProxies (ctx , batch )
@@ -380,7 +383,7 @@ func (c *DBChecker) checkProxiesProgressive(ctx context.Context, proxies []scrap
380383 select {
381384 case <- ctx .Done ():
382385 // Context cancelled, skip background save
383- log . Printf ("Context cancelled, skipping database save for batch %d" , batchNum )
386+ c . logger . WarnBg ("Context cancelled, skipping database save for batch %d" , batchNum )
384387 default :
385388 // Context still active, save in background
386389 go func (results []CheckResult , batchNumber int ) {
@@ -409,7 +412,7 @@ func (c *DBChecker) checkProxiesProgressive(ctx context.Context, proxies []scrap
409412 }
410413 if len (updates ) > 0 {
411414 if err := c .dbService .BatchUpdateProxyHealth (saveCtx , updates ); err == nil {
412- log . Printf ("Saved batch %d results to database (%d records)" , batchNumber , len (updates ))
415+ c . logger . InfoBg ("Saved batch %d results to database (%d records)" , batchNumber , len (updates ))
413416 }
414417 }
415418 }
@@ -424,13 +427,13 @@ func (c *DBChecker) checkProxiesProgressive(ctx context.Context, proxies []scrap
424427 healthyCount ++
425428 }
426429 }
427- log . Printf ("Batch %d/%d complete. Total healthy so far: %d" , batchNum , totalBatches , healthyCount )
430+ c . logger . InfoBg ("Batch %d/%d complete. Total healthy so far: %d" , batchNum , totalBatches , healthyCount )
428431
429432 // Add delay between batches (except for the last one)
430433 if end < len (proxies ) {
431434 select {
432435 case <- ctx .Done ():
433- log . Printf ("Context cancelled, stopping progressive checking at batch %d/%d with %d healthy proxies found" , batchNum , totalBatches , healthyCount )
436+ c . logger . WarnBg ("Context cancelled, stopping progressive checking at batch %d/%d with %d healthy proxies found" , batchNum , totalBatches , healthyCount )
434437 return allResults
435438 case <- time .After (c .batchDelay ):
436439 // Continue to next batch
@@ -444,7 +447,7 @@ func (c *DBChecker) checkProxiesProgressive(ctx context.Context, proxies []scrap
444447 healthyCount ++
445448 }
446449 }
447- log . Printf ("Progressive checking completed: checked %d proxies in %d batches, found %d healthy" , len (proxies ), totalBatches , healthyCount )
450+ c . logger . InfoBg ("Progressive checking completed: checked %d proxies in %d batches, found %d healthy" , len (proxies ), totalBatches , healthyCount )
448451 return allResults
449452}
450453
0 commit comments