294 lines
10 KiB
TypeScript
294 lines
10 KiB
TypeScript
import { Buffer } from 'buffer'
|
|
import { scaffoldProducts, scaffoldAvailability } from '@/src/trpc/apis/common-apis/common'
|
|
import { scaffoldEssentialConsts } from '@/src/trpc/apis/common-apis/common-trpc-index'
|
|
import { scaffoldStores } from '@/src/trpc/apis/user-apis/apis/stores'
|
|
import { scaffoldSlotsWithProducts } from '@/src/trpc/apis/user-apis/apis/slots'
|
|
import { scaffoldBanners } from '@/src/trpc/apis/user-apis/apis/banners'
|
|
import { scaffoldStoreWithProducts } from '@/src/trpc/apis/user-apis/apis/stores'
|
|
import {
|
|
getStoresSummary,
|
|
getAllRegions,
|
|
getEffectiveBoundId,
|
|
incrementRegionCacheVersion,
|
|
incrementRegionAvailabilityVersionNum,
|
|
incrementRegionSlotsVersionNum,
|
|
} from '@/src/dbService'
|
|
import { imageUploadS3 } from '@/src/lib/s3-client'
|
|
import { applyImageCacheBust } from '@/src/lib/product-image-cache'
|
|
import { getApiCacheKey, getCloudflareApiToken, getCloudflareZoneId, getAssetsDomain } from '@/src/lib/env-exporter'
|
|
import {
|
|
CACHE_FILENAMES,
|
|
availabilityCacheUrl,
|
|
availabilityObjectKey,
|
|
regionCacheObjectKey,
|
|
regionCacheUrl,
|
|
slotsCacheUrl,
|
|
slotsObjectKey,
|
|
storeDetailCacheUrl,
|
|
storeDetailObjectKey,
|
|
} from '@packages/shared'
|
|
import { retryWithExponentialBackoff } from '@/src/lib/retry'
|
|
import { refreshGenKvForRegion } from '@/src/lib/gen-store'
|
|
|
|
export interface RegionCacheFilesResult {
|
|
regionId: number
|
|
cacheVersion: number
|
|
products: string
|
|
essentialConsts: string
|
|
stores: string
|
|
slotsVersion: number
|
|
availabilityVersion: number
|
|
banners: string
|
|
individualStores: string[]
|
|
}
|
|
|
|
export async function createRegionCacheFiles(regionId: number): Promise<RegionCacheFilesResult> {
|
|
console.log(`Starting creation of cache files for region ${regionId}...`)
|
|
|
|
const cacheVersion = await incrementRegionCacheVersion(regionId)
|
|
// Bump the other two counters BEFORE any file is built: `essential-consts.json`
|
|
// (and the KV mirror) reads the region's version rows, so it must see the same
|
|
// versions the files are about to be written under.
|
|
const availabilityVersion = await incrementRegionAvailabilityVersionNum(regionId)
|
|
const slotsVersion = await incrementRegionSlotsVersionNum(regionId)
|
|
|
|
// Create all per-region cache files in parallel
|
|
const [
|
|
productsKey,
|
|
essentialConstsKey,
|
|
storesKey,
|
|
bannersKey,
|
|
individualStoreKeys,
|
|
] = await Promise.all([
|
|
createProductsFileInternal(regionId, cacheVersion),
|
|
createEssentialConstsFileInternal(regionId, cacheVersion),
|
|
createStoresFileInternal(regionId, cacheVersion),
|
|
createBannersFileInternal(regionId, cacheVersion),
|
|
createAllStoresFilesInternal(regionId, cacheVersion),
|
|
// Side effects only; their versions were hoisted above.
|
|
createSlotsCacheFile(regionId, false, slotsVersion),
|
|
createAvailabilityCacheFile(regionId, false, availabilityVersion),
|
|
])
|
|
|
|
const stores = await getStoresSummary(regionId)
|
|
const assetsDomain = getAssetsDomain()
|
|
const apiCacheKey = getApiCacheKey()
|
|
|
|
// Collect all per-region URLs for batch cache purge
|
|
const urls = [
|
|
regionCacheUrl(assetsDomain, apiCacheKey, regionId, cacheVersion, CACHE_FILENAMES.products),
|
|
regionCacheUrl(assetsDomain, apiCacheKey, regionId, cacheVersion, CACHE_FILENAMES.essentialConsts),
|
|
regionCacheUrl(assetsDomain, apiCacheKey, regionId, cacheVersion, CACHE_FILENAMES.stores),
|
|
slotsCacheUrl(assetsDomain, regionId, slotsVersion),
|
|
availabilityCacheUrl(assetsDomain, regionId, availabilityVersion),
|
|
regionCacheUrl(assetsDomain, apiCacheKey, regionId, cacheVersion, CACHE_FILENAMES.banners),
|
|
...stores.map((store) =>
|
|
storeDetailCacheUrl(assetsDomain, apiCacheKey, regionId, cacheVersion, store.id)
|
|
),
|
|
].filter((url) => url.length > 0)
|
|
|
|
// Purge all caches in one batch with retry
|
|
try {
|
|
await retryWithExponentialBackoff(() => clearUrlCache(urls))
|
|
console.log(`Cache purged for all ${urls.length} files of region ${regionId}`)
|
|
} catch (error) {
|
|
console.error(`Failed to purge cache for region ${regionId} files after 3 retries`, error)
|
|
}
|
|
|
|
console.log(`Cache files created successfully for region ${regionId}`)
|
|
|
|
// Single awaited mirror refresh for the region (all regional counters bumped above).
|
|
await refreshGenKvForRegion(regionId)
|
|
|
|
return {
|
|
regionId,
|
|
cacheVersion,
|
|
products: productsKey,
|
|
essentialConsts: essentialConstsKey,
|
|
stores: storesKey,
|
|
slotsVersion,
|
|
availabilityVersion,
|
|
banners: bannersKey,
|
|
individualStores: individualStoreKeys,
|
|
}
|
|
}
|
|
|
|
// With no regionId, generates cache files for EVERY region (sequentially to
|
|
// avoid D1 version-counter races). Returns per-region results.
|
|
export async function createAllCacheFiles(regionId?: number): Promise<RegionCacheFilesResult[]> {
|
|
const regionIds = regionId != null
|
|
? [regionId]
|
|
: (await getAllRegions()).map((r) => r.id)
|
|
|
|
const results: RegionCacheFilesResult[] = []
|
|
for (const id of regionIds) {
|
|
results.push(await createRegionCacheFiles(id))
|
|
}
|
|
return results
|
|
}
|
|
|
|
// Internal versions that skip cache purging (for batch operations)
|
|
async function createProductsFileInternal(regionId: number, version: number): Promise<string> {
|
|
const productsData = await scaffoldProducts(regionId)
|
|
|
|
// Only the published catalogue carries the cache-busting query: the admin
|
|
// endpoint and the test snapshot controller keep the raw asset URLs.
|
|
const catalogue = applyImageCacheBust(productsData)
|
|
const jsonContent = JSON.stringify(catalogue, null, 2)
|
|
const buffer = Buffer.from(jsonContent, 'utf-8')
|
|
const filePath = regionCacheObjectKey(
|
|
getApiCacheKey(),
|
|
regionId,
|
|
version,
|
|
CACHE_FILENAMES.products
|
|
)
|
|
|
|
console.log(filePath)
|
|
return await imageUploadS3(
|
|
buffer,
|
|
'application/json',
|
|
filePath
|
|
)
|
|
|
|
}
|
|
|
|
export async function createAvailabilityCacheFile(
|
|
regionId: number,
|
|
refresh = true,
|
|
version?: number
|
|
): Promise<number> {
|
|
const fileVersion = version ?? (await incrementRegionAvailabilityVersionNum(regionId))
|
|
const availabilityData = await scaffoldAvailability(regionId)
|
|
const jsonContent = JSON.stringify(availabilityData, null, 2)
|
|
const buffer = Buffer.from(jsonContent, 'utf-8')
|
|
const filePath = availabilityObjectKey(regionId, fileVersion)
|
|
|
|
console.log(filePath)
|
|
await imageUploadS3(
|
|
buffer,
|
|
'application/json',
|
|
filePath
|
|
)
|
|
|
|
if (refresh) await refreshGenKvForRegion(regionId)
|
|
|
|
return fileVersion
|
|
}
|
|
|
|
async function createEssentialConstsFileInternal(regionId: number, version: number): Promise<string> {
|
|
const boundId = await getEffectiveBoundId(regionId)
|
|
if (boundId == null) throw new Error(`Region ${regionId} has no bound; cannot build essential-consts cache`)
|
|
const essentialConstsData = await scaffoldEssentialConsts(boundId)
|
|
const jsonContent = JSON.stringify(essentialConstsData, null, 2)
|
|
const buffer = Buffer.from(jsonContent, 'utf-8')
|
|
return await imageUploadS3(
|
|
buffer,
|
|
'application/json',
|
|
regionCacheObjectKey(getApiCacheKey(), regionId, version, CACHE_FILENAMES.essentialConsts)
|
|
)
|
|
}
|
|
|
|
async function createStoresFileInternal(regionId: number, version: number): Promise<string> {
|
|
const storesData = await scaffoldStores(regionId)
|
|
const jsonContent = JSON.stringify(storesData, null, 2)
|
|
const buffer = Buffer.from(jsonContent, 'utf-8')
|
|
return await imageUploadS3(
|
|
buffer,
|
|
'application/json',
|
|
regionCacheObjectKey(getApiCacheKey(), regionId, version, CACHE_FILENAMES.stores)
|
|
)
|
|
}
|
|
|
|
export async function createSlotsCacheFile(
|
|
regionId: number,
|
|
refresh = true,
|
|
version?: number
|
|
): Promise<number> {
|
|
const fileVersion = version ?? (await incrementRegionSlotsVersionNum(regionId))
|
|
const slotsData = await scaffoldSlotsWithProducts(regionId)
|
|
const jsonContent = JSON.stringify(slotsData, null, 2)
|
|
const buffer = Buffer.from(jsonContent, 'utf-8')
|
|
const filePath = slotsObjectKey(regionId, fileVersion)
|
|
|
|
console.log(filePath)
|
|
await imageUploadS3(
|
|
buffer,
|
|
'application/json',
|
|
filePath
|
|
)
|
|
|
|
if (refresh) await refreshGenKvForRegion(regionId)
|
|
|
|
return fileVersion
|
|
}
|
|
|
|
async function createBannersFileInternal(regionId: number, version: number): Promise<string> {
|
|
const bannersData = await scaffoldBanners(regionId)
|
|
const jsonContent = JSON.stringify(bannersData, null, 2)
|
|
const buffer = Buffer.from(jsonContent, 'utf-8')
|
|
return await imageUploadS3(
|
|
buffer,
|
|
'application/json',
|
|
regionCacheObjectKey(getApiCacheKey(), regionId, version, CACHE_FILENAMES.banners)
|
|
)
|
|
}
|
|
|
|
async function createAllStoresFilesInternal(regionId: number, version: number): Promise<string[]> {
|
|
|
|
const stores = await getStoresSummary(regionId)
|
|
const results: string[] = []
|
|
|
|
for (const store of stores) {
|
|
const storeData = await scaffoldStoreWithProducts(regionId, store.id)
|
|
const jsonContent = JSON.stringify(storeData, null, 2)
|
|
const buffer = Buffer.from(jsonContent, 'utf-8')
|
|
const s3Key = await imageUploadS3(
|
|
buffer,
|
|
'application/json',
|
|
storeDetailObjectKey(getApiCacheKey(), regionId, version, store.id)
|
|
)
|
|
results.push(s3Key)
|
|
}
|
|
|
|
console.log(`Created ${results.length} store cache files for region ${regionId}`)
|
|
return results
|
|
}
|
|
|
|
export async function clearUrlCache(urls: string[]): Promise<{ success: boolean; errors?: string[] }> {
|
|
const cloudflareApiToken = getCloudflareApiToken()
|
|
const cloudflareZoneId = getCloudflareZoneId()
|
|
if (!cloudflareApiToken || !cloudflareZoneId) {
|
|
console.warn('Cloudflare credentials not configured, skipping cache clear')
|
|
return { success: false, errors: ['Cloudflare credentials not configured'] }
|
|
}
|
|
|
|
try {
|
|
const resp = await fetch(
|
|
`https://api.cloudflare.com/client/v4/zones/${cloudflareZoneId}/purge_cache`,
|
|
{
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': `Bearer ${cloudflareApiToken}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ files: urls }),
|
|
}
|
|
)
|
|
|
|
const result = await resp.json() as { success: boolean; errors?: { message: string }[] }
|
|
|
|
if (!result.success) {
|
|
const errorMessages = result.errors?.map(e => e.message) || ['Unknown error']
|
|
console.error(`Cloudflare cache purge failed for URLs: ${urls.join(', ')}`, errorMessages)
|
|
return { success: false, errors: errorMessages }
|
|
}
|
|
|
|
console.log(`Successfully purged ${urls.length} URLs from Cloudflare cache: ${urls.join(', ')}`)
|
|
return { success: true }
|
|
} catch (error) {
|
|
console.log(error)
|
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
console.error(`Error clearing Cloudflare cache for URLs: ${urls.join(', ')}`, errorMessage)
|
|
return { success: false, errors: [errorMessage] }
|
|
}
|
|
}
|