From d085c86bb60077ef41c46ccd27f283bb3fff3fde Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Mon, 29 Aug 2022 22:23:07 +0800 Subject: [PATCH] Enqueue text to speech tasks --- packages/api/src/routers/text_to_speech.ts | 15 ++++++-- packages/api/src/services/speech.ts | 45 ---------------------- 2 files changed, 12 insertions(+), 48 deletions(-) diff --git a/packages/api/src/routers/text_to_speech.ts b/packages/api/src/routers/text_to_speech.ts index 70e636e0b..dab71ac4e 100644 --- a/packages/api/src/routers/text_to_speech.ts +++ b/packages/api/src/routers/text_to_speech.ts @@ -9,9 +9,10 @@ import { getPageById } from '../elastic/pages' import { Speech, SpeechState } from '../entity/speech' import { buildLogger } from '../utils/logger' import { getClaimsByToken } from '../utils/auth' -import { shouldSynthesize, synthesize } from '../services/speech' +import { shouldSynthesize } from '../services/speech' import { readPushSubscription } from '../datalayer/pubsub' import { AppDataSource } from '../server' +import { enqueueTextToSpeech } from '../utils/createTask' const logger = buildLogger('app.dispatch') @@ -62,8 +63,16 @@ export function textToSpeechRouter() { state: SpeechState.INITIALIZED, voice: 'en-US-JennyNeural', }) - await synthesize(page, speech) - logger.info('page synthesized') + // enqueue a task to convert text to speech + const taskName = await enqueueTextToSpeech({ + userId, + speechId: speech.id, + text: page.content, + voice: speech.voice, + priority: 'low', + }) + logger.info('Start Text to speech task', { taskName }) + return res.status(202).send('Text to speech task started') } res.status(200).send('Page should not synthesize') diff --git a/packages/api/src/services/speech.ts b/packages/api/src/services/speech.ts index 8d390c374..380a14a36 100644 --- a/packages/api/src/services/speech.ts +++ b/packages/api/src/services/speech.ts @@ -1,16 +1,6 @@ -import { getRepository } from '../entity/utils' -import { Speech, SpeechState } from '../entity/speech' import { searchPages } from '../elastic/pages' import { Page, PageType } from '../elastic/types' import { SortBy, SortOrder } from '../utils/search' -import { synthesizeTextToSpeech } from '../utils/textToSpeech' - -export const setSpeechFailure = async (id: string) => { - // update state - await getRepository(Speech).update(id, { - state: SpeechState.FAILED, - }) -} /* * We should not synthesize the page when: @@ -54,38 +44,3 @@ export const shouldSynthesize = async ( page.savedAt < recentListenedPage[0].listenedAt ) } - -export const synthesize = async (page: Page, speech: Speech): Promise => { - try { - if (page.pageType === PageType.File || !page.content) { - // we don't synthesize files for now - return - } - - console.log('Start synthesizing', { pageId: page.id, speechId: speech.id }) - const startTime = Date.now() - const speechOutput = await synthesizeTextToSpeech({ - id: speech.id, - text: page.content, - languageCode: page.language, - voice: speech.voice, - textType: 'ssml', - }) - console.log('Synthesized article', { - audioFileName: speechOutput.audioFileName, - speechMarksFileName: speechOutput.speechMarksFileName, - duration: Date.now() - startTime, - }) - - // set state to completed - await getRepository(Speech).update(speech.id, { - audioFileName: speechOutput.audioFileName, - speechMarksFileName: speechOutput.speechMarksFileName, - state: SpeechState.COMPLETED, - }) - } catch (error) { - console.log('Error synthesize article', error) - await setSpeechFailure(speech.id) - throw error - } -}