fix: commit pre-compiled dist and simplify sync Dockerfile
Server npm proxy silently drops devDependencies, making TypeScript unavailable in Docker. Solution: compile locally and commit dist/. Dockerfile now only needs prod deps + Playwright, both of which install cleanly via the public registry. Also fix TS2688/TS2304 errors: add DOM to tsconfig lib and cast querySelectorAll results to Element inside $$eval callbacks. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
60
sync/dist/index.js
vendored
Normal file
60
sync/dist/index.js
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
require("dotenv/config");
|
||||
const pg_1 = require("pg");
|
||||
const scraper_1 = require("./scraper");
|
||||
const db_1 = require("./db");
|
||||
function log(msg) {
|
||||
console.log(`[sync] ${new Date().toISOString()} ${msg}`);
|
||||
}
|
||||
function requireEnv(name) {
|
||||
const val = process.env[name];
|
||||
if (!val)
|
||||
throw new Error(`Missing required environment variable: ${name}`);
|
||||
return val;
|
||||
}
|
||||
/** Returns milliseconds until the next midnight (00:00:00) in local time. */
|
||||
function msUntilMidnight() {
|
||||
const now = new Date();
|
||||
const midnight = new Date(now);
|
||||
midnight.setDate(now.getDate() + 1);
|
||||
midnight.setHours(0, 0, 0, 0);
|
||||
return midnight.getTime() - now.getTime();
|
||||
}
|
||||
async function runSync() {
|
||||
const username = requireEnv('FDISK_USERNAME');
|
||||
const password = requireEnv('FDISK_PASSWORD');
|
||||
const pool = new pg_1.Pool({
|
||||
host: requireEnv('DB_HOST'),
|
||||
port: parseInt(process.env.DB_PORT ?? '5432'),
|
||||
database: requireEnv('DB_NAME'),
|
||||
user: requireEnv('DB_USER'),
|
||||
password: requireEnv('DB_PASSWORD'),
|
||||
});
|
||||
try {
|
||||
log('Starting FDISK sync');
|
||||
const { members, ausbildungen } = await (0, scraper_1.scrapeAll)(username, password);
|
||||
await (0, db_1.syncToDatabase)(pool, members, ausbildungen);
|
||||
log(`Sync complete — ${members.length} members, ${ausbildungen.length} Ausbildungen`);
|
||||
}
|
||||
finally {
|
||||
await pool.end();
|
||||
}
|
||||
}
|
||||
async function main() {
|
||||
log('FDISK sync service started');
|
||||
// Run once immediately on startup so the first sync doesn't wait until midnight
|
||||
await runSync().catch(err => log(`ERROR during initial sync: ${err.message}`));
|
||||
// Then schedule at midnight every day
|
||||
while (true) {
|
||||
const delay = msUntilMidnight();
|
||||
const nextRun = new Date(Date.now() + delay);
|
||||
log(`Next sync scheduled at ${nextRun.toLocaleString()} (in ${Math.round(delay / 60000)} min)`);
|
||||
await new Promise(r => setTimeout(r, delay));
|
||||
await runSync().catch(err => log(`ERROR during scheduled sync: ${err.message}`));
|
||||
}
|
||||
}
|
||||
main().catch(err => {
|
||||
console.error(`[sync] Fatal error: ${err.message}`);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user