overleaf/services/web/scripts/translations/upload.js
Jakob Ackermann dea52fec6f Merge pull request #11270 from overleaf/jpa-retry-translations-upload
[web] scripts: add retry to translations upload script

GitOrigin-RevId: b67af0def876d5e1dd0478e7b8fcd234bfc05447
2023-01-17 09:04:51 +00:00

64 lines
1.8 KiB
JavaScript

const Path = require('path')
const { promises: fs } = require('fs')
const { promisify } = require('util')
const oneSky = require('@brainly/onesky-utils')
const { withAuth } = require('./config')
const sleep = promisify(setTimeout)
async function uploadLocales() {
// Docs: https://github.com/onesky/api-documentation-platform/blob/master/resources/file.md#upload---upload-a-file
const blob = await oneSky.postFile(
withAuth({
fileName: 'en-US.json',
language: 'en-GB',
format: 'HIERARCHICAL_JSON',
content: await fs.readFile(
Path.join(__dirname, '/../../locales/en.json')
),
keepStrings: false, // deprecate locales that no longer exist in en.json
})
)
return JSON.parse(blob).data.import.id
}
async function getImportTask(importId) {
// Docs: https://github.com/onesky/api-documentation-platform/blob/master/resources/import_task.md
const blob = await oneSky.getImportTask(withAuth({ importId }))
return JSON.parse(blob).data
}
async function pollUploadStatus(importId) {
let task
while ((task = await getImportTask(importId)).status === 'in-progress') {
console.log('onesky is processing the import ...')
await sleep(5000)
}
if (task.status === 'failed') {
console.error(JSON.stringify({ task }, null, 2))
throw new Error('upload failed')
}
}
async function uploadOnce() {
const importId = await uploadLocales()
await pollUploadStatus(importId)
}
async function main() {
try {
await uploadOnce()
} catch (err) {
console.error('--- upload failed once ---')
console.error(err)
console.error('--- upload failed once ---')
console.log('retrying upload in 30s')
await sleep(30_000)
await uploadOnce()
}
}
main().catch(error => {
console.error({ error })
process.exit(1)
})