手动触发Azure功能

手动触发Azure功能

本文介绍了手动触发Azure功能-时间触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure函数,它每周一次在计时器触发器上运行.这可以很好地工作,并且符合预期,但是每个月大约需要一到两次,用户需要根据要求运行该功能,因此我需要在该功能上做一个发布以触发它-就像您可以在Azure门户中进行的操作一样.

I have an Azure Function that runs on a timer trigger once a week. This works great and as expected, but about once or twice a month, a user needs this function to run upon request, so I need to do a post to the function to trigger it - much like you can do from the Azure portal.

查看Azure门户,正在对以下功能进行http发布请求:

Looking at the Azure portal, a http post request is being done to the function like:

https://{funcapp}.azurewebsites.net/admin/functions/{func}

但是,如果我从Postman进行此操作,则会收到Http 401响应.我将如何去做这个请求?

However, if I do this from Postman, I get a Http 401 response. How would I go about to do this request?

我可以选择一个选项,而不是将触发器更改为队列,并每周运行第二个函数,该函数会将消息添加到队列中,但这对我来说似乎有点过分.

One option I have a to rather change the trigger to a queue and have a second function run on the weekly basis that would add a message to the queue, but this seems a bit excessive to me.

推荐答案

如果您使用单个功能应用程序可以由多个功能组成的事实来共享功能之间的业务逻辑,该怎么办?然后,您可以使一个function.json触发器基于HTTP请求,而另一个触发器基于计时器.

What if you share business logic between functions by using the fact that a single function app can be composed of multiple functions? Then you can have one function.json trigger based off of an HTTP request and the other trigger based off of a timer.

您的功能应用架构可能如下所示:

Your function app architecture could look like:

MyFunctionApp
|     host.json
|____ shared
|     |____ businessLogic.js
|____ function1
|     |____ index.js
|     |____ function.json
|____ function2
      |____ index.js
      |____ function.json

在"function1/index.js"和"function2/index.js"中

var logic = require("../shared/businessLogic");

module.exports = logic;

function1和function2的function.json可以配置为不同的触发器(计时器和HTTP或队列...任何您想要的!).

The function.json of function1 and function2 can be configured to different triggers (timer and HTTP or queue... whatever you want!).

在"shared/businessLogic.js

module.exports = function (context, req) {
    // This is where shared code goes. As an example, for an HTTP trigger:
    context.res = {
        body: "<b>Hello World</b>",
        status: 201,
        headers: {
            'content-type': "text/html"
        }
    };
    context.done();
};

(这是一个JavaScript示例,但其他语言也一样!)

(This is a JavaScript example, but same holds for other languages!)

这篇关于手动触发Azure功能-时间触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:38