Jolie on Azure Functions

Hi all. Some weeks ago Azure Functions' team released (as a preview) custom handlers.
From the official doc:
Custom handlers are best suited for situations where you want to:

  • Implement a function app in a language that's not officially supported.
  • Implement a function app in a language version or runtime not supported by default.
  • Provide more granular control over the function app execution environment.
I think this is a really powerful feature and you can actually find a lot of articles showing how to leverage it to write functions in Go, PHP, etc
Now I'm going to add yet another programming language to the list: Jolie.
Again, from the documentation:
To implement a custom handler, you need the following aspects to your application:
  • A host.json file at the root of your app
  • A function.json file for each function (inside a folder that matches the function name)
  • A command, script, or executable, which runs a web server
In addition, you have to supply all is needed for your function to run because we are going to deploy to a stateless environment (obviously JDK support is required on the target platform as Jolie is Java dependent).
I added a full Jolie distribution (what you find in JOLIE_HOME path) to my function project in jolie_bin directory and modified the launch script to point to it.

So my host.json looks like that:
{
  "version": "2.0",
  "httpWorker": {
    "description": {
      "arguments": ["jolie_services.ol"],
      "defaultExecutablePath": "jolie_func_executor.sh"
    },
    "extensionBundle": {
      "id": "Microsoft.Azure.Functions.ExtensionBundle",
      "version": "[1.*, 2.0.0)"
    }
  }
}
where jolie_services.ol actually contains the function's code.

Jolie native support for HTTP protocol makes it really easy to write a "webserver".
Unfortunately there's a problem because Azure Functions Host communicates with custom handlers on a dynamic port, on which your application has to be listening.
This port can be read by the FUNCTIONS_HTTPWORKER_PORT environment variable but when you define an InputPort in Jolie the Location parameter is static, for instance:
inputPort MyInput {
    Location: "socket://localhost:8180"
    Protocol: http
    Interfaces: MyInterface
}
Dynamic Locations aren't yet available in Jolie but there's a workaround using a constant.
In Location's definition, we refer to this constant:
Location: AzureFunctionsPort
In the launch command, we read the environment variable and pass it to our Jolie program setting the constant value:
-C AzureFunctionsPort="\"socket://localhost:$FUNCTIONS_HTTPWORKER_PORT\""
Here you can find a simple working example with detailed instructions to start from scratch.



Commenti

Post popolari in questo blog

Gateway Consorzio Triveneto per WP e-commerce

Jolie micro services on Kubernetes