@@ -42,43 +42,71 @@ export class TranscriptionController {
4242 const fileBlob = this . createFileBlob ( req . file ) ;
4343 console . log ( `✅ File blob created successfully` ) ;
4444
45- // OpenAI transcription
45+ // OpenAI transcription with retry logic
4646 let rawTranscriptionText ;
47- try {
48- console . log ( "🎙️ Starting OpenAI transcription..." ) ;
49- const transcriptionStartTime = Date . now ( ) ;
50- rawTranscriptionText = await openAIService . transcribeAudio ( fileBlob ) ;
51- const transcriptionDuration = Date . now ( ) - transcriptionStartTime ;
52-
53- console . log ( "✅ OpenAI transcription completed successfully" ) ;
54- console . log ( `⏱️ Transcription took: ${ transcriptionDuration } ms` ) ;
55- console . log ( `📝 Transcription length: ${ rawTranscriptionText . length } characters` ) ;
56- console . log ( "📄 Original Transcription:" , rawTranscriptionText ) ;
57- } catch ( error ) {
58- console . error ( "❌ Error during OpenAI transcription:" , error . message ) ;
59- console . error ( "Stack trace:" , error . stack ) ;
60- return res . status ( 500 ) . json ( {
61- error : "Failed to transcribe audio using OpenAI service" ,
62- details : error . message
63- } ) ;
47+ const maxRetries = 5 ;
48+ const retryDelay = 3000 ; // 1 second delay between retries
49+
50+ for ( let attempt = 1 ; attempt <= maxRetries ; attempt ++ ) {
51+ try {
52+ console . log ( `🎙️ Starting OpenAI transcription (attempt ${ attempt } /${ maxRetries } )...` ) ;
53+ const transcriptionStartTime = Date . now ( ) ;
54+ rawTranscriptionText = await openAIService . transcribeAudio ( fileBlob ) ;
55+ const transcriptionDuration = Date . now ( ) - transcriptionStartTime ;
56+
57+ console . log ( "✅ OpenAI transcription completed successfully" ) ;
58+ console . log ( `⏱️ Transcription took: ${ transcriptionDuration } ms` ) ;
59+ console . log ( `📝 Transcription length: ${ rawTranscriptionText . length } characters` ) ;
60+ console . log ( "📄 Original Transcription:" , rawTranscriptionText ) ;
61+ break ; // Success, exit retry loop
62+ } catch ( error ) {
63+ console . error ( `❌ Error during OpenAI transcription (attempt ${ attempt } /${ maxRetries } ):` , error . message ) ;
64+
65+ if ( attempt === maxRetries ) {
66+ console . error ( "💥 All transcription attempts failed" ) ;
67+ console . error ( "Stack trace:" , error . stack ) ;
68+ return res . status ( 500 ) . json ( {
69+ error : "Failed to transcribe audio using OpenAI service after multiple attempts" ,
70+ details : error . message ,
71+ attempts : maxRetries
72+ } ) ;
73+ }
74+
75+ console . log ( `⏳ Waiting ${ retryDelay } ms before retry attempt ${ attempt + 1 } ...` ) ;
76+ await new Promise ( resolve => setTimeout ( resolve , retryDelay ) ) ;
77+ }
6478 }
6579
66- // Google Docs append
67- try {
68- console . log ( "📝 Appending text to Google Docs..." ) ;
69- const docsStartTime = Date . now ( ) ;
70- await googleDocsService . appendText ( rawTranscriptionText ) ;
71- const docsDuration = Date . now ( ) - docsStartTime ;
72-
73- console . log ( "✅ Google Docs update completed successfully" ) ;
74- console . log ( `⏱️ Docs update took: ${ docsDuration } ms` ) ;
75- } catch ( error ) {
76- console . error ( "❌ Error during Google Docs update:" , error . message ) ;
77- console . error ( "Stack trace:" , error . stack ) ;
78- return res . status ( 500 ) . json ( {
79- error : "Failed to append text to Google Docs" ,
80- details : error . message
81- } ) ;
80+ // Google Docs append with retry logic
81+ const docsMaxRetries = 5 ;
82+ const docsRetryDelay = 3000 ; // 3 second delay between retries
83+
84+ for ( let attempt = 1 ; attempt <= docsMaxRetries ; attempt ++ ) {
85+ try {
86+ console . log ( `📝 Appending text to Google Docs (attempt ${ attempt } /${ docsMaxRetries } )...` ) ;
87+ const docsStartTime = Date . now ( ) ;
88+ await googleDocsService . appendText ( rawTranscriptionText ) ;
89+ const docsDuration = Date . now ( ) - docsStartTime ;
90+
91+ console . log ( "✅ Google Docs update completed successfully" ) ;
92+ console . log ( `⏱️ Docs update took: ${ docsDuration } ms` ) ;
93+ break ; // Success, exit retry loop
94+ } catch ( error ) {
95+ console . error ( `❌ Error during Google Docs update (attempt ${ attempt } /${ docsMaxRetries } ):` , error . message ) ;
96+
97+ if ( attempt === docsMaxRetries ) {
98+ console . error ( "💥 All Google Docs update attempts failed" ) ;
99+ console . error ( "Stack trace:" , error . stack ) ;
100+ return res . status ( 500 ) . json ( {
101+ error : "Failed to append text to Google Docs after multiple attempts" ,
102+ details : error . message ,
103+ attempts : docsMaxRetries
104+ } ) ;
105+ }
106+
107+ console . log ( `⏳ Waiting ${ docsRetryDelay } ms before retry attempt ${ attempt + 1 } ...` ) ;
108+ await new Promise ( resolve => setTimeout ( resolve , docsRetryDelay ) ) ;
109+ }
82110 }
83111
84112 const totalDuration = Date . now ( ) - startTime ;
0 commit comments