Skip to content

Commit cca9b3e

Browse files
authored
Merge pull request #5722 from nextcloud/backport/5705/stable34
[stable34] fix(frontend): retry built-in CODE cold starts and show startup status in Office viewer
2 parents 8beed04 + 8db89d2 commit cca9b3e

2 files changed

Lines changed: 51 additions & 33 deletions

File tree

src/services/collabora.js

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const LOADING_ERROR = {
1010
PROXY_FAILED: 2,
1111
}
1212

13+
const PROXY_STARTING_STATES = new Set(['starting', 'stopped', 'restarting'])
14+
const PROXY_POLL_INTERVAL_MS = 1500
15+
const PROXY_POLL_TIMEOUT_MS = 30000
16+
17+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
18+
1319
export const isCollaboraConfigured = () => {
1420
const collaboraCapabilities = getCapabilities()?.collabora
1521
return isBuiltinCodeServerUsed() || collaboraCapabilities.length !== 0
@@ -26,43 +32,46 @@ export const checkCollaboraConfiguration = async () => {
2632
}
2733
}
2834

29-
let proxyStatusCheckRetry = 0
30-
export const checkProxyStatus = async (_resolve, _reject) => {
35+
export const checkProxyStatus = async () => {
3136
const wopiUrl = getCapabilities()?.config?.wopi_url
3237
if (wopiUrl.indexOf('proxy.php') === -1) {
3338
return true
3439
}
3540

3641
const url = wopiUrl.slice(0, wopiUrl.indexOf('proxy.php') + 'proxy.php'.length)
3742
const proxyStatusUrl = url + '?status'
43+
const deadline = Date.now() + PROXY_POLL_TIMEOUT_MS
44+
45+
while (Date.now() < deadline) {
46+
let result
47+
try {
48+
result = await axios.get(proxyStatusUrl)
49+
} catch (e) {
50+
await sleep(PROXY_POLL_INTERVAL_MS)
51+
continue
52+
}
3853

39-
const checkProxyStatusCallback = async (resolve, reject) => {
40-
const result = await axios.get(proxyStatusUrl)
41-
if (!result || !result?.data?.status) {
42-
reject('Failed to contact status endpoint')
54+
const status = result?.data?.status
55+
if (!status) {
56+
await sleep(PROXY_POLL_INTERVAL_MS)
57+
continue
4358
}
4459

45-
if (result.data.status === 'OK') {
46-
return resolve(true)
60+
if (status === 'OK') {
61+
return true
4762
}
48-
if (result.data.status === 'error') {
49-
return reject(t('richdocuments', 'Built-in CODE server failed to start'))
63+
64+
if (status === 'error') {
65+
throw Error(LOADING_ERROR.PROXY_FAILED)
5066
}
5167

52-
if (proxyStatusCheckRetry < 3 && (result.data.status === 'starting' || result.data.status === 'stopped' || result.data.status === 'restarting')) {
53-
setTimeout(() => {
54-
proxyStatusCheckRetry++
55-
checkProxyStatus(resolve, reject)
56-
})
57-
} else {
58-
reject('Maximum retries reached')
68+
if (PROXY_STARTING_STATES.has(status)) {
69+
await sleep(PROXY_POLL_INTERVAL_MS)
70+
continue
5971
}
6072

73+
await sleep(PROXY_POLL_INTERVAL_MS)
6174
}
6275

63-
if (_reject && _resolve) {
64-
return checkProxyStatusCallback(_reject, _resolve)
65-
} else {
66-
return new Promise(checkProxyStatusCallback)
67-
}
76+
throw Error(t('richdocuments', 'Starting the built-in CODE server is taking longer than expected'))
6877
}

src/view/Office.vue

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ import {
100100
} from '../helpers/url.js'
101101
import PostMessageService from '../services/postMessage.tsx'
102102
import FilesAppIntegration from './FilesAppIntegration.js'
103-
import { LOADING_ERROR, checkCollaboraConfiguration, checkProxyStatus } from '../services/collabora.js'
103+
import {
104+
LOADING_ERROR,
105+
checkCollaboraConfiguration,
106+
checkProxyStatus,
107+
isBuiltinCodeServerUsed,
108+
} from '../services/collabora.js'
104109
import { enableScrollLock, disableScrollLock } from '../helpers/mobileFixer.js'
105110
import axios from '@nextcloud/axios'
106111
import {
@@ -277,7 +282,11 @@ export default {
277282
})
278283
try {
279284
await checkCollaboraConfiguration()
285+
if (isBuiltinCodeServerUsed()) {
286+
this.loadingMsg = t('richdocuments', 'Starting the built-in CODE server …')
287+
}
280288
await checkProxyStatus()
289+
this.loadingMsg = null
281290
} catch (e) {
282291
this.error = e.message
283292
this.loading = LOADING_STATE.FAILED
@@ -408,16 +417,16 @@ export default {
408417
this.documentReady()
409418
410419
if (loadState('richdocuments', 'open_local_editor', true) && !this.isEmbedded) {
411-
this.sendPostMessage('Insert_Button', {
412-
id: 'Open_Local_Editor',
413-
imgurl: window.location.protocol + '//' + getNextcloudUrl() + imagePath('richdocuments', 'launch.svg'),
414-
mobile: false,
415-
label: t('richdocuments', 'Open in local editor'),
416-
hint: t('richdocuments', 'Open in local editor'),
417-
insertBefore: 'print',
418-
accessKey: '2',
419-
})
420-
}
420+
this.sendPostMessage('Insert_Button', {
421+
id: 'Open_Local_Editor',
422+
imgurl: window.location.protocol + '//' + getNextcloudUrl() + imagePath('richdocuments', 'launch.svg'),
423+
mobile: false,
424+
label: t('richdocuments', 'Open in local editor'),
425+
hint: t('richdocuments', 'Open in local editor'),
426+
insertBefore: 'print',
427+
accessKey: '2',
428+
})
429+
}
421430
422431
if (this.isEmbedded && this.hasWidgetEditingEnabled) {
423432
this.sendPostMessage('Hide_Sidebar')

0 commit comments

Comments
 (0)