Using a Task to Periodically Pull in External Data

This topic provides information about Tasks and how to create and manage them.

A Task can be created to periodically pull in data from external systems. For example, you could use a Task to invoke a REST request on an endpoint of an external cloud service to pull in data, and then add that data as an Event into a Stream within Octave.

Follow the steps below to create a Task that invokes a GET request on a weather service endpoint and adds the whether data as an Event to a Stream named offline:

  1. Create a Task.
  2. Within the Task's JavaScript function invoke Octave's Octave.Http.get() cloud API to invoke a GET on the weather service endpoint and place the results in a new JSON object:
function() {

var headers={'Content-Type': 'application/json'};

var weather_url = 'https://api.openweathermap.org/data/2.5/onecall?lat=43&lon=1&exclude=hourly,daily,minutely&appid=56040...';

   var weather_response = Octave.Http.get(weather_url,headers);

   var result = {};

       var event =  {
           elems : {
              'weather':JSON.parse(weather_response.content)
           },
		   hash : "weather"
        };
        result['/my_company/weather'] = [event];
   
   return result

}

When the Task executes, an Event similar to the following is added to the Stream:

{
  "/my_company/weather": [
    {
      "elems": {
        "weather": {
          "alerts": [
            {
              "description": "Although rather usual in this region, locally or potentially dangerous phenomena are expected. (such as local winds, summer thunderstorms, rising streams or high waves)",
              "end": 1611154800,
              "event": "Moderate avalanches warning",
              "sender_name": "METEO-FRANCE",
              "start": 1611068400
            }
          ],
          "current": {
            "clouds": 0,
            "dew_point": 270.72,
            "dt": 1611080131,
            "feels_like": 274.58,
            "humidity": 53,
            "pressure": 1017,
            "sunrise": 1611040926,
            "sunset": 1611075061,
            "temp": 279.26,
            "uvi": 0,
            "visibility": 10000,
            "weather": [
              {
                "description": "clear sky",
                "icon": "01n",
                "id": 800,
                "main": "Clear"
              }
            ],
            "wind_deg": 184,
            "wind_speed": 3.32
          },
          "lat": 43,
          "lon": 1,
          "timezone": "Europe/Paris",
          "timezone_offset": 3600
        }
      },
      "hash": "weather"
    }
  ]
}