Added instrumentation.ts that reads persisted watcher state from the database and restarts active watchers when the Next.js server boots. Also added imapflow/mailparser to serverExternalPackages. Made-with: Cursor
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
export async function register() {
|
|
// Only run on the Node.js server runtime, not during build or in the edge runtime
|
|
if (process.env.NEXT_RUNTIME !== "nodejs") return;
|
|
|
|
const { prisma } = await import("@/lib/db");
|
|
|
|
let settings;
|
|
try {
|
|
settings = await prisma.appSettings.findUnique({
|
|
where: { id: "singleton" },
|
|
});
|
|
} catch (err) {
|
|
console.error("[instrumentation] Could not read settings:", err);
|
|
return;
|
|
}
|
|
|
|
if (!settings) return;
|
|
|
|
// Resume email monitoring
|
|
if (
|
|
settings.emailWatching &&
|
|
settings.emailImapHost &&
|
|
settings.emailImapUser &&
|
|
settings.emailImapPass
|
|
) {
|
|
try {
|
|
const { startEmailWatching } = await import("@/lib/email-watcher");
|
|
await startEmailWatching({
|
|
host: settings.emailImapHost,
|
|
port: settings.emailImapPort,
|
|
user: settings.emailImapUser,
|
|
pass: settings.emailImapPass,
|
|
tls: settings.emailImapTls,
|
|
folder: settings.emailFolder,
|
|
processedAction: settings.emailProcessed,
|
|
processedFolder: settings.emailProcessedFolder,
|
|
});
|
|
console.log("[instrumentation] Email monitoring resumed");
|
|
} catch (err) {
|
|
console.error("[instrumentation] Failed to resume email monitoring:", err);
|
|
}
|
|
}
|
|
|
|
// Resume folder watching
|
|
if (settings.watching && settings.watchDir) {
|
|
try {
|
|
const { startWatching } = await import("@/lib/watcher");
|
|
await startWatching(settings.watchDir);
|
|
console.log("[instrumentation] Folder watching resumed");
|
|
} catch (err) {
|
|
console.error("[instrumentation] Failed to resume folder watching:", err);
|
|
}
|
|
}
|
|
}
|