Merge pull request #8 from jorilallo/jori-lock
added document create locking
This commit is contained in:
@ -8,7 +8,7 @@
|
|||||||
"build:webpack": "cross-env NODE_ENV=production webpack --config webpack.config.prod.js --progress",
|
"build:webpack": "cross-env NODE_ENV=production webpack --config webpack.config.prod.js --progress",
|
||||||
"build:analyze": "cross-env NODE_ENV=production webpack --config webpack.config.prod.js --json | webpack-bundle-size-analyzer",
|
"build:analyze": "cross-env NODE_ENV=production webpack --config webpack.config.prod.js --json | webpack-bundle-size-analyzer",
|
||||||
"build": "npm run clean && npm run build:webpack",
|
"build": "npm run clean && npm run build:webpack",
|
||||||
"start": "cross-env NODE_ENV=development DEBUG=cache,presenters ./node_modules/.bin/nodemon --watch server index.js",
|
"start": "cross-env NODE_ENV=development DEBUG=sql,cache,presenters ./node_modules/.bin/nodemon --watch server index.js",
|
||||||
"lint": "eslint frontend",
|
"lint": "eslint frontend",
|
||||||
"deploy": "git push heroku master",
|
"deploy": "git push heroku master",
|
||||||
"heroku-postbuild": "npm run build && npm run sequelize db:migrate",
|
"heroku-postbuild": "npm run build && npm run sequelize db:migrate",
|
||||||
@ -102,6 +102,8 @@
|
|||||||
"react-keydown": "^1.6.1",
|
"react-keydown": "^1.6.1",
|
||||||
"react-router": "2.5.1",
|
"react-router": "2.5.1",
|
||||||
"rebass": "0.2.6",
|
"rebass": "0.2.6",
|
||||||
|
"redis": "^2.6.2",
|
||||||
|
"redis-lock": "^0.1.0",
|
||||||
"reflexbox": "^2.0.0",
|
"reflexbox": "^2.0.0",
|
||||||
"safestart": "0.8.0",
|
"safestart": "0.8.0",
|
||||||
"sass-loader": "4.0.0",
|
"sass-loader": "4.0.0",
|
||||||
@ -109,6 +111,7 @@
|
|||||||
"sequelize-cli": "2.4.0",
|
"sequelize-cli": "2.4.0",
|
||||||
"sequelize-encrypted": "0.1.0",
|
"sequelize-encrypted": "0.1.0",
|
||||||
"slug": "0.9.1",
|
"slug": "0.9.1",
|
||||||
|
"string-hash": "^1.1.0",
|
||||||
"style-loader": "0.13.0",
|
"style-loader": "0.13.0",
|
||||||
"truncate-html": "0.0.6",
|
"truncate-html": "0.0.6",
|
||||||
"url-loader": "0.5.7",
|
"url-loader": "0.5.7",
|
||||||
|
@ -3,6 +3,8 @@ import httpErrors from 'http-errors';
|
|||||||
import {
|
import {
|
||||||
sequelize,
|
sequelize,
|
||||||
} from '../sequelize';
|
} from '../sequelize';
|
||||||
|
import { lock } from '../redis';
|
||||||
|
import isUUID from 'validator/lib/isUUID';
|
||||||
|
|
||||||
const URL_REGEX = /^[a-zA-Z0-9-]*-([a-zA-Z0-9]{10,15})$/;
|
const URL_REGEX = /^[a-zA-Z0-9-]*-([a-zA-Z0-9]{10,15})$/;
|
||||||
|
|
||||||
@ -14,26 +16,28 @@ import { Document, Atlas } from '../models';
|
|||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
const getDocumentForId = async (id) => {
|
const getDocumentForId = async (id) => {
|
||||||
|
try {
|
||||||
let document;
|
let document;
|
||||||
if (id.match(URL_REGEX)) {
|
if (isUUID(id)) {
|
||||||
|
document = await Document.findOne({
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else if (id.match(URL_REGEX)) {
|
||||||
document = await Document.findOne({
|
document = await Document.findOne({
|
||||||
where: {
|
where: {
|
||||||
urlId: id.match(URL_REGEX)[1],
|
urlId: id.match(URL_REGEX)[1],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
try {
|
throw httpErrors.NotFound();
|
||||||
document = await Document.findOne({
|
}
|
||||||
where: {
|
return document;
|
||||||
id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Invalid UUID
|
// Invalid UUID
|
||||||
throw httpErrors.NotFound();
|
throw httpErrors.NotFound();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return document;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// FIXME: This really needs specs :/
|
// FIXME: This really needs specs :/
|
||||||
@ -130,6 +134,9 @@ router.post('documents.create', auth(), async (ctx) => {
|
|||||||
|
|
||||||
if (!ownerCollection) throw httpErrors.BadRequest();
|
if (!ownerCollection) throw httpErrors.BadRequest();
|
||||||
|
|
||||||
|
const document = await (() => {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
lock(ownerCollection.id, 10000, async (done) => {
|
||||||
let parentDocumentObj = {};
|
let parentDocumentObj = {};
|
||||||
if (parentDocument && ownerCollection.type === 'atlas') {
|
if (parentDocument && ownerCollection.type === 'atlas') {
|
||||||
parentDocumentObj = await Document.findOne({
|
parentDocumentObj = await Document.findOne({
|
||||||
@ -153,10 +160,16 @@ router.post('documents.create', auth(), async (ctx) => {
|
|||||||
|
|
||||||
// TODO: Move to afterSave hook if possible with imports
|
// TODO: Move to afterSave hook if possible with imports
|
||||||
if (parentDocument && ownerCollection.type === 'atlas') {
|
if (parentDocument && ownerCollection.type === 'atlas') {
|
||||||
|
await ownerCollection.reload();
|
||||||
ownerCollection.addNodeToNavigationTree(document);
|
ownerCollection.addNodeToNavigationTree(document);
|
||||||
await ownerCollection.save();
|
await ownerCollection.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
done(resolve(document));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: await presentDocument(ctx, document, {
|
data: await presentDocument(ctx, document, {
|
||||||
includeCollection: true,
|
includeCollection: true,
|
||||||
@ -187,6 +200,7 @@ router.post('documents.update', auth(), async (ctx) => {
|
|||||||
await document.save();
|
await document.save();
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
|
// TODO: Add locking
|
||||||
const collection = await Atlas.findById(document.atlasId);
|
const collection = await Atlas.findById(document.atlasId);
|
||||||
if (collection.type === 'atlas') {
|
if (collection.type === 'atlas') {
|
||||||
await collection.updateNavigationTree();
|
await collection.updateNavigationTree();
|
||||||
@ -212,6 +226,7 @@ router.post('documents.delete', auth(), async (ctx) => {
|
|||||||
|
|
||||||
if (!document || document.teamId !== user.teamId) throw httpErrors.BadRequest();
|
if (!document || document.teamId !== user.teamId) throw httpErrors.BadRequest();
|
||||||
|
|
||||||
|
// TODO: Add locking
|
||||||
if (collection.type === 'atlas') {
|
if (collection.type === 'atlas') {
|
||||||
// Don't allow deletion of root docs
|
// Don't allow deletion of root docs
|
||||||
if (!document.parentDocumentId) {
|
if (!document.parentDocumentId) {
|
||||||
|
@ -142,7 +142,7 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
|
|
||||||
return newTree;
|
return newTree;
|
||||||
},
|
},
|
||||||
addNodeToNavigationTree(document) {
|
async addNodeToNavigationTree(document) {
|
||||||
const newNode = {
|
const newNode = {
|
||||||
id: document.id,
|
id: document.id,
|
||||||
title: document.title,
|
title: document.title,
|
||||||
|
10
server/redis.js
Normal file
10
server/redis.js
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import redis from 'redis';
|
||||||
|
import redisLock from 'redis-lock';
|
||||||
|
|
||||||
|
const client = redis.createClient(process.env.REDIS_URL);
|
||||||
|
const lock = redisLock(client);
|
||||||
|
|
||||||
|
export {
|
||||||
|
client,
|
||||||
|
lock,
|
||||||
|
};
|
Reference in New Issue
Block a user