From 60bc7bf4e1ca77329faf5c9a5bbab4ca6e6c47b2 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Tue, 2 Jun 2015 12:36:38 +0100 Subject: [PATCH] pulled logic for arching docs arpart a bit, can be moved into its own file hacked out (needs tests) the unarchiving of individual docs & doc updates --- .../docstore/app/coffee/DocManager.coffee | 61 +++++++++++-------- .../docstore/app/coffee/MongoManager.coffee | 3 + 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/services/docstore/app/coffee/DocManager.coffee b/services/docstore/app/coffee/DocManager.coffee index aae71bc2d3..a89d83e11c 100644 --- a/services/docstore/app/coffee/DocManager.coffee +++ b/services/docstore/app/coffee/DocManager.coffee @@ -16,7 +16,13 @@ module.exports = DocManager = return callback(err) else if !doc? return callback new Errors.NotFoundError("No such doc: #{doc_id} in project #{project_id}") - callback null, doc + else if doc?.inS3 + DocManager.unarchiveDoc project_id, doc_id, (err)-> + if err? + return callback(err) + MongoManager.findDoc doc_id, callback + else + callback err, doc getAllDocs: (project_id, callback = (error, docs) ->) -> DocManager.unArchiveAllDocs project_id, (error) -> @@ -29,8 +35,8 @@ module.exports = DocManager = return callback(null, docs) updateDoc: (project_id, doc_id, lines, callback = (error, modified, rev) ->) -> - MongoManager.findDoc doc_id, (err, doc)-> - if err? + DocManager.getDoc doc_id, (err, doc)-> + if err? and !(error instanceof Errors.NotFoundError) logger.err project_id: project_id, doc_id: doc_id, err:err, "error getting document for update" return callback(err) @@ -63,51 +69,54 @@ module.exports = DocManager = return callback(error) if error? callback() + + #DOC ARCHIVER archiveAllDocs: (project_id, callback = (error, docs) ->) -> MongoManager.getProjectsDocs project_id, (error, docs) -> if err? return callback(error) else if !docs? return callback new Errors.NotFoundError("No docs for project #{project_id}") - jobs = _.map docs, (doc) -> - (cb)-> - logger.log project_id: project_id, doc_id: doc._id, "sending doc to s3" - options = buildS3Options(doc.lines, project_id+"/"+doc._id) - request.put options, (err, res)-> - if err? || res.statusCode != 200 - logger.err err:err, res:res, "something went wrong archiving doc in aws" - cb(err) - MongoManager.markDocAsArchived doc._id, (error) -> - return cb(error) if error? - cb() - + (cb)-> DocManager.archiveDoc project_id, doc, cb async.series jobs, callback + + archiveDoc: (project_id, doc, callback)-> + logger.log project_id: project_id, doc_id: doc._id, "sending doc to s3" + options = buildS3Options(doc.lines, project_id+"/"+doc._id) + request.put options, (err, res)-> + if err? || res.statusCode != 200 + logger.err err:err, res:res, "something went wrong archiving doc in aws" + callback(err) + MongoManager.markDocAsArchived doc._id, (error) -> + return callback(error) if error? + callback() + unArchiveAllDocs: (project_id, callback = (error) ->) -> MongoManager.getProjectsDocs project_id, (error, docs) -> if err? return callback(error) else if !docs? return callback new Errors.NotFoundError("No docs for project #{project_id}") - jobs = _.map docs, (doc) -> (cb)-> if !doc.inS3? return cb() else - logger.log project_id: project_id, doc_id: doc._id, "getting doc from s3" - options = buildS3Options(true, project_id+"/"+doc._id) - request.get options, (err, res, lines)-> - if err? || res.statusCode != 200 - logger.err err:err, res:res, "something went wrong unarchiving doc from aws" - cb(err) - MongoManager.upsertIntoDocCollection project_id, doc._id.toString(), lines, (error) -> - return cb(error) if error? - cb() - + unarchiveDoc project_id, doc_id, cb async.series jobs, callback + unarchiveDoc: (project_id, doc_id, callback)-> + logger.log project_id: project_id, doc_id: doc._id, "getting doc from s3" + options = buildS3Options(true, project_id+"/"+doc._id) + request.get options, (err, res, lines)-> + if err? || res.statusCode != 200 + logger.err err:err, res:res, "something went wrong unarchiving doc from aws" + callback(err) + MongoManager.upsertIntoDocCollection project_id, doc._id.toString(), lines, (error) -> + return callback(error) if error? + callback() buildS3Options = (content, key)-> return { diff --git a/services/docstore/app/coffee/MongoManager.coffee b/services/docstore/app/coffee/MongoManager.coffee index ef73297b90..1f8b9ca6d8 100644 --- a/services/docstore/app/coffee/MongoManager.coffee +++ b/services/docstore/app/coffee/MongoManager.coffee @@ -34,5 +34,8 @@ module.exports = MongoManager = $unset: {} update.$set["inS3"] = true update.$unset["lines"] = true + # to ensure that the lines have not changed during the archive process + # what if we passed the lines through into the query {_id: doc_id, lines:lines} + # or more performant would be todo the rev {_id:doc_id, rev:rev} db.docs.update _id: doc_id, update, (err)-> callback(err) \ No newline at end of file