Auto-resume email and folder watchers on server startup
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
This commit is contained in:
parent
d56b1a27ec
commit
6cef79309b
2 changed files with 55 additions and 1 deletions
|
|
@ -14,7 +14,7 @@ const nextConfig: NextConfig = {
|
|||
},
|
||||
],
|
||||
},
|
||||
serverExternalPackages: ["sharp", "pdf2pic", "chokidar", "pg"],
|
||||
serverExternalPackages: ["sharp", "pdf2pic", "chokidar", "pg", "imapflow", "mailparser"],
|
||||
};
|
||||
|
||||
export default nextConfig;
|
||||
|
|
|
|||
54
src/instrumentation.ts
Normal file
54
src/instrumentation.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue