首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是什么原因导致流星应用程序有时创建两个文档?

是什么原因导致流星应用程序有时创建两个文档?
EN

Stack Overflow用户
提问于 2016-10-24 23:23:46
回答 1查看 54关注 0票数 0

我有一个流星应用程序,它将文章的文本分割成段落、句子、单词和字符,然后将其存储在json中,然后将其保存为集合中的文档。我正在测试的文档现在以mongodb中的15133字节结束。

当我插入文档时,插入大约需要20或30秒。然后,有时它又开始遍历我的文章创建例程,并插入另一个文档。有时,它会插入3个或更多的文档。有时,它的行为是应该的,并且只将一个文档插入到集合中。

我应该寻找什么可能导致这种行为的原因?

这是我的代码,按要求:

代码语言:javascript
复制
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
}
})

我称之为这里的方法:

代码语言:javascript
复制
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);
 }
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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,这将在第二次尝试中失败。出于某种原因,这种情况在你的情况下不会发生。

最后,您所描述的问题清楚地表明,可能应该使用另一种模式来处理像您这样计算成本高昂的任务。如果我是你,我会从分几个步骤开始:

  1. 首先,我要确保通过POST请求将文档上传到服务器,而不是通过DDP。
  2. 然后,我将实现一个"process“服务器端方法,该方法获取已经在服务器上或数据库中的文件(万一您使用了文件集合)。该方法应该做的第一件事是调用this.unblock(),但这还不是全部。
  3. 理想情况下,计算任务应该在一个独立的进程中执行。只有当该过程完成时,该方法才会返回,告诉实际调用者完成了任务。但是由于我们调用了this.unblock(),调用者可以执行不同的任务,例如,在等待结果时调用其他方法/订阅。

有时候,有一个独立的过程是不够的。我经历过不得不将任务委托给另一个工作服务器的情况。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40229092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档