我有一个流星应用程序,它将文章的文本分割成段落、句子、单词和字符,然后将其存储在json中,然后将其保存为集合中的文档。我正在测试的文档现在以mongodb中的15133字节结束。
当我插入文档时,插入大约需要20或30秒。然后,有时它又开始遍历我的文章创建例程,并插入另一个文档。有时,它会插入3个或更多的文档。有时,它的行为是应该的,并且只将一个文档插入到集合中。
我应该寻找什么可能导致这种行为的原因?
这是我的代码,按要求:
Meteor.methods({
'createArticle': function (text, title) {
var article = {}
article.title = title
article.userID = "sdfgsdfg"
article.text = text
article.paragraphs = []
var paragraphs = splitArticleIntoParagraphs(text)
console.log("paragraphs", paragraphs)
_.each(paragraphs, function (paragraph, p) {
if (paragraph !== "") {
console.log("paragraph", paragraph)
article.paragraphs[p] = {}
article.paragraphs[p].read = false
article.paragraphs[p].text = paragraph
console.log("paragraphs[p]", article.paragraphs[p])
var sentences = splitParagraphIntoSentences(paragraph)
article.paragraphs[p].sentences = []
}
_.each(sentences, function (sentence, s) {
if (sentence !== "") {
article.paragraphs[p].sentences[s] = {}
console.log("sentence", sentence)
article.paragraphs[p].sentences[s].text = sentence
article.paragraphs[p].sentences[s].read = false
console.log("paragraphs[p].sentences[s]", article.paragraphs[p].sentences[s])
var wordsForward = splitSentenceIntoWordsForward(sentence)
console.log("wordsForward", JSON.stringify(wordsForward))
article.paragraphs[p].sentences[s].forward = {}
article.paragraphs[p].sentences[s].forward.words = wordsForward
// var wordsReverse = splitSentenceIntoWordsReverse(sentence)
_.each(wordsForward, function (word, w) {
if (word) {
// console.log("word", JSON.stringify(word))
// article.paragraphs[p].sentences[s] = {}
// article.paragraphs[p].sentences[s].forward = {}
// article.paragraphs[p].sentences[s].forward.words = []
article.paragraphs[p].sentences[s].forward.words[w] = {}
article.paragraphs[p].sentences[s].forward.words[w].wordID = word._id
article.paragraphs[p].sentences[s].forward.words[w].simp = word.simp
article.paragraphs[p].sentences[s].forward.words[w].trad = word.trad
console.log("word.simp", word.simp)
var characters = word.simp.split('')
console.log("characters", characters)
article.paragraphs[p].sentences[s].forward.words[w].characters = []
_.each(characters, function (character, c) {
if (character) {
console.log("character", character, p, s, w, c)
article.paragraphs[p].sentences[s].forward.words[w].characters[c] = {}
article.paragraphs[p].sentences[s].forward.words[w].characters[c].text = character
article.paragraphs[p].sentences[s].forward.words[w].characters[c].wordID = Words.findOne({simp: character})._id
}
})
}
})
}
})
})
// console.log("article", JSON.stringify(article))
// console.log(JSON.stringify(article.paragraphs[10].sentences[1].forward))//.words[4].characters[0])
console.log("done")
var id = Articles.insert(article)
console.log("id", id)
return id
}
})我称之为这里的方法:
Template.articleList.events({
"click #addArticle": function(event) {
event.preventDefault();
var title = $('#title').val();
var text = $('#text').val();
$('#title').value = '';
$('#text').value = '';
$('#text').attr('rows', '3');
Meteor.call('createArticle', text, title);
}
})发布于 2016-10-25 07:53:31
需要记住的一件重要的事情是,流星方法在执行CPU密集型任务时不能很好地工作。由于流星服务器只在一个线程中工作,任何类型的阻塞计算--比如您的计算--都会影响所有客户端连接,例如延迟DDP心跳。这反过来又会导致客户认为连接已经中断。
正如@ghybs在其中一个注释中所建议的,您的方法可能是由一个不耐烦的DDP客户端触发的,他认为服务器断开了连接。防止这种行为的最简单方法是将noRetry标志添加到Meteor.apply中,如下所示:
https://docs.meteor.com/api/methods.html#Meteor-apply
我相信Meteor.call没有这个选择。
另一种策略是确保您的方法是幂等的,即多次调用它们不应该产生任何额外的效果。这通常是正确的--至少在使用方法模拟时是这样的--因为重新尝试db insert将重复使用相同的文档id,这将在第二次尝试中失败。出于某种原因,这种情况在你的情况下不会发生。
最后,您所描述的问题清楚地表明,可能应该使用另一种模式来处理像您这样计算成本高昂的任务。如果我是你,我会从分几个步骤开始:
this.unblock(),但这还不是全部。this.unblock(),调用者可以执行不同的任务,例如,在等待结果时调用其他方法/订阅。有时候,有一个独立的过程是不够的。我经历过不得不将任务委托给另一个工作服务器的情况。
https://stackoverflow.com/questions/40229092
复制相似问题