Check the status of an external HTTP service

Description

In this tutorial you will learn how to create a script that periodically checks the availability of an external HTTP service.

The script will be executed every 30 seconds and report the service status as a boolean value.

@startuml
skinparam backgroundColor transparent

package "root/.../status" {
    [owner.scripts] as trigger
}

    node ospscripts as "osp-scripts"
    node external as "External service"
interface boolean as "Boolean value"

ospscripts -[#black]> trigger : <size:11><color:black>**executes**
trigger -[#navy]d-> external : <size:11><color:navy>**HTTP request**
external .[#navy]u.> trigger : <size:11><color:navy>**HTTP response**
trigger -[#dodgerblue]> boolean : <size:11><color:dodgerblue>**true/false**
@enduml

Steps

1. Create an integer value script that will be executed every 10 seconds

root/examples/scripts/external-service/status/value.ospp

{
    "name": "Service status",
    "description": "The status of an external service",
    "type": "BOOLEAN"
}

root/examples/scripts/external-service/status/owner.scripts

{
    "moduleId": "modules.scripts.scripts-1",
    "scheduledExecutions": ["0/30 * * ? * * *"],
    "accessedValues": [],
    "sourceFile": "root/scripts/external-service/status/script.js"
}

2. Write a script that issue an HTTP GET request to an external service and returns the result

root/examples/scripts/external-service/status/script.js

main();

function main() {
    let result = http.doGet("http://external-service.url");
    if (!result.isSuccess()) {
        log.warn("The service is unavailable cause [{}].", result.getError());
    }

    return result.isSuccess();
}