New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(processor): remove race condition failsafe #4791
Conversation
b411032
to
2a6cbda
Compare
// 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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are only three reasons that a doc
will already exist while the processor is trying to create
a post:
- A race condition inside the warehouse (which seems to be fixed in warehouse@2.1.0 back in 2015)
- The
file.type
isupdate
. in this case, there is no need to load thedoc
again. - In some rare situation where two
file
object is created, with the sameid
and the samecreate
type (E.g. inside our unit test when we trying tocreate
2file
s under the samefile.path
. This is not likely to happen in the real-world production). But Hexo can still deal with the issue by replacingdoc
directly. And I add a warning message for the case, and hopefully, someone who encounters the issue would report it to us.
return Promise.all([ | ||
post.remove(), | ||
unlink(file.source) | ||
]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both _Document.remove
method and fs.unlink
method returns Promise
.
What does it do?
This closes #4430
How to test
Pull request tasks