Merged in feat/warmup-cache-function (pull request #1581)
Feat/warmup cache function * WIP * Fix warmup for all languages * Cleanup * Added env flag so we can disable warmup * Changed time to 04.00 UTC since backend updates their data at 03.00 UTC * Add return statements * Merge branch 'master' into feat/warmup-cache-function Approved-by: Anton Gunnarsson
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
import type { Config, Context } from "@netlify/functions"
|
||||
import { warmupHotelDataOnLang } from "../utils/hoteldata"
|
||||
|
||||
export default async (_request: Request, _context: Context) => {
|
||||
await warmupHotelDataOnLang(Lang.da)
|
||||
return new Response("Warmup success", { status: 200 })
|
||||
}
|
||||
|
||||
export const config: Config = {
|
||||
schedule: "0 4 * * *",
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
import type { Config, Context } from "@netlify/functions"
|
||||
import { warmupHotelDataOnLang } from "../utils/hoteldata"
|
||||
|
||||
export default async (_request: Request, _context: Context) => {
|
||||
await warmupHotelDataOnLang(Lang.de)
|
||||
return new Response("Warmup success", { status: 200 })
|
||||
}
|
||||
|
||||
export const config: Config = {
|
||||
schedule: "5 4 * * *",
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
import type { Config, Context } from "@netlify/functions"
|
||||
import { warmupHotelDataOnLang } from "../utils/hoteldata"
|
||||
|
||||
export default async (_request: Request, _context: Context) => {
|
||||
await warmupHotelDataOnLang(Lang.en)
|
||||
return new Response("Warmup success", { status: 200 })
|
||||
}
|
||||
|
||||
export const config: Config = {
|
||||
schedule: "10 4 * * *",
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
import type { Config, Context } from "@netlify/functions"
|
||||
import { warmupHotelDataOnLang } from "../utils/hoteldata"
|
||||
|
||||
export default async (_request: Request, _context: Context) => {
|
||||
await warmupHotelDataOnLang(Lang.fi)
|
||||
return new Response("Warmup success", { status: 200 })
|
||||
}
|
||||
|
||||
export const config: Config = {
|
||||
schedule: "15 4 * * *",
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
import type { Config, Context } from "@netlify/functions"
|
||||
import { warmupHotelDataOnLang } from "../utils/hoteldata"
|
||||
|
||||
export default async (_request: Request, _context: Context) => {
|
||||
await warmupHotelDataOnLang(Lang.no)
|
||||
return new Response("Warmup success", { status: 200 })
|
||||
}
|
||||
|
||||
export const config: Config = {
|
||||
schedule: "20 4 * * *",
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
import type { Config, Context } from "@netlify/functions"
|
||||
import { warmupHotelDataOnLang } from "../utils/hoteldata"
|
||||
|
||||
export default async (_request: Request, _context: Context) => {
|
||||
await warmupHotelDataOnLang(Lang.sv)
|
||||
return new Response("Warmup success", { status: 200 })
|
||||
}
|
||||
|
||||
export const config: Config = {
|
||||
schedule: "25 4 * * *",
|
||||
}
|
||||
@@ -17,7 +17,10 @@ export default async (request: Request, _context: Context) => {
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(`Error syncing sitemap: ${error}`)
|
||||
return new Response("Failed to sync sitemap", { status: 500 })
|
||||
}
|
||||
|
||||
return new Response("Sitemap synced successfully", { status: 200 })
|
||||
}
|
||||
|
||||
export const config: Config = {
|
||||
|
||||
30
apps/scandic-web/netlify/utils/hoteldata.ts
Normal file
30
apps/scandic-web/netlify/utils/hoteldata.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
export async function warmupHotelDataOnLang(lang: Lang) {
|
||||
const PUBLIC_URL = Netlify.env.get("PUBLIC_URL")
|
||||
console.info(
|
||||
`[WARMUP] Started warmup cache hoteldata for language ${lang} at: ${new Date().toISOString()}!`
|
||||
)
|
||||
|
||||
try {
|
||||
const hotelsResponse = await fetch(
|
||||
`${PUBLIC_URL}/api/hoteldata?lang=${lang}`,
|
||||
{
|
||||
headers: { cache: "no-store" },
|
||||
}
|
||||
)
|
||||
|
||||
if (!hotelsResponse.ok) {
|
||||
throw new Error(
|
||||
`[WARMUP] Failed to warmup cache for hotels on language ${lang} with error: ${hotelsResponse.statusText}`
|
||||
)
|
||||
}
|
||||
|
||||
const hotels = await hotelsResponse.json()
|
||||
console.info(`[WARMUP] Retrieved ${hotels.length} hotels.`)
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[WARMUP] Error warming cache with hoteldata on language ${lang} with error: ${error}`
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user