mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 00:25:38 -04:00
fix: misc bug fixes and reliability improvements
- Fix `refreshCreditsJob` to treat `null` `lastFetchedAt` as needing refresh (not just missing cast entry) - Fix `buildBackupCron` 6h expression to correctly fire every 6 hours using explicit hour list instead of `*/6` - Fix `vacuumDatabase` to pass the path directly as a bound parameter (the manual escape was redundant and incorrect) - Clean up orphaned `.tmp` files when image cache writes fail in both `downloadAndCacheImage` and `fetchAndMaybeCache` - Surface import job payload parse/validation errors as job-level `error` status instead of throwing unhandled exceptions; check for cancellation before the processing loop starts - Replace bare `Error` throws in `readImportJob` with `ORPCError` (`NOT_FOUND` / `FORBIDDEN`) so oRPC maps them to correct HTTP status codes - Auto-promote title status to `in_progress` when `logEpisodeWatchBatch` is called for titles that have no status or are only on the watchlist - Refresh seasons from TMDB when a webhook references a season not yet in the DB (handles newly-released seasons) - Upgrade fire-and-forget enrichment failures and webhook processing errors from `log.debug` → `log.warn` so they surface in production logs
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { mkdir, rename } from "node:fs/promises";
|
||||
import { mkdir, rename, unlink } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import { CACHE_DIR, TMDB_IMAGE_BASE_URL } from "@sofa/config";
|
||||
@@ -101,6 +101,7 @@ export async function downloadAndCacheImage(
|
||||
log.debug(`Cached ${category}/${filename} (${buffer.length} bytes)`);
|
||||
} catch (err) {
|
||||
log.warn(`Failed to write cached image ${filename}:`, err);
|
||||
unlink(tmpPath).catch(() => {});
|
||||
}
|
||||
|
||||
return buffer;
|
||||
@@ -132,7 +133,10 @@ export async function fetchAndMaybeCache(
|
||||
const tmpPath = `${finalPath}.tmp.${Date.now()}`;
|
||||
Bun.write(tmpPath, buffer)
|
||||
.then(() => rename(tmpPath, finalPath))
|
||||
.catch((err) => log.warn(`Failed to cache ${category}/${filename}:`, err));
|
||||
.catch((err) => {
|
||||
log.warn(`Failed to cache ${category}/${filename}:`, err);
|
||||
unlink(tmpPath).catch(() => {});
|
||||
});
|
||||
|
||||
return { buffer, contentType };
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ORPCError } from "@orpc/server";
|
||||
|
||||
import { type ImportJob, NormalizedImportSchema } from "@sofa/api/schemas";
|
||||
import {
|
||||
getImportJob,
|
||||
@@ -254,11 +256,11 @@ export function readImportJob(jobId: string, userId?: string): ImportJob {
|
||||
const row = getImportJob(jobId);
|
||||
|
||||
if (!row) {
|
||||
throw new Error(`Import job ${jobId} not found`);
|
||||
throw new ORPCError("NOT_FOUND", { message: `Import job ${jobId} not found` });
|
||||
}
|
||||
|
||||
if (userId && row.userId !== userId) {
|
||||
throw new Error("Not authorized");
|
||||
throw new ORPCError("FORBIDDEN", { message: "Not authorized" });
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -285,18 +287,33 @@ export async function processImportJob(jobId: string): Promise<void> {
|
||||
const row = getImportJob(jobId);
|
||||
|
||||
if (!row) {
|
||||
throw new Error(`Import job ${jobId} not found`);
|
||||
log.error(`Import job ${jobId} not found`);
|
||||
return;
|
||||
}
|
||||
|
||||
let rawPayload: unknown;
|
||||
try {
|
||||
rawPayload = JSON.parse(row.payload);
|
||||
} catch {
|
||||
throw new Error(`Import job ${jobId} has invalid JSON payload`);
|
||||
updateImportJobProgress(jobId, {
|
||||
status: "error",
|
||||
finishedAt: new Date(),
|
||||
errors: JSON.stringify([`Import job ${jobId} has invalid JSON payload`]),
|
||||
currentMessage: "Import failed",
|
||||
});
|
||||
log.error(`Import job ${jobId} has invalid JSON payload`);
|
||||
return;
|
||||
}
|
||||
const parsed = NormalizedImportSchema.safeParse(rawPayload);
|
||||
if (!parsed.success) {
|
||||
throw new Error(`Import job ${jobId} has malformed payload: ${parsed.error.message}`);
|
||||
updateImportJobProgress(jobId, {
|
||||
status: "error",
|
||||
finishedAt: new Date(),
|
||||
errors: JSON.stringify([`Malformed payload: ${parsed.error.message}`]),
|
||||
currentMessage: "Import failed",
|
||||
});
|
||||
log.error(`Import job ${jobId} has malformed payload: ${parsed.error.message}`);
|
||||
return;
|
||||
}
|
||||
const data = parsed.data;
|
||||
const result: ImportResult = {
|
||||
@@ -343,6 +360,18 @@ export async function processImportJob(jobId: string): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the job was cancelled while we were preparing
|
||||
const preStartStatus = getImportJobStatus(jobId);
|
||||
if (preStartStatus?.status === "cancelled") {
|
||||
updateImportJobProgress(jobId, {
|
||||
finishedAt: new Date(),
|
||||
totalItems: total,
|
||||
currentMessage: "Import cancelled",
|
||||
});
|
||||
log.info(`Import job ${jobId} was cancelled before processing started`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Set status to running + totalItems atomically
|
||||
updateImportJobProgress(jobId, {
|
||||
status: "running",
|
||||
|
||||
@@ -118,15 +118,15 @@ function fireAndForgetEnrichment(
|
||||
backdropPath: string | null | undefined,
|
||||
type: "movie" | "tv",
|
||||
) {
|
||||
refreshAvailability(titleId).catch((err) => log.debug("Availability enrichment failed:", err));
|
||||
refreshAvailability(titleId).catch((err) => log.warn("Availability enrichment failed:", err));
|
||||
refreshRecommendations(titleId).catch((err) =>
|
||||
log.debug("Recommendations enrichment failed:", err),
|
||||
log.warn("Recommendations enrichment failed:", err),
|
||||
);
|
||||
syncTitleArt(titleId, posterPath, backdropPath, type).catch((err) =>
|
||||
log.debug("Cache/thumbhash failed:", err),
|
||||
log.warn("Cache/thumbhash failed:", err),
|
||||
);
|
||||
refreshCredits(titleId).catch((err) => log.debug("Credits enrichment failed:", err));
|
||||
refreshTrailer(titleId).catch((err) => log.debug("Trailer enrichment failed:", err));
|
||||
refreshCredits(titleId).catch((err) => log.warn("Credits enrichment failed:", err));
|
||||
refreshTrailer(titleId).catch((err) => log.warn("Trailer enrichment failed:", err));
|
||||
}
|
||||
|
||||
type ImportResult = ReturnType<typeof _getOrFetchTitleByTmdbId>;
|
||||
|
||||
@@ -88,6 +88,19 @@ export function logEpisodeWatchBatch(
|
||||
if (episodeIds.length === 0) return;
|
||||
|
||||
batchInsertEpisodeWatchesTransaction(userId, episodeIds, source, watchedAt);
|
||||
|
||||
// Auto-set title status to in_progress for affected titles
|
||||
const titleIds = new Set<string>();
|
||||
for (const episodeId of episodeIds) {
|
||||
const titleId = getEpisodeTitleId(episodeId);
|
||||
if (titleId) titleIds.add(titleId);
|
||||
}
|
||||
for (const titleId of titleIds) {
|
||||
const existing = getTitleStatus(userId, titleId);
|
||||
if (!existing || existing.status === "watchlist") {
|
||||
setTitleStatus(userId, titleId, "in_progress", source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function markAllEpisodesWatched(
|
||||
|
||||
@@ -6,9 +6,10 @@ import {
|
||||
updateIntegrationLastEvent,
|
||||
} from "@sofa/db/queries/webhooks";
|
||||
import { createLogger } from "@sofa/logger";
|
||||
import { getTvDetails } from "@sofa/tmdb/client";
|
||||
|
||||
import { resolveMovieTmdbId, resolveShowTmdbId } from "./imports/resolve";
|
||||
import { getOrFetchTitleByTmdbId } from "./metadata";
|
||||
import { getOrFetchTitleByTmdbId, refreshTvChildren } from "./metadata";
|
||||
import { logEpisodeWatch, logMovieWatch } from "./tracking";
|
||||
|
||||
const log = createLogger("webhooks");
|
||||
@@ -265,7 +266,18 @@ export async function processWebhook(
|
||||
}
|
||||
|
||||
// Find the episode in our DB
|
||||
const season = findSeasonByTitleAndNumber(title.id, resolved.seasonNumber);
|
||||
let season = findSeasonByTitleAndNumber(title.id, resolved.seasonNumber);
|
||||
|
||||
if (!season) {
|
||||
// Season might be newly released — try refreshing from TMDB
|
||||
try {
|
||||
const show = await getTvDetails(resolved.showTmdbId);
|
||||
await refreshTvChildren(title.id, resolved.showTmdbId, show.number_of_seasons);
|
||||
season = findSeasonByTitleAndNumber(title.id, resolved.seasonNumber);
|
||||
} catch (err) {
|
||||
log.warn(`Failed to refresh seasons for TMDB ${resolved.showTmdbId}:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
if (!season) {
|
||||
logEvent(connectionId, event, "error", `Season ${resolved.seasonNumber} not found`);
|
||||
|
||||
@@ -89,7 +89,7 @@ export function optimizeDatabase() {
|
||||
}
|
||||
|
||||
export function vacuumDatabase(into: string): void {
|
||||
getClient().run("VACUUM INTO ?", [into.replace(/'/g, "''")]);
|
||||
getClient().run("VACUUM INTO ?", [into]);
|
||||
}
|
||||
|
||||
export function isDatabaseAccessBlocked(): boolean {
|
||||
|
||||
Reference in New Issue
Block a user