clean up error handling in say verb

This commit is contained in:
Dave Horton
2022-04-21 10:27:33 -04:00
parent 359cb82d80
commit 698d12a95f
2 changed files with 42 additions and 35 deletions

View File

@@ -331,7 +331,7 @@ class CallSession extends Emitter {
speech_credential_sid: credential.speech_credential_sid, speech_credential_sid: credential.speech_credential_sid,
accessKeyId: credential.access_key_id, accessKeyId: credential.access_key_id,
secretAccessKey: credential.secret_access_key, secretAccessKey: credential.secret_access_key,
region: process.env.AWS_REGION || credential.aws_region region: credential.aws_region || process.env.AWS_REGION
}; };
} }
else if ('microsoft' === vendor) { else if ('microsoft' === vendor) {

View File

@@ -56,50 +56,57 @@ class TaskSay extends Task {
// synthesize all of the text elements // synthesize all of the text elements
let lastUpdated = false; let lastUpdated = false;
/* otel: trace time for tts */ /* produce an audio segment from the provided text */
const {span} = this.startChildSpan('tts-generation', { const generateAudio = async(text) => {
'tts.vendor': vendor,
'tts.language': language,
'tts.voice': voice
});
this.ttsSpan = span;
const filepath = (await Promise.all(this.text.map(async(text) => {
if (this.killed) return; if (this.killed) return;
if (text.startsWith('silence_stream://')) return text; if (text.startsWith('silence_stream://')) return text;
const {filePath, servedFromCache} = await synthAudio(stats, {
text, /* otel: trace time for tts */
vendor, const {span} = this.startChildSpan('tts-generation', {
language, 'tts.vendor': vendor,
voice, 'tts.language': language,
engine, 'tts.voice': voice
salt, });
credentials try {
}).catch((err) => { const {filePath, servedFromCache} = await synthAudio(stats, {
this.logger.info(err, 'Error synthesizing tts'); text,
vendor,
language,
voice,
engine,
salt,
credentials
});
this.logger.debug(`file ${filePath}, served from cache ${servedFromCache}`);
if (filePath) cs.trackTmpFile(filePath);
if (!servedFromCache && !lastUpdated) {
lastUpdated = true;
updateSpeechCredentialLastUsed(credentials.speech_credential_sid)
.catch(() => {/*already logged error */});
}
span.setAttributes({'tts.cached': servedFromCache});
span.end();
return filePath;
} catch (err) {
this.logger.info({err}, 'Error synthesizing tts');
span.end();
writeAlerts({ writeAlerts({
account_sid: cs.accountSid, account_sid: cs.accountSid,
alert_type: AlertType.TTS_NOT_PROVISIONED, alert_type: AlertType.TTS_NOT_PROVISIONED,
vendor, vendor,
detail: err.message detail: err.message
}); }).catch((err) => this.logger.info({err}, 'Error generating alert for tts failure'));
}).catch((err) => this.logger.info({err}, 'Error generating alert for tts failure')); return;
this.logger.debug(`file ${filePath}, served from cache ${servedFromCache}`);
if (filePath) cs.trackTmpFile(filePath);
if (!servedFromCache && !lastUpdated) {
lastUpdated = true;
updateSpeechCredentialLastUsed(credentials.speech_credential_sid)
.catch(() => {/*already logged error */});
} }
this.ttsSpan.setAttributes({'tts.cached': servedFromCache}); };
return filePath;
}))).filter((fp) => fp && fp.length); const arr = this.text.map((t) => generateAudio(t));
this.ttsSpan?.end(); const filepath = (await Promise.all(arr)).filter((fp) => fp && fp.length);
this.logger.debug({filepath}, 'synthesized files for tts'); this.logger.debug({filepath}, 'synthesized files for tts');
while (!this.killed && (this.loop === 'forever' || this.loop--) && this.ep?.connected) { while (!this.killed && (this.loop === 'forever' || this.loop--) && this.ep?.connected) {
let segment = 0; let segment = 0;
do { while (!this.killed && segment < filepath.length) {
if (cs.isInConference) { if (cs.isInConference) {
const {memberId, confName, confUuid} = cs; const {memberId, confName, confUuid} = cs;
await this.playToConfMember(this.ep, memberId, confName, confUuid, filepath[segment]); await this.playToConfMember(this.ep, memberId, confName, confUuid, filepath[segment]);
@@ -109,10 +116,10 @@ class TaskSay extends Task {
await ep.play(filepath[segment]); await ep.play(filepath[segment]);
this.logger.debug(`Say:exec completed play file ${filepath[segment]}`); this.logger.debug(`Say:exec completed play file ${filepath[segment]}`);
} }
} while (!this.killed && ++segment < filepath.length); segment++;
}
} }
} catch (err) { } catch (err) {
this.ttsSpan?.end();
this.logger.info(err, 'TaskSay:exec error'); this.logger.info(err, 'TaskSay:exec error');
} }
this.emit('playDone'); this.emit('playDone');