You might have multiple connections and models that should run in a specific order. Perhaps a model that sends emails based on a HubSpot connection. In those scenarios it can be helpful to orchestrate your assets.

We currently only support orchestration by creating a Python connector that triggers each of your assets in the right order. It might sound complex, but it isn’t. Follow the guide below and you should be good-to-go in less than 5 minutes.

How-To

  1. Find the IDs of the asset you want to run. You can find these from the folder overview by hovering the asset icon.
  1. Get your base URL. In this case, I should store the “demo”-part.
  1. Create a bearer token. Go to View Profile in the bottom, left-hand corner and navigate to API Tokens. Create a token and save it. Note that only Super-Admins can create tokens.
  2. Copy/Paste the script below into a Python connector.
  3. Replace the values shown below with the values from Step #1-#3. You can add as many asset_ids as you want to.

Script

orchestration template
## Import Less functions from helpers
from helpers.lessFunctions import *

## Import required library
import requests
import time
from datetime import datetime
import pandas as pd
import json

# Generate a results object for logging in the platform
result = {}
result['startedAt'] = str(pd.to_datetime(datetime.now()))
result['event_type'] = "result"
result['status'] = '200'
result['records'] = 0

try:
    # Insert the assets that you would like to run in the order that you would like to run them
    asset_ids = [
        {"id": 1, "type": "datamodels"}, 
        {"id": 2, "type": "connections"},
    ]

    # Insert the base url below
    baseURL = "" 
    bearer = "" 

    # Insert token below instead of "abc"
    headers = {"Authorization": "Bearer {}".format(bearer)}

    for asset_id in asset_ids:
        sendLogMessage("Running asset ID: {}".format(asset_id["id"]), "info")

        not_finished = True
        
        try:
            url = "https://{}.less.tech/api/public/{}/{}/execute".format(baseURL, asset_id["type"], asset_id["id"])
            r = requests.request("GET", url, headers=headers)
            task = r.json()["data"]["id"]
            sendLogMessage("Started executing: {} for asset: {}".format(task, asset_id["id"]), "info")
        except Exception as e:
            sendLogMessage("Failed to start executing: {} for asset: {}".format(task, asset_id["id"]), "error")

        while not_finished:
            url = "https://{}.less.tech/api/public/{}/status/{}".format(baseURL, asset_id["type"], task)
            r = requests.request("GET", url, headers=headers)
            if r.json()["data"]["container_status"] == "STOPPED":
                if r.json()["data"]["run_code"] != 200:
                    sendLogMessage("An error happened for asset: {}".format(asset_id["id"]), "error")
                    not_finished = False
                else:
                    print("Successfully ran")
                    sendLogMessage("Successfully run", "info")
                    not_finished = False
            else:
                not_finished = True
                sendLogMessage("Job is not finished yet", "info")
                time.sleep(10)

    result['stoppedAt'] = str(pd.to_datetime(datetime.now()))
    result['records'] = 0
    sendResultMessage(result['status'], result['startedAt'], result['records'],result['stoppedAt'], error="")
    
    
except Exception as e:
    #error out exeptions
    result['status'] = '500'
    result['records'] = 0
    result['stoppedAt'] = str(pd.to_datetime(datetime.now()))
    result['error'] = str(e)
    sendResultMessage(result['status'], result['startedAt'], result['records'], result['stoppedAt'], error=result["error"])