8 Serverless (FaaS)
verita84 edited this page 2021-11-15 19:28:05 +00:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

With this feature, applications can spin up containers themselves and retrieve data from the PiCluster server. Lets explore how this works. First, upload function-sample.zip from picluster/example/x86_64/ubuntu in the Image -> Manage -> Upload section of the PiCluster Web Console. Next, create a function as shown below.

Pic Below is the sample code for the function. It is a simple Node.js application that sends Hello, World! back to the server as the output.

var request = require('request');
var server = process.env.SERVER;
var token = process.env.TOKEN;
var uuid = process.env.UUID;
var output = '';

function sendOutput(what) {

  const body = JSON.stringify({
    token,
    output: what,
    uuid
  });

  const options = {
    url: server + '/function',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': body.length
    },
    body
  };

  request(options, (error, response) => {
    if (error) {
      console.log('An error has occurred.' + error);
    }
  });
}

sendOutput('Hello, World!');

When a function is finished running, the container is automatically deleted and the output is stored on the server. When the application requests the data from the server, the data is removed as well.

Pic In the screenshot above, you can see the current functions running on the cluster where the data is not retrieved yet. There is also another option under the functions category to remove all the functions and the output data stored.

In the screenshot below, you can see the function viewer. From here, you can select a function where the output was not retrieved yet by an application and view the output. You will also notice that there is a curl command in the Data Retrieval text box. For testing, you can copy and paste the contents into a terminal to see the output from the function.

Pic