11import { ModelPricingRegistry , seedLlmPricing } from "@internal/llm-pricing" ;
22import { prisma , $replica } from "~/db.server" ;
33import { env } from "~/env.server" ;
4+ import { signalsEmitter } from "~/services/signals.server" ;
45import { singleton } from "~/utils/singleton" ;
56import { setLlmPricingRegistry } from "./utils/enrichCreatableEvents.server" ;
67
78async function initRegistry ( registry : ModelPricingRegistry ) {
89 if ( env . LLM_PRICING_SEED_ON_STARTUP ) {
9- const result = await seedLlmPricing ( prisma ) ;
10+ await seedLlmPricing ( prisma ) ;
1011 }
1112
1213 await registry . loadFromDatabase ( ) ;
@@ -28,15 +29,34 @@ export const llmPricingRegistry = singleton("llmPricingRegistry", () => {
2829
2930 // Periodic reload
3031 const reloadInterval = env . LLM_PRICING_RELOAD_INTERVAL_MS ;
31- setInterval ( ( ) => {
32- registry
33- . reload ( )
34- . then ( ( ) => {
35- } )
36- . catch ( ( err ) => {
37- console . error ( "Failed to reload LLM pricing registry" , err ) ;
38- } ) ;
32+ const interval = setInterval ( ( ) => {
33+ registry . reload ( ) . catch ( ( err ) => {
34+ console . error ( "Failed to reload LLM pricing registry" , err ) ;
35+ } ) ;
3936 } , reloadInterval ) ;
4037
38+ signalsEmitter . on ( "SIGTERM" , ( ) => {
39+ clearInterval ( interval ) ;
40+ } ) ;
41+ signalsEmitter . on ( "SIGINT" , ( ) => {
42+ clearInterval ( interval ) ;
43+ } ) ;
44+
4145 return registry ;
4246} ) ;
47+
48+ /**
49+ * Wait for the LLM pricing registry to finish its initial load, with a timeout.
50+ * After the first call resolves (or times out), subsequent calls are no-ops.
51+ */
52+ export async function waitForLlmPricingReady ( ) : Promise < void > {
53+ if ( ! llmPricingRegistry || llmPricingRegistry . isLoaded ) return ;
54+
55+ const timeoutMs = env . LLM_PRICING_READY_TIMEOUT_MS ;
56+ if ( timeoutMs <= 0 ) return ;
57+
58+ await Promise . race ( [
59+ llmPricingRegistry . isReady ,
60+ new Promise < void > ( ( resolve ) => setTimeout ( resolve , timeoutMs ) ) ,
61+ ] ) ;
62+ }
0 commit comments