fix: auth scheduler env vars, concurrency and browser stability

This commit is contained in:
Giancarmine Salucci
2025-12-21 02:15:22 +01:00
parent 9357bd483a
commit 342a8eb259
9 changed files with 420 additions and 79 deletions

View File

@@ -1,10 +1,11 @@
import fs from 'fs';
import path from 'path';
import { getBrowser } from './browser';
import { env } from '$env/dynamic/private';
export interface SchedulerConfig {
enabled: boolean;
intervalHours: number;
intervalMinutes: number;
}
interface SchedulerState {
@@ -23,12 +24,19 @@ const state: SchedulerState = {
* Get scheduler configuration from environment variables
*/
function getConfig(): SchedulerConfig {
const enabled = process.env.AUTH_SCHEDULER_ENABLED === 'true';
const intervalHours = parseInt(process.env.AUTH_SCHEDULER_INTERVAL_HOURS || '12', 10);
const enabled = env.AUTH_SCHEDULER_ENABLED === 'true';
let intervalMinutes = parseInt(env.AUTH_SCHEDULER_INTERVAL_MINUTES || '720', 10);
if (isNaN(intervalMinutes) || intervalMinutes < 15) {
console.warn(
`[Scheduler] Invalid or too short interval '${env.AUTH_SCHEDULER_INTERVAL_MINUTES}'. Defaulting to 720 minutes.`
);
intervalMinutes = 720;
}
return {
enabled,
intervalHours
intervalMinutes
};
}
@@ -70,14 +78,17 @@ async function renewInstagramAuth(): Promise<boolean> {
state.isRenewing = true;
let context = null;
let page = null;
try {
console.log('[Scheduler] Starting Instagram authentication renewal...');
console.log(`[Scheduler] Loading existing auth from: ${authPath}`);
const browser = await getBrowser();
// Load existing authentication state
const context = await browser.newContext({ storageState: authPath });
const page = await context.newPage();
context = await browser.newContext({ storageState: authPath });
page = await context.newPage();
// Navigate to Instagram homepage - the existing auth will be used automatically
await page.goto('https://www.instagram.com/', { waitUntil: 'domcontentloaded' });
@@ -88,9 +99,6 @@ async function renewInstagramAuth(): Promise<boolean> {
console.log('[Scheduler] Successfully authenticated with Instagram');
} catch (e) {
console.warn('[Scheduler] Home icon not found - session may be expired or invalid');
await page.close();
await context.close();
state.isRenewing = false;
return false;
}
@@ -105,9 +113,6 @@ async function renewInstagramAuth(): Promise<boolean> {
// Update auth.json with refreshed session
await context.storageState({ path: authPath });
await page.close();
await context.close();
state.lastRenewalTime = Date.now();
console.log(`[Scheduler] Instagram authentication renewed successfully at ${new Date().toISOString()}`);
console.log(`[Scheduler] Auth state updated at: ${authPath}`);
@@ -117,6 +122,12 @@ async function renewInstagramAuth(): Promise<boolean> {
console.error('[Scheduler] Instagram authentication renewal failed:', error);
return false;
} finally {
if (page) {
await page.close().catch(() => {});
}
if (context) {
await context.close().catch(() => {});
}
state.isRenewing = false;
}
}
@@ -137,9 +148,9 @@ export async function startScheduler(): Promise<void> {
return;
}
const intervalMs = config.intervalHours * 60 * 60 * 1000;
const intervalMs = config.intervalMinutes * 60 * 1000;
console.log(`[Scheduler] Starting authentication scheduler with ${config.intervalHours}h interval`);
console.log(`[Scheduler] Starting authentication scheduler with ${config.intervalMinutes}min interval`);
// Schedule periodic renewals
state.intervalId = setInterval(async () => {