Spaces:
Sleeping
Sleeping
Nathan commited on
Commit ·
ede1a98
1
Parent(s): 106bc0f
Fix: Added Cloud Scheduler & Corrected God Mode Limit detection
Browse filesFiles changed (2)
- server.js+28
- utils/autonomous-agent.js+17-15
650 unmodified lines651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681650 unmodified linesapp.listen(PORT, '0.0.0.0', () => { console.log(`🚀 Permanent Memory Server running on port ${PORT}`);});
// --- BACKGROUND SCHEDULER (Every 1 Minute) ---setInterval(async () => { try { const now = new Date(); // Get IST Time (matches dashboard clock) const istTime = now.toLocaleTimeString('en-GB', { timeZone: 'Asia/Kolkata', hour: '2-digit', minute: '2-digit' }); // 1. Fetch all active channels const { data: channels } = await db.from('channels').select('user_id, preferred_hours, last_run').eq('is_active', true); for (const channel of channels) { if (channel.preferred_hours && channel.preferred_hours.includes(istTime)) { // 2. Prevent double-triggering within the same minute/hour const lastRun = channel.last_run ? new Date(channel.last_run) : new Date(0); const diffMinutes = (now - lastRun) / (1000 * 60); if (diffMinutes > 10) { // Only run if it hasn't run in the last 10 mins console.log(`⏰ [Scheduler] Triggering Production for User ${channel.user_id} at ${istTime} IST`); const { runDailyAutomation } = require('./utils/autonomous-agent'); runDailyAutomation(channel.user_id).catch(err => console.error(`🔥 [Scheduler Error] User ${channel.user_id}:`, err)); } } } } catch (err) { console.error("🔥 [Scheduler Critical Failure]:", err.message); }}, 60000);29 unmodified lines30313233333435363738394041424344454647484950515253404142435455566 unmodified lines636465565758596061626366676824 unmodified lines93949594959697989910029 unmodified lines for (const channel of channels) { const currentUserId = channel.user_id;
// 🛡️ SECURITY: 1. Daily Limits Check // 🛡️ SECURITY: 1. Fetch User Data (Keys & Plan) const { data: userData } = await db.from('users') .select('custom_client_id, custom_client_secret, plan, role') .eq('id', currentUserId) .single();
if (!userData?.custom_client_id || !userData?.custom_client_secret) { await log(`❌ Production Paused: Please provide your own Google API keys in Advanced Settings to continue.`, currentUserId); continue; }
// ADMIN GOD-MODE & ULTRA: Define Limits const userPlan = (userData.role === 'admin' || userData.plan === 'ultra') ? 'ultra' : userData.plan; const limit = userPlan === 'ultra' ? 9999 : (userPlan === 'premium' ? 5 : 1);
const { count: videosToday } = await db.from('activity') .select('*', { count: 'exact', head: true }) .eq('user_id', currentUserId) .eq('type', 'VIDEO_PUBLISHED') .gte('created_at', new Date().toISOString().split('T')[0]);
// ADMIN GOD-MODE & ULTRA: Define Limits const userPlan = (channel.user_role === 'admin' || channel.user_plan === 'ultra') ? 'ultra' : channel.user_plan; const limit = userPlan === 'ultra' ? 9999 : (userPlan === 'premium' ? 5 : 1);
if (videosToday >= limit) { console.log(`[Limit] User ${currentUserId} reached ${limit} videos. Skipping.`); await log(`Limit reached (${limit}/day). Come back tomorrow! 🛑`, currentUserId);6 unmodified lines continue; }
// 3. Custom API Key Verification (FROM USERS TABLE) const { data: userKeys } = await db.from('users').select('custom_client_id, custom_client_secret').eq('id', currentUserId).single();
if (!userKeys?.custom_client_id || !userKeys?.custom_client_secret) { await log(`❌ Production Paused: Please provide your own Google API keys in Advanced Settings to continue.`, currentUserId); continue; }
// ACTIVATE LOCK await db.from('channels').update({ is_processing: true }).eq('id', channel.id);
24 unmodified lines description: result.metadata.description, hashtags: result.metadata.hashtags, token: channel.token, custom_client_id: userKeys.custom_client_id, custom_client_secret: userKeys.custom_client_secret custom_client_id: userData.custom_client_id, custom_client_secret: userData.custom_client_secret });
await log(`✅ Success! Published to YouTube: ${uploadResult.id}`, currentUserId);server.js+28
utils/autonomous-agent.js+17-15