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 lines651652653650 unmodified linesapp.listen(PORT, '0.0.0.0', () => {console.log(`π Permanent Memory Server running on port ${PORT}`);});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 channelsconst { 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/hourconst 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 minsconsole.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 lines30313233343536373839404142434445466 unmodified lines535455565758596061626364656624 unmodified lines919293949596979829 unmodified linesfor (const channel of channels) {const currentUserId = channel.user_id;// π‘οΈ SECURITY: 1. Daily Limits Checkconst { 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 Limitsconst 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 linescontinue;}// 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 LOCKawait db.from('channels').update({ is_processing: true }).eq('id', channel.id);24 unmodified linesdescription: result.metadata.description,hashtags: result.metadata.hashtags,token: channel.token,custom_client_id: userKeys.custom_client_id,custom_client_secret: userKeys.custom_client_secret});await log(`β Success! Published to YouTube: ${uploadResult.id}`, currentUserId);29 unmodified lines3031323334353637383940414243444546474849505152535455566 unmodified lines63646566676824 unmodified lines9394959697989910029 unmodified linesfor (const channel of channels) {const currentUserId = channel.user_id;// π‘οΈ 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 Limitsconst 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]);if (videosToday >= limit) {console.log(`[Limit] User ${currentUserId} reached ${limit} videos. Skipping.`);await log(`Limit reached (${limit}/day). Come back tomorrow! π`, currentUserId);6 unmodified linescontinue;}// ACTIVATE LOCKawait db.from('channels').update({ is_processing: true }).eq('id', channel.id);24 unmodified linesdescription: result.metadata.description,hashtags: result.metadata.hashtags,token: channel.token,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