Skip to content

Commit

Permalink
fix(processor): remove race condition failsafe (#4791)
Browse files Browse the repository at this point in the history
* fix(processor): race condition

* fix(processor): add race condition warning

* test: avoid potential race condition
  • Loading branch information
SukkaW committed Oct 8, 2021
1 parent 9edbc99 commit 02cbfe3
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 94 deletions.
8 changes: 4 additions & 4 deletions lib/plugins/processor/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Promise = require('bluebird');
const { parse: yfm } = require('hexo-front-matter');
const { extname, relative } = require('path');
const { Pattern } = require('hexo-util');
const { magenta } = require('nanocolors');

module.exports = ctx => {
return {
Expand Down Expand Up @@ -96,11 +97,10 @@ function processPage(ctx, file) {
data.layout = false;
}

// FIXME: Data may be inserted when reading files. Load it again to prevent
// race condition. We have to solve this in warehouse.
const doc = Page.findOne({source: path});

if (doc) {
if (file.type !== 'update') {
ctx.log.warn(`Trying to "create" ${magenta(file.path)}, but the file already exists!`);
}
return doc.replace(data);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/plugins/processor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { parse: yfm } = require('hexo-front-matter');
const { extname, join } = require('path');
const { stat, listDir } = require('hexo-fs');
const { slugize, Pattern, Permalink } = require('hexo-util');
const { magenta } = require('nanocolors');

const postDir = '_posts/';
const draftDir = '_drafts/';
Expand Down Expand Up @@ -163,11 +164,10 @@ function processPost(ctx, file) {
data.permalink = undefined;
}

// FIXME: Data may be inserted when reading files. Load it again to prevent
// race condition. We have to solve this in warehouse.
const doc = Post.findOne({source: file.path});

if (doc) {
if (file.type !== 'update') {
ctx.log.warn(`Trying to "create" ${magenta(file.path)}, but the file already exists!`);
}
return doc.replace(data);
}

Expand Down
12 changes: 8 additions & 4 deletions test/scripts/processors/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ describe('asset', () => {
asset.modified.should.be.true;
asset.renderable.should.be.false;

asset.remove();
unlink(file.source);
return Promise.all([
asset.remove(),
unlink(file.source)
]);
});

it('asset - type: create (when source path is configed to parent directory)', async () => {
Expand Down Expand Up @@ -136,8 +138,10 @@ describe('asset', () => {
asset.modified.should.be.true;
asset.renderable.should.be.false;

asset.remove();
unlink(file.source);
return Promise.all([
asset.remove(),
unlink(file.source)
]);
});

it('asset - type: skip', async () => {
Expand Down

0 comments on commit 02cbfe3

Please sign in to comment.