Azure Functions EventhubTrigger - 数据库插入

Azure Functions EventhubTrigger - database inserts

我在由偶数集线器触发的 azure 函数中实现了一些代码。
如果触发,我想将数据插入天蓝色 sql 数据库。 我得到了我的代码 运行 并且批量插入工作正常,但我经常收到 RequestError Timeout。

有人可以给我一些建议,告诉我如何使用 azure 函数以正确的方式实现这个用例。由于发送到事件中心的数据,该函数实际上经常被触发。

[2022-03-30T13:13:07.464Z] Executing 'Functions.hotDatatoSql' (Reason='(null)', Id=a329b220-6ef6-4d75-9c15-beed6d7375cb)
[2022-03-30T13:13:07.466Z] Trigger Details: PartionId: 0, Offset: 85929768072-85930225256, EnqueueTimeUtc: 2022-03-29T16:46:03.5990000Z-2022-03-29T16:47:49.0860000Z, SequenceNumber: 351947-352202, Count: 256
[2022-03-30T13:13:23.109Z] RequestError: Timeout: Request failed to complete in 15000ms
[2022-03-30T13:13:23.111Z] Executed 'Functions.hotDatatoSql' (Succeeded, Id=a329b220-6ef6-4d75-9c15-beed6d7375cb, Duration=15717ms)
[2022-03-30T13:13:23.113Z]     at BulkLoad.done [as callback] (C:\Home\Software\azure_functions\node_modules\mssql\lib\tedious\request.js:307:19)
[2022-03-30T13:13:23.142Z]     at Parser.<anonymous> (C:\Home\Software\azure_functions\node_modules\tedious\lib\connection.js:2910:26)
[2022-03-30T13:13:23.145Z]     at Object.onceWrapper (node:events:509:28)
[2022-03-30T13:13:23.148Z]     at Parser.emit (node:events:390:28)
[2022-03-30T13:13:23.150Z]     at Readable.<anonymous> (C:\Home\Software\azure_functions\node_modules\tedious\lib\token\token-stream-parser.js:32:12)
[2022-03-30T13:13:23.152Z]     at Readable.emit (node:events:390:28)
[2022-03-30T13:13:23.153Z]     at endReadableNT (node:internal/streams/readable:1343:12)
[2022-03-30T13:13:23.162Z]     at processTicksAndRejections (node:internal/process/task_queues:83:21) {
[2022-03-30T13:13:23.164Z]   code: 'ETIMEOUT',
[2022-03-30T13:13:23.167Z]   originalError: RequestError: Timeout: Request failed to complete in 15000ms

我的 Azure 函数代码:

const mssql = require('mssql');
const { get } = require('./pool-manager')

const config = {
    user: "...",
    password: "...",
    server: '....',
    database: '...',
    pool: {
        max: 10,
        min: 0,
        idleTimeoutMillis: 30000
    }, 
    options: {
        encrypt: true,
        trustServerCertificate: true
    }
};

const table = new mssql.Table('dbo.testTable');
table.columns.add('row1', mssql.VarChar(512), {nullable: true});
table.columns.add('row2', mssql.DateTime2, {nullable: true});
table.columns.add('row3', mssql.NVarChar(mssql.MAX), {nullable: true});

module.exports = async function (context, eventHubMessages) {

    const pool = await get('default', config);

    eventHubMessages.forEach((message, index) => {

        table.rows.add(message.id, message.time, JSON.stringify(message.data));
    });

    const request = new mssql.Request(pool);

    try{
        let result = await request.bulk(table);
        //console.log(result);
    }
    catch(err){
        console.log(err);
    }
};

为了达到上述要求,正如@Peter Bons所建议的,我们需要增加超时,因为默认超时为 15 秒。

例如增加idletimeout:

const config = {
    user: '...',
    password: '...',
    server: 'localhost',
    database: '...',
    pool: {
        max: 10,
        min: 0,
        idleTimeoutMillis: 300000
    }
}

有关更多信息,您也可以参考此 SO THREAD