javascript - 从调度程序填充 Azure 移动服务

标签 javascript sql azure scheduled-tasks azure-mobile-services

我创建了这样的代码,用于从另一个网站的 xml 导出中获取新闻,我正在尝试用它填充我的数据库。

function UpdateLunchTime() {
    var httpRequest = require('request');
    var xml2js = require('xml2js');
    var parser = new xml2js.Parser();
    var url = 'http://www...com/export/xml/actualities';
    httpRequest.get({
            url: url
    }, function(err, response, body) {
        if (err) {
            console.warn(statusCodes.INTERNAL_SERVER_ERROR, 
                'Some problem.');
        } else if (response.statusCode !== 200) {
            console.warn(statusCodes.BAD_REQUEST, 
                'Another problem');
        } else {
            //console.log(body);
            parser.parseString(body, function (err2, result) {
                //console.log(result.Root.event);
                var count = 0;
                for (var i=0;i<result.Root.event.length;i++)
                { 
                    //console.log(result.Root.event[i]);
                    InsertActionToDatabase(result.Root.event[i]);
                }
                /*
                result.Root.event.forEach(function(entry) {
                    InsertActionToDatabase(entry);
                });
                */
            });
        }
    });
}

function InsertActionToDatabase(action)
{
    var queryString = "INSERT INTO Action (title, description, ...) VALUES (?, ?, ...)";
    mssql.query(queryString, [action.akce[0], action.description[0],...], {
    success: function(insertResults) {
    },
      error: function(err) {
      console.log("Problem: " + err);
      }
    });
}

对于个人实际情况,它工作正常,但是当我在整个 xml 上运行它时,我收到此错误:

Error: [Microsoft][SQL Server Native Client 10.0][SQL Server]Resource ID : 1. The request limit for the database is 180 and has been reached. See 'http://go.microsoft.com/fwlink/?LinkId=267637' for assistance.

对于最后几个对象,我收到此错误:

Error: [Microsoft][SQL Server Native Client 10.0]TCP Provider: Only one usage of each socket address (protocol/network address/port) is normally permitted.

感谢帮助

最佳答案

问题是您试图在数据库中进行太多并发(插入)操作。请记住,在 Node.js 中(几乎)一切都是异步的,因此当您为其中一项调用 InsertActionToDatabase 时,此操作将立即开始,而不是等待完成返回。因此,您基本上是在尝试一次插入所有事件,并且正如错误消息所述,可以与 SQL 服务器建立的并发连接数有限制。

您需要做的是将循环更改为异步运行,方法是等待其中一个操作完成,然后再开始下一个操作(您也可以“批量”一次发送较少数量的操作,在每个操作之后继续批处理完成,但代码稍微复杂一点)如下所示。

var count = result.Root.event.length;
var insertAction = function(index) {
    if (index >= count) return;
    InsertActionToDatabase(result.Root.event[i], function() {
        insertAction(index + 1);
    });
}
insertAction(0);

并且 InsertActionToDatabase 函数将在完成时调用一个回调参数。

function InsertActionToDatabase(item, done) {
    var table = tables.getTable('event');
    table.insert(item, {
        success: function() {
            console.log('Inserted event: ', item);
            done();
        }
    });
}

关于javascript - 从调度程序填充 Azure 移动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23021307/

相关文章:

sql - mysql "group by"查询非常慢

azure - 如何在 azure cosmos DocumentDB 中的查询中读取数据时将 RequestCharge 减少到 1RU

javascript - 等待函数完成多个资源调用

javascript - Grunt 文件对象格式 : All regular files files immediately under top level directory

mysql - mysql查询中<>的含义是什么?

c# - 在一个应用程序中使用多个数据库

javascript - 正确的方式?在 NodeJS 和 HTML 中包含 JS

javascript - 如何检查对象中是否存在数字数组

azure - 使用 Microsoft Graph API 和 REST 方法从 Azure 门户应用程序注册中检索应用程序所有者

c# - 如何在 C# ASP.NET Web API 2 中从 Azure 队列返回 HTTP 状态代码