Skip to content

Commit

Permalink
Invert remote saves (#1098)
Browse files Browse the repository at this point in the history
* On rare occasions S3 connection goes down so improve error logging... UI will currently show 404 on script edit submit... ideally this should be 502 but will work as former. UI new scripts will bail out and reload empty editor.
* MongoDB, once inside `.save` it will **not** abort unless it's a `.pre` option with the schema which we don't currently utilize due to code structure so do it last.
* Improve logging for MongoDB errors... if MongoDB connection goes down script source will be saved but UI will be out of sync instead of completely absent on a request... these are even rarer than S3.
* Denoted `fallthrough` comment on an error trap if User elevation didn't happen but log for Admin+ intervention.

NOTES:
`unmarkModified` is pretty much useless in this case scenario but was tested

Applies to #430 / #72 Related to #486

Auto-merge
  • Loading branch information
Martii authored May 1, 2017
1 parent 04a768a commit 1424dce
Showing 1 changed file with 56 additions and 31 deletions.
87 changes: 56 additions & 31 deletions controllers/scriptStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1206,40 +1206,65 @@ exports.storeScript = function (aUser, aMeta, aBuf, aCallback, aUpdate) {
}
}

aScript.save(function (aErr, aScript) {
var s3 = new AWS.S3();

// WARNING: No error handling at this stage

s3.putObject({ Bucket: bucketName, Key: installName, Body: aBuf },
function (aErr, aData) {
var userDoc = null;

// Don't save a script if storing failed
if (aErr) {
console.error(aUser.name, '-', installName);
console.error(JSON.stringify(aErr, null, ' '));
console.error(JSON.stringify(
aScript.toObject ? aScript.toObject({ virtuals: true }) : aScript, null, ' ')
);
aCallback(null);
return;
}
// Attempt to write out data to externals...
var s3 = new AWS.S3();
s3.putObject({
Bucket: bucketName,
Key: installName,
Body: aBuf

}, function (aErr, aData) {
if (aErr) {
// Forward the error
aScript.invalidate('_id', aErr);

// Localize the error
console.error(
'S3 putObject critical error\n' +
installName + '\n' +
JSON.stringify(aErr, null, ' ')
);

aCallback(null);
return;
}

aScript.save(function (aErr, aScript) {
var userDoc = null;

if (aUser.role === userRoles.length - 1) {
userDoc = aUser;
if (!userDoc.save) {
// We're probably using req.session.user which may have gotten serialized.
userDoc = new User(userDoc);
// Localize the error
if (aErr) {
console.error(
'MongoDB Script save critical error\n' +
installName + '\n' +
JSON.stringify(aErr, null, ' ')
);

aCallback(null);
return;
}

// Check for role change and modify accordingly
if (aUser.role === userRoles.length - 1) {
userDoc = aUser;
if (!userDoc.save) {
// Probably using req.session.user which may have gotten serialized.
userDoc = new User(userDoc);
}
--userDoc.role;
userDoc.save(function (aErr, aUser) {
if (aErr) {
console.warning('MongoDB User save warning error\n' +
aUser.name + ' was NOT role elevated from User to Author'
);
// fallthrough
}
--userDoc.role;
userDoc.save(function (aErr, aUser) {
aCallback(aScript);
});
} else {
aCallback(aScript);
}
});
});
} else {
aCallback(aScript);
}
});
});
});
};
Expand Down

0 comments on commit 1424dce

Please sign in to comment.