Skip to content

Commit

Permalink
logging and backend generation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgsolomon committed Oct 29, 2024
1 parent 672b055 commit 130c2e6
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 162 deletions.
63 changes: 41 additions & 22 deletions generate/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import { promisify } from 'util';

const execP = promisify(exec);

const agents = [
'BARACK_OBAMA',
'BEN_SHAPIRO',
'JORDAN_PETERSON',
'JOE_ROGAN',
'DONALD_TRUMP',
];

dotenv.config();

async function cleanupResources() {
Expand Down Expand Up @@ -53,12 +45,22 @@ async function mainFn(
cleanSrt,
credits
) {
console.log('🚀 Starting mainFn with:', {
topic,
agentA,
agentB,
videoId,
userId,
});
try {
console.log('📝 Updating process_id in pending-videos...');
await query(
'UPDATE `pending-videos` SET process_id = ? WHERE video_id = ?',
[PROCESS_ID, videoId]
);
console.log('✅ Process ID updated successfully');

console.log('🎙️ Starting transcription function...');
await transcribeFunction(
local,
topic,
Expand All @@ -72,72 +74,88 @@ async function mainFn(
cleanSrt,
videoId
);
console.log('✅ Transcription completed successfully');

console.log('📊 Updating status to Deploying assets...');
await query(
"UPDATE `pending-videos` SET status = 'Deploying assets to S3 for render', progress = 50 WHERE video_id = ?",
[videoId]
);
console.log('✅ Status updated to Deploying assets');

console.log('Building project with npm...');

console.log('🏗️ Building project with npm...');
const { stdout, stderr } = await execP(
'npx remotion lambda sites create src/index.ts --site-name=brainrot'
);
console.log(`stdout: ${stdout}`);
if (stderr) console.error(`stderr: ${stderr}`);
console.log(`stdout: ${stdout}`);
if (stderr) console.error(`stderr: ${stderr}`);
console.log('📤 Build stdout:', stdout);
if (stderr) console.error('⚠️ Build stderr:', stderr);

console.log('🔍 Searching for serve URL in output...');
const regexServeUrl =
/https:\/\/[\w-]+\.s3\.us-east-1\.amazonaws\.com\/sites\/[\w-]+\/index\.html/;

const matchServeUrl = stdout.match(regexServeUrl);
console.log('🌐 Serve URL found:', matchServeUrl);

console.log('Serve URL: ' + matchServeUrl);

console.log('📊 Updating status to Rendering video...');
await query(
"UPDATE `pending-videos` SET status = 'Rendering video on lambda functions', progress = 75 WHERE video_id = ?",
[videoId]
);
console.log('✅ Status updated to Rendering video');

console.log('🎬 Starting video render...');
const { stdout: stdoutRender, stderr: stderrRender } = await execP(
'npx remotion lambda render https://remotionlambda-useast1-oaz2rkh49x.s3.us-east-1.amazonaws.com/sites/brainrot/index.html Video'
);
console.log('📤 Render stdout:', stdoutRender);
if (stderrRender) console.error('⚠️ Render stderr:', stderrRender);

console.log('🔍 Searching for S3 URL in render output...');
const regex =
/https:\/\/s3\.us-east-1\.amazonaws\.com\/[\w-]+\/renders\/[\w-]+\/out\.mp4/;
const match = stdoutRender.match(regex);

let s3Url = '';
if (match) {
s3Url = match[0];
console.log(s3Url);
console.log('🎥 S3 URL found:', s3Url);
} else {
console.error('❌ No S3 URL found in output');
throw new Error('No S3 URL found in the output');
}

console.log('🧹 Cleaning up resources...');
await cleanupResources();
console.log('✅ Resources cleaned up');

console.log('💾 Inserting video record into database...');
await query(
`INSERT INTO videos (user_id, agent1, agent2, title, url, video_id) VALUES (?, ?, ?, ?, ?, ?)`,
[userId, agentA, agentB, topic, s3Url, videoId]
);
console.log('✅ Video record inserted');

console.log('📊 Updating final status to COMPLETED...');
await query(
"UPDATE `pending-videos` SET status = 'COMPLETED', progress = 100 WHERE video_id = ?",
[videoId]
);
console.log('✅ Process completed successfully');
} catch (error) {
console.error(`exec error: ${error}`);
console.error('❌ Error in mainFn:', error);
console.log('🧹 Starting cleanup after error...');
await cleanupResources();
console.log('📊 Updating status to ERROR...');
await query(
"UPDATE `pending-videos` SET status = 'ERROR' WHERE video_id = ?",
[videoId]
);
console.log('💰 Refunding credits...');
await query(
'UPDATE `brainrot-users` SET credits = credits + ? WHERE id = ?',
[credits, userId]
);
console.log('✅ Error handling completed');
}
}

Expand All @@ -156,6 +174,7 @@ async function pollPendingVideos() {
console.log('Found pending video:', rows[0]);
const video = rows[0];
try {
console.log('🚀 Starting mainFn...');
await mainFn(
video.title,
video.agent1,
Expand Down Expand Up @@ -183,10 +202,10 @@ async function pollPendingVideos() {
);
}
} else {
console.log('No pending videos found, sleeping for 15 seconds...');
await sleep(15000);
console.log('No pending videos found, sleeping for 5 seconds...');
await sleep(5000);
}
await sleep(15000);
await sleep(5000);
}
}

Expand Down
Loading

0 comments on commit 130c2e6

Please sign in to comment.