Merged in feature/warmup (pull request #1887)
* unified warmup function Approved-by: Linus Flood
This commit is contained in:
50
apps/scandic-web/services/warmup/index.ts
Normal file
50
apps/scandic-web/services/warmup/index.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
import { warmupCountry } from "./warmupCountries"
|
||||
import { warmupHotelData } from "./warmupHotelData"
|
||||
import { warmupHotelIdsByCountry } from "./warmupHotelIdsByCountry"
|
||||
|
||||
import type { WarmupFunctionsKey } from "./warmupKeys"
|
||||
|
||||
export type WarmupFunction = () => Promise<WarmupResult>
|
||||
|
||||
type BaseWarmup = {
|
||||
status: "skipped" | "completed"
|
||||
}
|
||||
|
||||
type FailedWarmup = {
|
||||
status: "error"
|
||||
error: Error
|
||||
}
|
||||
|
||||
export type WarmupResult = BaseWarmup | FailedWarmup
|
||||
|
||||
export const warmupFunctions: Record<WarmupFunctionsKey, WarmupFunction> = {
|
||||
countries_en: warmupCountry(Lang.en),
|
||||
countries_da: warmupCountry(Lang.da),
|
||||
countries_de: warmupCountry(Lang.de),
|
||||
countries_fi: warmupCountry(Lang.fi),
|
||||
countries_sv: warmupCountry(Lang.sv),
|
||||
countries_no: warmupCountry(Lang.no),
|
||||
|
||||
hotelsByCountry: warmupHotelIdsByCountry(),
|
||||
|
||||
hotelData_en: warmupHotelData(Lang.en),
|
||||
hotelData_da: warmupHotelData(Lang.da),
|
||||
hotelData_de: warmupHotelData(Lang.de),
|
||||
hotelData_fi: warmupHotelData(Lang.fi),
|
||||
hotelData_sv: warmupHotelData(Lang.sv),
|
||||
hotelData_no: warmupHotelData(Lang.no),
|
||||
}
|
||||
|
||||
export async function warmup(key: WarmupFunctionsKey): Promise<WarmupResult> {
|
||||
const func = warmupFunctions[key]
|
||||
if (!func) {
|
||||
return {
|
||||
status: "error",
|
||||
error: new Error(`Warmup function ${key} not found`),
|
||||
}
|
||||
}
|
||||
|
||||
return func()
|
||||
}
|
||||
28
apps/scandic-web/services/warmup/warmupCountries.ts
Normal file
28
apps/scandic-web/services/warmup/warmupCountries.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { getCountries } from "@/server/routers/hotels/utils"
|
||||
import { getServiceToken } from "@/server/tokenManager"
|
||||
|
||||
import type { Lang } from "@/constants/languages"
|
||||
import type { WarmupFunction, WarmupResult } from "."
|
||||
|
||||
export const warmupCountry =
|
||||
(lang: Lang): WarmupFunction =>
|
||||
async (): Promise<WarmupResult> => {
|
||||
try {
|
||||
const serviceToken = await getServiceToken()
|
||||
|
||||
await getCountries({
|
||||
lang: lang,
|
||||
serviceToken: serviceToken.access_token,
|
||||
warmup: true,
|
||||
})
|
||||
} catch (error) {
|
||||
return {
|
||||
status: "error",
|
||||
error: error as Error,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
status: "completed",
|
||||
}
|
||||
}
|
||||
31
apps/scandic-web/services/warmup/warmupHotelData.ts
Normal file
31
apps/scandic-web/services/warmup/warmupHotelData.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { env } from "@/env/server"
|
||||
import { serverClient } from "@/lib/trpc/server"
|
||||
|
||||
import type { Lang } from "@/constants/languages"
|
||||
import type { WarmupFunction, WarmupResult } from "."
|
||||
|
||||
export const warmupHotelData =
|
||||
(lang: Lang): WarmupFunction =>
|
||||
async (): Promise<WarmupResult> => {
|
||||
if (!env.ENABLE_WARMUP_HOTEL) {
|
||||
return {
|
||||
status: "skipped",
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await serverClient().hotel.hotels.getDestinationsMapData({
|
||||
lang,
|
||||
warmup: true,
|
||||
})
|
||||
} catch (error) {
|
||||
return {
|
||||
status: "error",
|
||||
error: error as Error,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
status: "completed",
|
||||
}
|
||||
}
|
||||
60
apps/scandic-web/services/warmup/warmupHotelIdsByCountry.ts
Normal file
60
apps/scandic-web/services/warmup/warmupHotelIdsByCountry.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
import {
|
||||
getCountries,
|
||||
getHotelIdsByCountry,
|
||||
} from "@/server/routers/hotels/utils"
|
||||
import { getServiceToken } from "@/server/tokenManager"
|
||||
|
||||
import { safeTry } from "@/utils/safeTry"
|
||||
|
||||
import type { WarmupFunction, WarmupResult } from "."
|
||||
|
||||
export const warmupHotelIdsByCountry =
|
||||
(): WarmupFunction => async (): Promise<WarmupResult> => {
|
||||
try {
|
||||
let serviceToken = await getServiceToken()
|
||||
|
||||
const [countries, countriesError] = await safeTry(
|
||||
getCountries({
|
||||
lang: Lang.en,
|
||||
serviceToken: serviceToken.access_token,
|
||||
warmup: true,
|
||||
})
|
||||
)
|
||||
|
||||
if (!countries || countriesError) {
|
||||
return {
|
||||
status: "error",
|
||||
error: new Error("Unable to get countries"),
|
||||
}
|
||||
}
|
||||
|
||||
const countryNames = countries.data.map((country) => country.name)
|
||||
for (const countryName of countryNames) {
|
||||
serviceToken = await getServiceToken()
|
||||
const [_, error] = await safeTry(
|
||||
getHotelIdsByCountry({
|
||||
country: countryName,
|
||||
serviceToken: serviceToken.access_token,
|
||||
})
|
||||
)
|
||||
|
||||
if (error) {
|
||||
console.error(
|
||||
`[Warmup]: Error fetching hotel IDs for ${countryName}:`,
|
||||
error
|
||||
)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
status: "completed",
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
status: "error",
|
||||
error: error as Error,
|
||||
}
|
||||
}
|
||||
}
|
||||
20
apps/scandic-web/services/warmup/warmupKeys.ts
Normal file
20
apps/scandic-web/services/warmup/warmupKeys.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
const langs = Object.keys(Lang) as Lang[]
|
||||
|
||||
/*
|
||||
* Keys for warmup functions, the order of the keys is the order in which they will be executed
|
||||
*/
|
||||
export const warmupKeys = [
|
||||
...langs.map((lang) => `countries_${lang}` as const),
|
||||
"hotelsByCountry",
|
||||
...langs.map((lang) => `hotelData_${lang}` as const),
|
||||
] as const
|
||||
|
||||
export type WarmupFunctionsKey = (typeof warmupKeys)[number]
|
||||
|
||||
export function isWarmupKey(key: unknown): key is WarmupFunctionsKey {
|
||||
return (
|
||||
typeof key === "string" && warmupKeys.includes(key as WarmupFunctionsKey)
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user