Serverless Functions for Business Logic
This section discusses how to implement business behavior using serverless functions.
What is a Trillo Serverless Function?
Trillo serverless function implements some business logic and it can be invoked using restful API.
A serverless function is automatically published as an API.
Create a Function
Creating a serverless function using Trillo Workbench is simple. Simply select the Functions menu options in the left navigation. Select + New Function.
Currently, Trillo Workbench supports only Java and Python languages.
Say, you create a function by giving it a name as MyFunction. The function will be created with the following stubbed code.
In the above code, MyFunction is the name of the function. It has a method 'postMethodChangeMe'. This serves as an API endpoint. It is of type HTTP post due to the annotation above the method. Note that the function implements the ServerlessFunction class (which implements TrilloFunction). More about it below.
THe URL that will target this function and its method would be as follows:
<hostname>/ds/function/shared/MyFunction/postMethodChangeMe
where,
ds/function indicates the service of Trillo Workbench (data dervice and its function module).
shared is package (folder) containing the function.
MyFunction is the name of the function that is the target of URL.
postMethodChangeMe is the method that will be invoked due to API call.
A function can support more than one endpoint (callable methods as API). The names of the methods can be anything (generally reflect the purpose). Say the above MyFunction needs to support the following 4 methods:
saveValue
updateValue
getValue
removeOverheads
The refined MyFunction will be as follows.
In this case, the function will publish 4 endpoints.
a post endpoint as /ds/function/shared/MyFunction/saveValue.
a put endpoint as /ds/function/shared/MyFunction/updateValue.
a get endpoint as /ds/function/shared/MyFunction/getValue.
a delete endpoint as /ds/function/shared/MyFunction/removeOverheads.
ServerlessFunction (BaseClass of Trillo Function)
ServerlessFunction is the base class of Trillo functions class. This encapsulates runtime context provides access to its properties such as user identity, task id, stateMap. All These properties are available to any subclass using accessor methods.
Parameter Name
Description
isOfUser
Internal identifier of the current user (on whose behalf the call is made, it may be a system user).
userId
User id (assigned to the user when it was created, also login userId)
firstName
First name of the user
lastName
Last name of the user.
Email of the user.
role
Roles assigned to the user.
emailVerified
A flag indicating if the user's email has been verified or not.
tenantId
If the deployment is a multi-tenant then it returns the id of the tenant.
tenantName
If the deployment is a multi-tenant then it returns the name of the tenant.
executionId
If the function is running as a background job then it gives the identifier which is used to track the task in the database (it is internal is you can ignore it barring an advanced use-case).
taskName
If the function is running as a background job then it gives the name of the task. It may be useful for logging.
stateMap
An arbitrary java.util.map/Dict can be used to store some state information. This is useful when a function calls another function. This map is passed back to the calling function with any updates made by the caller function. This is useful, for example, for building a lookup cache in a chain of function.
All Trillo serverless function runs within a transaction boundary. By default, the transaction is rolled back if the function invocation returns a Result object with an error. Since a function may run a few steps that may be committed irrespective of the final result, Trillo SDK provides an API to commit the transaction. After a transaction is committed, a new transaction is created.
Sample Hello World Function
The sample code with a method called "sayHello" as the endpoint:
Notice that this method simply echos "Hello from <current user id>". The current user id is accessed from the runtime context. The function adds the parameter "youSaid" if it is passed.
Test Function
The sample code with a method called "sayHello" as the endpoint:
Result
Notice in the above figure, that the actual result is wrapped inside an object and passed as its data attribute. The wrapper class is Result (a Java class/ python class). It contains the actual result in the data attribute. It has other attributes for status, error message, detailed message, etc. A special attribute called _rtag is included with a special value _r_ to assist a JavaScript client in identifying if the returned value is an instance of Result.
Log Messages In a Function
The serverless function can use logging APIs available in Trillo SDK. See the modified example code below:
Audit Logs
Audit logs are similar to the log messages discussed above with the distinction that these calls also log messages in the database. This is useful for auditing important business events. It is also useful to track, troubleshoot, and monitor the status of long-running jobs.
The following code shows how to use logging vs audit logs.
"actionName" can be any text. It is generally the action that caused this log, such as "BucketFileWrite". It is used to filter logs.
Samples
Trillo SDK (APIs) in a Function
Trillo SDK provides several APIs to simplify writing a serverless function code. These APIs include:
Database access
Google cloud service
External restful service
CVS, JSON file processing
...
...
many more
Not only Trillo SDK functions but several open-source libraries that are integrated with the Trillo runtime, become available for use in your serverless functions (such as Apache Commons for example).
Version 1 Serverless Function
Trillo function Version 1 (V1) supported only one endpoint per function, landing into a default method called "handle". They were always invoked using HTTP-Post irrespective of API semantics. An example of V1 is shown below.
Methods of Version 1 Function
A V1 serverless functions generally have the following two methods. The first, method called 'handle', is the default method and always required. The second method is optional. It could have been renamed or merged into first one.
handle: This is the entry point of the function that is invoked by the runtime by passing ScriptParameter (see below).
_handle: A private method that is invoked by the method handle.
The 'handle' method catches all exceptions, unwraps all error messages, and passes as a Result object.
You should implement your code in the _handle (you can rename it). T
he endpoint of published by the function is /ds/function/MyFunction.
ScriptParameter
ScriptParamter provides equivalent functionality as provided by the superclass (ServerlessFunction) of Version 2 Trillo functions. It provides the same accessor methods as provided by the ServerlessFunction. In addition to it, it provides a method "getV" to retrieve the parameter passed to the function.
Last updated