Files
dragonsstash/worker/src/index.ts
admin ab558e00f5 feat: add preview management, channel controls, invite polish, and recovery
- Auto-extract preview images from ZIP/RAR/7z archives during ingestion
- Upload custom preview images via package drawer
- Select preview from archive contents with on-demand extraction UI
- Manually add Telegram channels by t.me link, username, or invite link
- Invite code UX: bulk create, copy link, usage tracking, delete confirm
- Incomplete upload recovery: verify dest messages on worker startup
- Rebuild package DB by scanning destination channel with live progress

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 00:09:59 +01:00

68 lines
2.1 KiB
TypeScript

import { mkdir } from "fs/promises";
import { config } from "./util/config.js";
import { logger } from "./util/logger.js";
import { markStaleRunsAsFailed } from "./db/queries.js";
import { cleanupTempDir } from "./worker.js";
import { recoverIncompleteUploads } from "./recovery.js";
import { startScheduler, stopScheduler } from "./scheduler.js";
import { startFetchListener, stopFetchListener } from "./fetch-listener.js";
import { db, pool } from "./db/client.js";
const log = logger.child({ module: "main" });
async function main(): Promise<void> {
log.info("DragonsStash Telegram Worker starting");
log.info({ config: { ...config, databaseUrl: "***" } }, "Configuration loaded");
if (!config.telegramApiId || !config.telegramApiHash) {
log.fatal("TELEGRAM_API_ID and TELEGRAM_API_HASH are both required");
process.exit(1);
}
// Ensure temp directory exists
await mkdir(config.tempDir, { recursive: true });
await mkdir(config.tdlibStateDir, { recursive: true });
// Clean up stale state
await cleanupTempDir();
await markStaleRunsAsFailed();
// Verify destination messages exist for all "uploaded" packages.
// Resets any packages whose dest message is missing so they get re-processed.
await recoverIncompleteUploads();
// Start the fetch listener (pg_notify for on-demand channel fetching)
await startFetchListener();
// Start the scheduler
await startScheduler();
}
// Graceful shutdown
function shutdown(signal: string): void {
log.info({ signal }, "Shutdown signal received");
// Stop accepting new work
stopFetchListener();
// Wait for any active cycle to finish before closing DB
stopScheduler()
.then(() => Promise.all([db.$disconnect(), pool.end()]))
.then(() => {
log.info("Shutdown complete");
process.exit(0);
})
.catch((err) => {
log.error({ err }, "Error during shutdown");
process.exit(1);
});
}
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("SIGINT", () => shutdown("SIGINT"));
main().catch((err) => {
log.fatal({ err }, "Worker failed to start");
process.exit(1);
});