Zal
Zal

Reputation: 975

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 for one and a http trigger for the other, see src_http and src_timer below:

folders

This is definitely not desireable, since I am duplicating my code.

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.

Upvotes: 1

Views: 5634

Answers (2)

Kashyap
Kashyap

Reputation: 17441

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:

    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);
       }

    }

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"
            }
        ]
    }

Upvotes: 2

silent
silent

Reputation: 16138

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).

Upvotes: 2

Related Questions