问题描述
我有一个每周在计时器触发器上运行一次的 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 post 请求,例如:
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.
推荐答案
如果利用单个函数应用可以由多个函数组成的事实,在函数之间共享业务逻辑怎么办?然后你可以有一个基于 HTTP 请求的 function.json 触发器和另一个基于计时器的触发器.
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 函数 - 时间触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!