refactor unarchiveDoc to use a separate getDoc helper

This commit is contained in:
Brian Gough 2021-07-30 16:03:43 +01:00
parent 5b8059f341
commit 0095a381b0

View file

@ -23,6 +23,7 @@ module.exports = {
unarchiveDoc: callbackify(unarchiveDoc), unarchiveDoc: callbackify(unarchiveDoc),
destroyAllDocs: callbackify(destroyAllDocs), destroyAllDocs: callbackify(destroyAllDocs),
destroyDoc: callbackify(destroyDoc), destroyDoc: callbackify(destroyDoc),
getDoc: callbackify(getDoc),
promises: { promises: {
archiveAllDocs, archiveAllDocs,
archiveDocById, archiveDocById,
@ -31,6 +32,7 @@ module.exports = {
unarchiveDoc, unarchiveDoc,
destroyAllDocs, destroyAllDocs,
destroyDoc, destroyDoc,
getDoc,
}, },
} }
@ -125,38 +127,17 @@ async function unArchiveAllDocs(projectId) {
} }
} }
async function unarchiveDoc(projectId, docId) { // get the doc from the PersistorManager without storing it in mongo
logger.log( async function getDoc(projectId, docId) {
{ project_id: projectId, doc_id: docId },
'getting doc from persistor'
)
const originalDoc = await MongoManager.findDoc(projectId, docId, { inS3: 1 })
if (!originalDoc.inS3) {
// return if it's not actually in S3 as there's nothing to do
return
}
const key = `${projectId}/${docId}` const key = `${projectId}/${docId}`
let stream, sourceMd5 const sourceMd5 = await PersistorManager.getObjectMd5Hash(
try { settings.docstore.bucket,
sourceMd5 = await PersistorManager.getObjectMd5Hash( key
settings.docstore.bucket, )
key const stream = await PersistorManager.getObjectStream(
) settings.docstore.bucket,
stream = await PersistorManager.getObjectStream( key
settings.docstore.bucket, )
key
)
} catch (err) {
// if we get a 404, we could be in a race and something else has unarchived the doc already
if (err instanceof Errors.NotFoundError) {
const doc = await MongoManager.findDoc(projectId, docId, { inS3: 1 })
if (!doc.inS3) {
// the doc has been archived while we were looking for it, so no error
return
}
}
throw err
}
stream.resume() stream.resume()
const json = await _streamToString(stream) const json = await _streamToString(stream)
const md5 = crypto.createHash('md5').update(json).digest('hex') const md5 = crypto.createHash('md5').update(json).digest('hex')
@ -181,6 +162,36 @@ async function unarchiveDoc(projectId, docId) {
} else { } else {
throw new Error("I don't understand the doc format in s3") throw new Error("I don't understand the doc format in s3")
} }
return mongoDoc
}
// get the doc and unarchive it to mongo
async function unarchiveDoc(projectId, docId) {
logger.log(
{ project_id: projectId, doc_id: docId },
'getting doc from persistor'
)
const key = `${projectId}/${docId}`
const originalDoc = await MongoManager.findDoc(projectId, docId, { inS3: 1 })
if (!originalDoc.inS3) {
// return if it's not actually in S3 as there's nothing to do
return
}
let mongoDoc
try {
mongoDoc = await getDoc(projectId, docId)
} catch (err) {
// if we get a 404, we could be in a race and something else has unarchived the doc already
if (err instanceof Errors.NotFoundError) {
const doc = await MongoManager.findDoc(projectId, docId, { inS3: 1 })
if (!doc.inS3) {
// the doc has been archived while we were looking for it, so no error
return
}
}
throw err
}
await MongoManager.upsertIntoDocCollection(projectId, docId, mongoDoc) await MongoManager.upsertIntoDocCollection(projectId, docId, mongoDoc)
await PersistorManager.deleteObject(settings.docstore.bucket, key) await PersistorManager.deleteObject(settings.docstore.bucket, key)
} }