Skip to content

Commit 1424dce

Browse files
authored
Invert remote saves (#1098)
* 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
1 parent 04a768a commit 1424dce

File tree

1 file changed

+56
-31
lines changed

1 file changed

+56
-31
lines changed

controllers/scriptStorage.js

+56-31
Original file line numberDiff line numberDiff line change
@@ -1206,40 +1206,65 @@ exports.storeScript = function (aUser, aMeta, aBuf, aCallback, aUpdate) {
12061206
}
12071207
}
12081208

1209-
aScript.save(function (aErr, aScript) {
1210-
var s3 = new AWS.S3();
1211-
1212-
// WARNING: No error handling at this stage
1213-
1214-
s3.putObject({ Bucket: bucketName, Key: installName, Body: aBuf },
1215-
function (aErr, aData) {
1216-
var userDoc = null;
1217-
1218-
// Don't save a script if storing failed
1219-
if (aErr) {
1220-
console.error(aUser.name, '-', installName);
1221-
console.error(JSON.stringify(aErr, null, ' '));
1222-
console.error(JSON.stringify(
1223-
aScript.toObject ? aScript.toObject({ virtuals: true }) : aScript, null, ' ')
1224-
);
1225-
aCallback(null);
1226-
return;
1227-
}
1209+
// Attempt to write out data to externals...
1210+
var s3 = new AWS.S3();
1211+
s3.putObject({
1212+
Bucket: bucketName,
1213+
Key: installName,
1214+
Body: aBuf
1215+
1216+
}, function (aErr, aData) {
1217+
if (aErr) {
1218+
// Forward the error
1219+
aScript.invalidate('_id', aErr);
1220+
1221+
// Localize the error
1222+
console.error(
1223+
'S3 putObject critical error\n' +
1224+
installName + '\n' +
1225+
JSON.stringify(aErr, null, ' ')
1226+
);
1227+
1228+
aCallback(null);
1229+
return;
1230+
}
1231+
1232+
aScript.save(function (aErr, aScript) {
1233+
var userDoc = null;
12281234

1229-
if (aUser.role === userRoles.length - 1) {
1230-
userDoc = aUser;
1231-
if (!userDoc.save) {
1232-
// We're probably using req.session.user which may have gotten serialized.
1233-
userDoc = new User(userDoc);
1235+
// Localize the error
1236+
if (aErr) {
1237+
console.error(
1238+
'MongoDB Script save critical error\n' +
1239+
installName + '\n' +
1240+
JSON.stringify(aErr, null, ' ')
1241+
);
1242+
1243+
aCallback(null);
1244+
return;
1245+
}
1246+
1247+
// Check for role change and modify accordingly
1248+
if (aUser.role === userRoles.length - 1) {
1249+
userDoc = aUser;
1250+
if (!userDoc.save) {
1251+
// Probably using req.session.user which may have gotten serialized.
1252+
userDoc = new User(userDoc);
1253+
}
1254+
--userDoc.role;
1255+
userDoc.save(function (aErr, aUser) {
1256+
if (aErr) {
1257+
console.warning('MongoDB User save warning error\n' +
1258+
aUser.name + ' was NOT role elevated from User to Author'
1259+
);
1260+
// fallthrough
12341261
}
1235-
--userDoc.role;
1236-
userDoc.save(function (aErr, aUser) {
1237-
aCallback(aScript);
1238-
});
1239-
} else {
12401262
aCallback(aScript);
1241-
}
1242-
});
1263+
});
1264+
} else {
1265+
aCallback(aScript);
1266+
}
1267+
});
12431268
});
12441269
});
12451270
};

0 commit comments

Comments
 (0)