【问题标题】:Azure Function - After Out binding how to trigger a functionAzure Function - Out 绑定后如何触发函数
【发布时间】:2019-04-12 12:42:33
【问题描述】:

我有一个 Azure 函数。我创建了一个输出绑定,并且正在将数据写入该输出 CosmosDB。

不过我想问一下,一旦这样做了,是否有可能触发另一个触发器?

或者我是否必须手动编写代码才能添加到数据库中,即不使用 out 绑定?

谢谢。

代码如下:

函数.json

{
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "name": "documents",
      "direction": "in",
      "leaseCollectionName": "leases",
      "connectionStringSetting": "COSMOSDB_INPUT_CONNECTION_STRING",
      "databaseName": "default",
      "collectionName": "metadata",
      "createLeaseCollectionIfNotExists": false,
      "leaseCollectionPrefix": "IngestMetadata",
      "startFromBeginning": true
    },
    {
      "type": "cosmosDB",
      "name": "outputdocuments",
      "direction": "out",
      "connectionStringSetting": "COSMOSDB_CONNECTION_STRING",
      "databaseName": "default",
      "collectionName": "metadata",
      "createIfNotExists": true
  }
  ],
  "scriptFile": "../dist/IngestMetadata/index.js"
}

还有代码本身:

const cosmosDBTrigger: AzureFunction = async function (context: Context, documents: any[]): Promise<void> {
    if (!!documents && documents.length > 0) {
        context.bindings.outputdocuments = documents;
    }

    context.done();
}

所以在context.done之后我想再触发一次

【问题讨论】:

    标签: azure azure-functions


    【解决方案1】:

    是的,您可以为此使用 azure function triggers

    function.json

    {
        "type": "cosmosDBTrigger",
        "name": "documents",
        "direction": "in",
        "leaseCollectionName": "leases",
        "connectionStringSetting": "<connection-app-setting>",
        "databaseName": "Tasks",
        "collectionName": "Items",
        "createLeaseCollectionIfNotExists": true
    }
    

    然后是js代码。

      module.exports = function (context, documents) {
          context.log('First document Id modified : ', documents[0].id);
    
          context.done();
        }
    

    因此,您可以有逻辑,例如从 http 触发器写入 cosmos db,并在将任何内容写入 cosmos db 后立即触发另一个触发器,依此类推

    PS。总而言之,由于我回答了您之前的问题,因此您将拥有 2 个函数,第一个是来自您的另一个问题,您将输出写入 cosmos db,第二个函数来自这个问题,一旦有 1 个函数将被触发完成执行和 cosmos db 中可用的数据

    【讨论】:

    • stackoverflow.com/questions/55644026/… 所以 Volodymyr - 它在这个问题的 context.done 之后。我想发送一个触发器。
    • 我需要做这样的事情吗:docs.microsoft.com/en-us/azure/azure-functions/…
    • 没有。您发布的链接是一种如何在不实际写入 cosmosdb 或队列的情况下测试您的队列/cosmosdb 触发器。
    • 所以基本上回到你的第一个问题,结果你将有 2 个 azure 函数,一个来自你的第一个问题,它将写入 cosmosdb,第二个来自当前问题,一旦有一个函数,你就会触发完成
    • @userMod2 我已经回答了 :)
    猜你喜欢
    • 2018-08-29
    • 1970-01-01
    • 2020-05-03
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2021-02-16
    • 1970-01-01
    相关资源
    最近更新 更多