|
| 1 | +import pRetry from "p-retry"; |
1 | 2 | import { $replica } from "~/db.server"; |
2 | 3 | import { env } from "~/env.server"; |
| 4 | +import { logger } from "~/services/logger.server"; |
3 | 5 | import { signalsEmitter } from "~/services/signals.server"; |
4 | 6 | import { singleton } from "~/utils/singleton"; |
5 | 7 | import { OrganizationDataStoresRegistry } from "./organizationDataStoresRegistry.server"; |
6 | 8 |
|
7 | 9 | export const organizationDataStoresRegistry = singleton("organizationDataStoresRegistry", () => { |
8 | 10 | const registry = new OrganizationDataStoresRegistry($replica); |
9 | 11 |
|
10 | | - registry.loadFromDatabase().catch((err) => { |
11 | | - console.error("[OrganizationDataStoresRegistry] Failed to initialize", err); |
| 12 | + // Runs as soon as this singleton is created (first import of this module). The |
| 13 | + // registry’s `isReady` promise resolves when this eventually succeeds. |
| 14 | + const startupLoadPromise = pRetry(() => registry.loadFromDatabase(), { |
| 15 | + forever: true, |
| 16 | + retries: 10, |
| 17 | + minTimeout: 1_000, |
| 18 | + maxTimeout: 60_000, |
| 19 | + factor: 2, |
| 20 | + onFailedAttempt: (error) => { |
| 21 | + logger.warn("[OrganizationDataStoresRegistry] Startup load failed, retrying", { |
| 22 | + attemptNumber: error.attemptNumber, |
| 23 | + retriesLeft: error.retriesLeft, |
| 24 | + error: error.message, |
| 25 | + }); |
| 26 | + }, |
| 27 | + }); |
| 28 | + startupLoadPromise.catch((err) => { |
| 29 | + console.error("[OrganizationDataStoresRegistry] Unexpected startup load failure", err); |
12 | 30 | }); |
13 | 31 |
|
14 | 32 | const interval = setInterval(() => { |
|
0 commit comments