feat: add comprehensive logging for background refresh

Add detailed logging to verify background refresh is working:
- Log when background refresh is triggered
- Log the response status and result from refresh endpoint
- Log errors if refresh fetch fails
- Add logging in refresh endpoint to track:
  - When refresh starts
  - When metadata is found
  - When metadata is cached
  - Any errors during refresh

This will help diagnose if background refresh is actually
populating the Redis cache after timeouts.
This commit is contained in:
Gigi
2025-11-07 19:19:47 +01:00
parent 66db9cd23f
commit b0e43ccee7
2 changed files with 19 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
const expectedSecret = process.env.OG_REFRESH_SECRET || ''
if (providedSecret !== expectedSecret) {
console.error('Background refresh unauthorized: secret mismatch')
return res.status(401).json({ error: 'Unauthorized' })
}
@@ -16,19 +17,24 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
return res.status(400).json({ error: 'Missing naddr parameter' })
}
console.log(`Background refresh started for ${naddr}`)
try {
// Fetch metadata via relays (WebSockets)
// Fetch metadata via relays (WebSockets) - no timeout, let it take as long as needed
const meta = await fetchArticleMetadataViaRelays(naddr)
if (meta) {
console.log(`Background refresh found metadata for ${naddr}:`, { title: meta.title, summary: meta.summary?.substring(0, 50) })
// Store in Redis
await setArticleMeta(naddr, meta)
console.log(`Background refresh cached metadata for ${naddr}`)
return res.status(200).json({ ok: true, cached: true })
} else {
console.log(`Background refresh found no metadata for ${naddr}`)
return res.status(200).json({ ok: true, cached: false })
}
} catch (err) {
console.error('Error refreshing article metadata:', err)
console.error(`Error refreshing article metadata for ${naddr}:`, err)
return res.status(500).json({ error: 'Internal server error' })
}
}

View File

@@ -67,13 +67,21 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
? `${req.headers['x-forwarded-proto']}://${req.headers['x-forwarded-host']}`
: `https://read.withboris.com`
fetch(`${origin}/api/article-og-refresh?naddr=${encodeURIComponent(naddr)}`, {
const refreshUrl = `${origin}/api/article-og-refresh?naddr=${encodeURIComponent(naddr)}`
console.log(`Triggering background refresh for ${naddr}`)
fetch(refreshUrl, {
method: 'POST',
headers: { 'x-refresh-key': secret },
keepalive: true
}).catch(() => {
// Ignore errors in background refresh trigger
})
.then(async (resp) => {
const result = await resp.json().catch(() => ({}))
console.log(`Background refresh response for ${naddr}:`, { status: resp.status, result })
})
.catch((err) => {
console.error(`Background refresh failed for ${naddr}:`, err)
})
}
// Generate and send HTML