Azure函数中的http和计时器都触发 [英] Both http and timer trigger in Azure Function
问题描述
通过复制项目文件夹,我可以在Azure函数中同时使用计时器和http触发器.这样,我就有一个单独的 function.json
,在其中可以为一个指定计时器触发器,为另一个指定一个http触发器,请参见 src_http
和 src_timer
下方:
I am able to have both a timer and http trigger in my Azure Function by duplicating the project folder. This way I have a seperate function.json
where I can specify a timer trigger for one and a http trigger for the other, see src_http
and src_timer
below:
这绝对是不可取的,因为我正在复制代码.
This is definitely not desireable, since I am duplicating my code.
问题:是否可以在一个功能中同时使用计时器和http触发器?
Question: Is there a way to have both a timer and http trigger in one function?
我阅读了此,看来这是不可能的,我希望我错了.
I read this and it looks like this is not possible, I hope I am wrong.
推荐答案
请参见导入行为.
在Java中,您可以执行以下操作,因为它在生成的 function.json
中使用 class-name.function-name
作为"scriptFile"
代码>:
In java you can do something like this because it uses class-name.function-name
as "scriptFile"
in generated function.json
:
public class EhConsumerFunctions {
private void processEvent(String request, final ExecutionContext context) {
// process...
}
@FunctionName("HttpTriggerFunc")
public void httpTriggerFunc(
@HttpTrigger(name = "req", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> req,
final ExecutionContext context) {
processEvent(req.getBody().get(), context);
}
@FunctionName("TimerTriggerFunc")
public void timerTriggerFunc(
@TimerTrigger(name = "timerRequest", schedule = "0 */5 * * * *") String timerRequest,
final ExecutionContext context) {
processEvent(timerRequest, context);
}
}
对于python,它采用脚本名称,并希望它具有 main
和单独的 function.json
.因此,您将必须有两个文件夹和两个脚本.但是每个脚本都可以 import
一个通用的业务逻辑模块来执行实际的处理.
For python, it takes script name and expects it to have a main
and separate function.json
. So you'll have to have two folders and two scripts. But each script can import
a common business logic module which does the actual processing.
类似的东西:
MyFunctionApp
|____ host.json
|____ business
| |____ logic.py
|____ http_trigger
| |____ __init__.py
| |____ function.json
|____ timer_trigger
|____ __init__.py
|____ function.json
http_trigger/__ init __.py
将具有:
from business import logic
def main(req: func.HttpRequest) -> func.HttpResponse:
return logic.process(req)
和 http_trigger/function.json
将具有:
{
"scriptFile": "http_trigger/__init__.py",
"disabled": false,
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
这篇关于Azure函数中的http和计时器都触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!