Skip to content Skip to sidebar Skip to footer

Both Http And Timer Trigger In Azure Function

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

Solution 1:

Then just don't duplicate your code ;) Move the common code that is used by both Functions into a common class etc. that you reference from the two. The two Functions itself only differ then in their signature (and how they are invoked under the hood).

Solution 2:

EDIT: See some official doc available now on Folder Structure and Import behavior.


In java you can do something like this because it uses class-name.function-name as "scriptFile" in generated function.json:

publicclassEhConsumerFunctions {
        privatevoidprocessEvent(String request, final ExecutionContext context) {
         // process...
        }

        @FunctionName("HttpTriggerFunc")publicvoidhttpTriggerFunc(
           @HttpTrigger(name = "req", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.ANONYMOUS)
           HttpRequestMessage<Optional<String>> req,
           final ExecutionContext context) {
           processEvent(req.getBody().get(), context);
       }

       @FunctionName("TimerTriggerFunc")publicvoidtimerTriggerFunc(
        @TimerTrigger(name = "timerRequest", schedule = "0 */5 * * * *") String timerRequest,
        final ExecutionContext context) {
           processEvent(timerRequest, context);
       }

    }

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.

Something like:

MyFunctionApp
|____ host.json
|____ business
|     |____ logic.py
|____ http_trigger
|     |______init__.py
|     |____ function.json
|____ timer_trigger
      |____ __init__.py
      |____ function.json

http_trigger/__init__.py will have:

from business import logic

def main(req: func.HttpRequest) -> func.HttpResponse:
    return logic.process(req)

and http_trigger/function.json will have:

{"scriptFile":"http_trigger/__init__.py","disabled":false,"bindings":[{"authLevel":"function","type":"httpTrigger","direction":"in","name":"req"},{"type":"http","direction":"out","name":"res"}]}

Post a Comment for "Both Http And Timer Trigger In Azure Function"