Serverless computing has been a growing technology in the cloud. These kind of architecture helps you build and run your applications without having to manage the infrastructure. Serverless computing allows you to completely focus on your business logic and leave all the headache of managing the servers, application servers, scaling, optimizing to the provider. IBM Bluemix, the cloud platform as a service (PaaS), has a service called OpenWhisk which allows each user to upload their programs, codes or functions and execute them through rest API.

In this article we will be building a simple chat application using OpenWhisk, a Docker container with Java 8 and Scala installed and IBM Cloudant database to store the chat informations. We will be creating a SCALA action which is a custom executable program packaged in a Docker container.

Architecture Diagram

architecture

Creating and Executing Scala Action
Following are the steps to create a Scala based function to run using OpenWhisk server. The github link to project can be found here.
1. Get and install wsk CLI. Make sure you have access to wsk CLI utility in your local machine. The wsk command allows you to run the function created in OpenWhisk server through command line. You can follow following link to setup wsk CLI.

https://console.ng.bluemix.net/openwhisk/cli

2. Run OpenWhisk server. You could create an instance of a OpenWhisk server from Bluemix console or you could run the OpenWhisk server locally into your machine by following the readme from the github.

https://github.com/openwhisk/openwhisk

3. Create a Dockerized Scala container. For this you could download a DockerSkeleton. Its a template to inject custom executables or binaries through Docker. You can download using command

wsk sdk install Docker

. The Dockerfile should have steps to install jdk 1.8+ and Scala 2.11.6+ so that we could compile and build the Scala based project that connects to the IBM Cloudant database for CRUD operations. The file should also contain steps to put Scala and Java runtime executables into to classpath. All the steps are defined in the Dockerfile.
From IBM Bluemix console create a Cloudant nosql service to store chat data information. Make sure you copy the credentials so that you can connect to the IBM Cloudant database for CRUD operations. The credentials attribute looks like the following:

username=
password=
host=
port=443
url=
dbname=
key=
passcode=

4. Create a Scala based project that exposes an API which basically connects to IBM Cloudant database to retrieve and store chat information. The Java and Scala module required for this is defined in build.sbt file within the project.
Build the Scala project locally. It creates a JAR file which is placed in the CLASSPATH so that the exec file will have access to it.

sbt clean assembly

5. Create a Scala exec file that is executable. The file name should be ‘exec’. This is a Scala class that has a main method which is executed by the OpenWhisk server when accessed through command line. This binary should be located in /action/exec in the Docker image. The executable receives two argument a command and the chat messages. Based on the command the logic to store or retrieve differs.

6. When the code base is ready, we need to build the Docker image and push it to the server using:

./buildAndPush.sh sanjeevghimire/Scalaexample

You should notice that the Scala executable is compiled part of Docker image
Now you can use the Docker container as OpenWhisk action. Create the action using
Following:

wsk action create --Docker Scalaexample sanjeevghimire/Scalaexample

You can now invoke the action you have just created by using the following script.
To send chat to the server you use:

wsk action invoke --blocking --result example --param  cmd sendChat --param message ‘hi there!’

Result:

{
      "chatTS": 1.485286534412e+12,
      "from": "openwhisk",
      "text": "I am good!",
      "to": "user"
  }

To get chat that you have sent use the following script:

wsk action invoke --blocking --result example --param command getChat

Result:

{
      "chatSessionId": "ZozbiUPzWiy7xPhA",
      "sessionStart": 1.485286490545e+12,
      "text": [
          {
              "chatTS": 1.485220172e+12,
              "from": "user",
              "text": "None",
              "to": "watson"
          },
          {
              "chatTS": 1.485281432e+12,
              "from": "user",
              "text": "how are you?",
              "to": "watson"
          },
          {
              "chatTS": 1.485220958e+12,
              "from": "user",
              "text": "hi",
              "to": "watson"
          },
          {
              "chatTS": 1.485220894e+12,
              "from": "user",
              "text": "hi",
              "to": "watson"
          }
      ]
  }

7. To update the Docker action, run buildAndPush.sh to upload the latest image to Docker Hub. This will allow the system to pull your new Docker image the next time it runs the code for your action.

 ./buildAndPush.sh sanjeevghimire/scalaexample

and

wsk action update --Docker sanjeevghimire/scalaexample

TroubleShooting

You can use the CLI to see the output logs to troubleshoot your code. Use the following script in a separate terminal to see the outputs.

wsk activation poll

Github Project

https://github.com/sanjeevghimire/openwhisk-Scala-Cloudant-action

Reference links
https://console.ng.Bluemix.net/docs/openwhisk/openwhisk_actions.html#openwhisk_actions_Docker
https://developer.ibm.com/openwhisk/
https://github.com/openwhisk/openwhisk

Join The Discussion

Your email address will not be published. Required fields are marked *