1

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.

2 Answers 2

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:

    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"
            }
        ]
    }
Sign up to request clarification or add additional context in comments.

1 Comment

This is really helpful, thank you for the extensive response.
2

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

3 Comments

I tried this, so I had a different folder with the functional code, let's call this src, then made folders src_http and src_timer where I imported the code from src. But I was not able to get this to work since I couldn't import code from a different folder and this src folder is not deployed to Azure.
I'm not big into python tbh but I know that this should definitely work and is not a limitation of Functions.
Thanks for the response, I will look into this and will post my findings here so others can find this as well. Just to be sure, so you would make three separate folders as well? src which has all the functional code of the project. src_http where http logic is defined and which imports src. Finally src_timer, same as http but timer logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.