Intranoggin

Blither, Blather, Web Content Management.
Blog » Alexa on Azure: Part 7–Keep it Alive

Alexa on Azure: Part 7–Keep it Alive

Azure functions run and scale on demand. As a consequence of this, they will be removed from memory altogether if they are not called frequently enough, specifically after 15 minutes. Since we want our Alexa Skill to respond quickly every time, we’ll want to avoid the reload penalty by ensuring our function is called at least once every 15 minutes. We’ll accomplish this through another timer function.

In our Alexa Functions project, add a new function and select “TimerTrigger – C#” as our function type. Give it a name, and set the schedule to 0 */15 * * * *, which is a CRON expression for every 15 minutes.

You can get more info on these expressions in the Azure Functions Timer documentation.

Create Timer Function

Tip: You actually may want to use 0 */1 * * * * during your debugging phase, which will execute the function once/minute. You can then update this value in your function.json file.

All we need our function to do is call each of the http functions we want to keep alive. In my case it is AlexaAskTeenageDaughter and AlexaAskTeenageSon. I don’t even need to do anything with the response I get, so my run.csx code is dead simple:

Run.csx

Of course this is available on GitHub.

You can see I make use of two environmental variables, AlexaAskTeenageDaughterURL and AlexaAskTeenageSonURL. I want to make sure I can set these variables in the functions’ application settings so the code can roll through the environments and always reference the correct urls for these services.

To create these environmental variables, navigate to your function within the Azure Portal. For one of the functions you will be keepign alive, copy it’s function url from the top of the development tab, and then click on the Function app settings link.

Function App Settings

In the Manage section, click the Go to App Service Settings button.

Go to App Service Settings

Click on the Application Settings tab and then add your key/value pairs for the URLs in the App Settings section.

Add App Settings

If you have more than one environment that you’ll be deploying, you may wish to go into the functions within each environment and proactively create these key/value pairs.

If you deploy this code now, it will work. However, because our timer executes a Get  and our http functions are expecting a post with a json object in the body, it will result in errors every time the function gets executed. Technically it will keep our functions alive, which meets the basic need, but I don’t like the idea clogging up our logs with a bunch of intentional errors. To keep our logs clean, I went back into our run.csx files for both AlexaAskTeenageDaughter and AlexaAskTeenageSon and added this small if statement at the top to do a quick return if we don’t have the body of the request object populated.

HTTP function adjustment


Posted: 3/14/2017 7:15:00 AM by Ryan Miller | with 0 comments