Skip to content

Commit 2f76857

Browse files
committed
Logging and other fixes. This one fixes #12 among other changes.
1 parent 0bc1314 commit 2f76857

4 files changed

Lines changed: 31 additions & 42 deletions

File tree

cmd/dxcluster-client/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ func RunApplication(ctx context.Context, args []string) int {
102102
}
103103
}
104104

105-
logging.Notice("Starting %s v%s (+%s)", version.ProjectName, version.ProjectVersion, version.ProjectGitHubURL)
106-
logging.Notice("User-Agent: %s", version.UserAgent)
105+
logging.Notice("Starting %s %s (+%s)", version.ProjectName, version.ProjectVersion, version.ProjectGitHubURL)
107106

108107
ctx, cancel := context.WithCancel(ctx)
109108
defer cancel()
@@ -281,7 +280,7 @@ func RunApplication(ctx context.Context, args []string) int {
281280
// CRITICAL BARRIER: DXCC and LoTW data are now FULLY LOADED and ready.
282281
// ═══════════════════════════════════════════════════════════════════════════
283282
// Spots can now be enriched immediately upon arrival from any source.
284-
logging.Info("DXCC and LoTW data loaded. Ready to initialize spot sources and HTTP API.")
283+
logging.Notice("DXCC and LoTW data loaded. Ready to initialize spot sources and HTTP API.")
285284

286285
// --- 6. Initialize POTA Client (if enabled) ---
287286
var potaClient *pota.Client

internal/dxcc/client.go

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"net/http"
12+
"os"
1213
"regexp"
1314
"strconv"
1415
"strings"
@@ -247,11 +248,24 @@ func (c *Client) Close() {
247248

248249
// createTables creates the DXCC tables if they don't exist.
249250
func (c *Client) createTables() error {
250-
// First, drop existing tables to ensure new schema
251-
dropQueries := []string{
252-
fmt.Sprintf("DROP TABLE IF EXISTS %s", prefixesTableName),
253-
fmt.Sprintf("DROP TABLE IF EXISTS %s", exceptionsTableName),
254-
fmt.Sprintf("DROP TABLE IF EXISTS %s", entitiesTableName),
251+
// Only drop tables if DXCC_FORCE_REBUILD environment variable is set
252+
// This prevents data loss on every restart while allowing schema updates when needed
253+
forceRebuild := os.Getenv("DXCC_FORCE_REBUILD") == "true" || os.Getenv("DXCC_FORCE_REBUILD") == "1"
254+
255+
if forceRebuild {
256+
logging.Warn("DXCC_FORCE_REBUILD is set - dropping existing tables to rebuild schema")
257+
dropQueries := []string{
258+
fmt.Sprintf("DROP TABLE IF EXISTS %s", prefixesTableName),
259+
fmt.Sprintf("DROP TABLE IF EXISTS %s", exceptionsTableName),
260+
fmt.Sprintf("DROP TABLE IF EXISTS %s", entitiesTableName),
261+
}
262+
263+
db := c.dbClient.GetDB()
264+
for _, query := range dropQueries {
265+
if _, err := db.Exec(query); err != nil {
266+
return fmt.Errorf("failed to drop table: %w\nQuery: %s", err, query)
267+
}
268+
}
255269
}
256270

257271
queries := []string{
@@ -311,13 +325,6 @@ func (c *Client) createTables() error {
311325

312326
db := c.dbClient.GetDB()
313327

314-
// Drop existing tables first
315-
for _, query := range dropQueries {
316-
if _, err := db.Exec(query); err != nil {
317-
return fmt.Errorf("failed to drop table: %w\nQuery: %s", err, query)
318-
}
319-
}
320-
321328
// Create new tables with updated schema
322329
for _, query := range queries {
323330
if _, err := db.Exec(query); err != nil {
@@ -522,9 +529,9 @@ func (c *Client) loadMapsFromDB(ctx context.Context) error {
522529
exceptionCount := len(c.exceptionsMap)
523530
entityCount := len(c.entitiesMap)
524531

525-
// Only log at INFO level if we actually have data, otherwise use DEBUG
532+
// Only log at NOTICE level if we actually have data, otherwise use DEBUG
526533
if prefixCount > 0 || exceptionCount > 0 || entityCount > 0 {
527-
logging.Info("DXCC data loaded: %d prefixes, %d exceptions, %d entities.", prefixCount, exceptionCount, entityCount)
534+
logging.Notice("DXCC data loaded: %d prefixes, %d exceptions, %d entities.", prefixCount, exceptionCount, entityCount)
528535
} else {
529536
logging.Debug("DXCC data loaded: %d prefixes, %d exceptions, %d entities (empty - will check for updates).", prefixCount, exceptionCount, entityCount)
530537
}
@@ -537,30 +544,14 @@ func (c *Client) loadMapsFromDB(ctx context.Context) error {
537544
}
538545

539546
// StartUpdater starts the periodic update job for DXCC data.
547+
// Note: Does NOT perform an initial check - main.go handles that synchronously before calling this.
540548
func (c *Client) StartUpdater(ctx context.Context) {
541549
if c.updateStop != nil {
542550
return // already running
543551
}
544552
c.updateStop = make(chan struct{})
545553
c.updateDone = make(chan struct{})
546554

547-
// Check if initial update is needed
548-
go func() {
549-
needsUpdate, err := c.needsUpdate(ctx)
550-
if err != nil {
551-
logging.Error("Failed to check if DXCC data needs update: %v", err)
552-
// Fallback to update on error
553-
needsUpdate = true
554-
}
555-
556-
if needsUpdate {
557-
logging.Info("DXCC data needs update, fetching...")
558-
c.fetchAndStoreData(ctx)
559-
} else {
560-
logging.Info("DXCC data is up to date, skipping initial download")
561-
}
562-
}()
563-
564555
go func() {
565556
ticker := time.NewTicker(c.cfg.DXCCUpdateInterval)
566557
defer ticker.Stop()
@@ -577,7 +568,7 @@ func (c *Client) StartUpdater(ctx context.Context) {
577568
}
578569
}
579570
}()
580-
logging.Info("DXCC updater started. Will check for updates every %s.", c.cfg.DXCCUpdateInterval)
571+
logging.Notice("DXCC updater started. Will check for updates every %s.", c.cfg.DXCCUpdateInterval)
581572
}
582573

583574
// fetchAndStoreData downloads the cty.xml.gz, parses it, and replaces data in the database.
@@ -652,7 +643,7 @@ func (c *Client) fetchAndStoreData(ctx context.Context) {
652643
logging.Error("Both Club Log and GitHub DXCC downloads failed after retries: %v", err)
653644
return
654645
}
655-
logging.Info("Successfully downloaded DXCC data from GitHub fallback.")
646+
logging.Notice("Successfully downloaded DXCC data from GitHub fallback.")
656647
// Debug: show a small diagnostic about the fetched payload to help tests
657648
gz := false
658649
if len(data) >= 2 && data[0] == 0x1f && data[1] == 0x8b {
@@ -668,7 +659,7 @@ func (c *Client) fetchAndStoreData(ctx context.Context) {
668659
}
669660
logging.Debug("DXCC fallback download diagnostics: bytes=%d gzipped=%t head=%q", len(data), gz, head)
670661
} else {
671-
logging.Info("Successfully downloaded DXCC data from Club Log.")
662+
logging.Notice("Successfully downloaded DXCC data from Club Log.")
672663
}
673664

674665
xmlData, err := c.decompressAndParseXML(data)

internal/lotw/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (c *Client) StartUpdater(ctx context.Context) {
213213
logging.Info("LoTW data needs update, fetching...")
214214
c.fetchAndStoreUsers(ctx)
215215
} else {
216-
logging.Info("LoTW data is up to date, skipping initial download")
216+
logging.Notice("LoTW data is up to date, skipping initial download")
217217
}
218218
}()
219219

internal/pota/client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func NewClient(ctx context.Context, cfg config.Config, rdb *redisclient.Client)
268268
// Choose caching mechanism
269269
if cfg.Redis.Enabled && rdb != nil {
270270
client.cacher = NewRedisSpotCacher(rdb, cfg.Redis.SpotExpiry)
271-
logging.Info("POTA client using Redis cache with expiry: %s", cfg.Redis.SpotExpiry)
271+
logging.Notice("POTA client using Redis cache with expiry: %s", cfg.Redis.SpotExpiry)
272272
} else {
273273
client.cacher = NewInMemorySpotCacher(cfg.MaxCache) // Use MaxCache as a proxy for in-memory size
274274
logging.Info("POTA client using in-memory cache (max %d spots).", cfg.MaxCache)
@@ -312,7 +312,7 @@ func (c *Client) StartPolling(ctx context.Context) {
312312
}
313313
}
314314
}()
315-
logging.Info("POTA polling started. Initial poll will run shortly, then every %s.", c.cfg.POTAPollInterval)
315+
logging.Notice("POTA polling started. Initial poll will run shortly, then every %s.", c.cfg.POTAPollInterval)
316316
}
317317

318318
// StopPolling halts the continuous polling.
@@ -332,7 +332,6 @@ func (c *Client) StopPolling() {
332332

333333
// fetchAndProcessSpots fetches, parses, and caches POTA spots.
334334
func (c *Client) fetchAndProcessSpots(ctx context.Context) {
335-
logging.Info("Fetching POTA spots from %s", config.POTAAPIEndpoint)
336335
req, err := http.NewRequestWithContext(ctx, "GET", config.POTAAPIEndpoint, nil)
337336
if err != nil {
338337
logging.Error("Failed to create HTTP request for POTA API: %v", err)
@@ -363,7 +362,7 @@ func (c *Client) fetchAndProcessSpots(ctx context.Context) {
363362
logging.Error("Failed to unmarshal POTA API response: %v", err)
364363
return
365364
}
366-
logging.Debug("Received %d raw POTA spots.", len(rawSpots))
365+
logging.Notice("Fetching POTA spots from %s: received %d spots", config.POTAAPIEndpoint, len(rawSpots))
367366

368367
for _, item := range rawSpots {
369368
// Parse frequency from string to float64

0 commit comments

Comments
 (0)