Fix slower Zillow unit photo discovery path

This commit is contained in:
2026-03-28 02:28:30 -05:00
parent 7690dc259b
commit 8fe451e8d0
11 changed files with 167 additions and 49 deletions

View File

@@ -10,6 +10,8 @@ export interface ListingDiscoveryResult {
interface ListingDiscoveryDeps {
timeoutMs?: number;
zillowTimeoutMs?: number;
harTimeoutMs?: number;
discoverZillowListingFn?: typeof discoverZillowListing;
discoverHarListingFn?: typeof discoverHarListing;
}
@@ -17,61 +19,82 @@ interface ListingDiscoveryDeps {
const DEFAULT_DISCOVERY_TIMEOUT_MS = Number(
process.env.PROPERTY_ASSESSOR_DISCOVERY_TIMEOUT_MS || 20_000
);
const DEFAULT_ZILLOW_DISCOVERY_TIMEOUT_MS = Number(
process.env.PROPERTY_ASSESSOR_ZILLOW_DISCOVERY_TIMEOUT_MS || 60_000
);
const DEFAULT_HAR_DISCOVERY_TIMEOUT_MS = Number(
process.env.PROPERTY_ASSESSOR_HAR_DISCOVERY_TIMEOUT_MS || DEFAULT_DISCOVERY_TIMEOUT_MS
);
interface SourceDiscoveryOutcome {
source: "zillow" | "har";
url: string | null;
attempts: string[];
}
export async function discoverListingSources(
address: string,
deps: ListingDiscoveryDeps = {}
): Promise<ListingDiscoveryResult> {
const attempts: string[] = [];
let zillowUrl: string | null = null;
let harUrl: string | null = null;
const timeoutMs = deps.timeoutMs ?? DEFAULT_DISCOVERY_TIMEOUT_MS;
const zillowTimeoutMs =
deps.zillowTimeoutMs ??
(deps.timeoutMs != null ? timeoutMs : DEFAULT_ZILLOW_DISCOVERY_TIMEOUT_MS);
const harTimeoutMs =
deps.harTimeoutMs ??
(deps.timeoutMs != null ? timeoutMs : DEFAULT_HAR_DISCOVERY_TIMEOUT_MS);
const discoverZillowListingFn = deps.discoverZillowListingFn || discoverZillowListing;
const discoverHarListingFn = deps.discoverHarListingFn || discoverHarListing;
try {
const result = await withTimeout(
() => discoverZillowListingFn(address),
{
operationName: "Zillow discovery",
timeoutMs
const runSource = async (
source: "zillow" | "har",
timeoutForSourceMs: number,
operation: () => Promise<{ listingUrl: string | null; attempts: string[] }>
): Promise<SourceDiscoveryOutcome> => {
try {
const result = await withTimeout(operation, {
operationName: `${source === "zillow" ? "Zillow" : "HAR"} discovery`,
timeoutMs: timeoutForSourceMs
});
return {
source,
url: result.listingUrl,
attempts: result.attempts
};
} catch (error) {
if (error instanceof TimeoutError) {
return {
source,
url: null,
attempts: [
`${source === "zillow" ? "Zillow" : "HAR"} discovery timed out after ${timeoutForSourceMs}ms.`
]
};
}
);
zillowUrl = result.listingUrl;
attempts.push(...result.attempts);
} catch (error) {
if (error instanceof TimeoutError) {
attempts.push(`Zillow discovery timed out after ${timeoutMs}ms.`);
} else {
attempts.push(
`Zillow discovery failed: ${error instanceof Error ? error.message : String(error)}`
);
}
}
try {
const result = await withTimeout(
() => discoverHarListingFn(address),
{
operationName: "HAR discovery",
timeoutMs
}
);
harUrl = result.listingUrl;
attempts.push(...result.attempts);
} catch (error) {
if (error instanceof TimeoutError) {
attempts.push(`HAR discovery timed out after ${timeoutMs}ms.`);
} else {
attempts.push(
`HAR discovery failed: ${error instanceof Error ? error.message : String(error)}`
);
return {
source,
url: null,
attempts: [
`${source === "zillow" ? "Zillow" : "HAR"} discovery failed: ${error instanceof Error ? error.message : String(error)}`
]
};
}
}
};
const zillowPromise = runSource("zillow", zillowTimeoutMs, () =>
discoverZillowListingFn(address, { timeoutMs: zillowTimeoutMs })
);
const harPromise = runSource("har", harTimeoutMs, () =>
discoverHarListingFn(address, { timeoutMs: harTimeoutMs })
);
const [zillowResult, harResult] = await Promise.all([zillowPromise, harPromise]);
const attempts = [...zillowResult.attempts, ...harResult.attempts];
return {
attempts,
zillowUrl,
harUrl
zillowUrl: zillowResult.url,
harUrl: harResult.url
};
}