Skip to content

Commit

Permalink
Don't show notification until download actually starts (microsoft#6369)
Browse files Browse the repository at this point in the history
Co-authored-by: Dong Lei <[email protected]>
Co-authored-by: Chris Whitten <[email protected]>
  • Loading branch information
3 people authored Mar 11, 2021
1 parent 0b28e1d commit 2e39dbf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ import { Locales } from '../../locales';

import { createNotification } from './notification';

export const downloadModel = (addr: string, model: 'en' | 'multilang') => {
export const downloadModel = (addr: string, model: 'en' | 'multilang', notificationStartCallback: () => void) => {
return new Promise<boolean>((resolve, reject) => {
httpClient.post(addr, { language: model }).then((resp) => {
if (resp.status === 201) {
resolve(true);
return;
}

if (resp.status !== 200) {
reject(false);
return;
}

notificationStartCallback();

const statusUri = resp.data;

const timer = setInterval(async () => {
Expand Down Expand Up @@ -74,11 +82,12 @@ export const orchestratorDispatcher = () => {
// Download Model Notification
const { addNotification, deleteNotification } = await snapshot.getPromise(dispatcherState);
const notification = createNotification(orchestratorDownloadNotificationProps());
addNotification(notification);

try {
for (const languageModel of availableLanguageModels(recognizers)) {
await downloadModel('/orchestrator/download', languageModel);
await downloadModel('/orchestrator/download', languageModel, () => {
addNotification(notification);
});
}
} finally {
deleteNotification(notification.id);
Expand Down
11 changes: 6 additions & 5 deletions Composer/packages/server/src/controllers/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ async function downloadDefaultModel(req: Request, res: Response) {
const modelName = lang.language === 'en' ? modelList.defaults?.en_intent : modelList.defaults?.multilingual_intent;
const modelPath = await getModelPath(modelName);

if (await pathExists(modelPath)) {
state = DownloadState.ALREADYDOWNLOADED;
return res.send(201);
}

const onProgress = (msg: string) => {
setTimeout(() => {
state = DownloadState.DOWNLOADING;
Expand All @@ -73,11 +78,7 @@ async function downloadDefaultModel(req: Request, res: Response) {
state = DownloadState.DOWNLOADING;

setTimeout(async () => {
if (!(await pathExists(modelPath))) {
await Orchestrator.baseModelGetAsync(modelPath, modelName, onProgress, onFinish);
} else {
state = DownloadState.ALREADYDOWNLOADED;
}
await Orchestrator.baseModelGetAsync(modelPath, modelName, onProgress, onFinish);
}, 0);

return res.send(200, '/orchestrator/status');
Expand Down

0 comments on commit 2e39dbf

Please sign in to comment.