Initialize broadcaster with last broadcast time from database
This commit is contained in:
parent
c13bf2bf35
commit
be5792a0c7
@ -31,8 +31,17 @@ func broadcaster(shutdown chan struct{}, dryRun bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Track when the last broadcast happened
|
||||
var lastBroadcastTime time.Time
|
||||
// Initialize the last broadcast time from the database
|
||||
lastBroadcastTime := getLastBroadcastTime()
|
||||
|
||||
if !lastBroadcastTime.IsZero() {
|
||||
logInfo("broadcaster", "Initialized last broadcast time from database", map[string]interface{}{
|
||||
"lastBroadcastTime": lastBroadcastTime.Format(time.RFC3339),
|
||||
"timeSince": time.Since(lastBroadcastTime).String(),
|
||||
})
|
||||
} else {
|
||||
logInfo("broadcaster", "No previous broadcast time found in database", nil)
|
||||
}
|
||||
|
||||
// Run checks frequently
|
||||
ticker := time.NewTicker(BROADCAST_CHECK_INTERVAL)
|
||||
|
36
storage.go
36
storage.go
@ -728,3 +728,39 @@ func logInfo(component string, message string, data map[string]interface{}) {
|
||||
// Log to structured log file and database
|
||||
logEvent("info", logData)
|
||||
}
|
||||
|
||||
// getLastBroadcastTime retrieves the most recent broadcast time from the database
|
||||
func getLastBroadcastTime() time.Time {
|
||||
var lastBroadcastTime time.Time
|
||||
|
||||
// Query for the most recent valid broadcast time
|
||||
row := db.QueryRow(`
|
||||
SELECT MAX(broadcastTime) FROM articles
|
||||
WHERE broadcastTime IS NOT NULL
|
||||
AND broadcastTime > 1
|
||||
AND broadcastTime != 0
|
||||
AND datetime(broadcastTime) != '1970-01-01 00:00:00'
|
||||
AND datetime(broadcastTime) != '0001-01-01 00:00:00'
|
||||
AND strftime('%Y', broadcastTime) > '2000' -- Ensure year is at least 2000
|
||||
`)
|
||||
|
||||
// Scan the result
|
||||
var lastTime sql.NullTime
|
||||
if err := row.Scan(&lastTime); err != nil {
|
||||
logInfo("db", "Error retrieving last broadcast time", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return time.Time{} // Return zero time on error
|
||||
}
|
||||
|
||||
if lastTime.Valid {
|
||||
lastBroadcastTime = lastTime.Time
|
||||
logInfo("db", "Retrieved last broadcast time", map[string]interface{}{
|
||||
"lastBroadcastTime": lastBroadcastTime.Format(time.RFC3339),
|
||||
})
|
||||
} else {
|
||||
logInfo("db", "No previous broadcasts found in database", nil)
|
||||
}
|
||||
|
||||
return lastBroadcastTime
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user