Querying Build Status and Output
Fetching build status from the API
Once a build, either a single-file source one or one from a GitHub repository, has been accepted, you can use its BuildID
to query the status of the build from the API by making a GET
request to /builds/{buildID}
.
The Signaloid Cloud Compute Engine returns the Build status in the Status
field of the response object.
You should continue to poll the API until the build status is in a terminal state; i.e., either Completed
, Cancelled
, or Stopped
.
- TypeScript
- cURL
The code snippet below shows how to fetch the status of the task from the API using the axios
library.
interface BuildAPIResponse {
data: {
Status: string;
// Add other properties here
};
}
let buildIDFromResponse = buildPostResponse.data.BuildID;
let buildStatus = "";
let retryCountBuild = 0;
let retryDelayBuild = 2000;
let buildLog: string;
const maxRetryDelayBuild = 30000;
console.log("Waiting for the build to finish...");
await getBuildStatus(buildIDFromResponse);
// Helper functions for fetching build status with backoff
async function getBuildStatus(buildID: string) {
try {
// get build data from API
const response = await signaloidClient.get(`/builds/${buildIDFromResponse}`);
buildAPIResponseHandler(response);
} catch (error) {
console.log("Error:", error);
}
}
async function buildAPIResponseHandler(response: BuildAPIResponse) {
if ("Status" in response.data) {
buildStatus = response.data.Status;
console.log(`...build status : ${buildStatus}`);
} else {
throw new Error("Build status not found in response");
}
if (["Completed", "Cancelled", "Stopped"].includes(buildStatus)) {
// task is in a terminal state
console.log(`Build in terminal state : ${buildStatus}. Can fetch task outputs.`);
await getBuildOutputs(buildIDFromResponse); // `getBuildOutputs` is defined below.
} else {
// build is not in a terminal state. retry with delay
retryCountBuild += 1;
retryDelayBuild = Math.min(Math.pow(2, retryCountBuild) * 1000, maxRetryDelayBuild);
console.log(`Build still active : ${buildStatus}. Waiting for ${retryDelayBuild}ms and retying...`);
setTimeout(() => {
getBuildStatus(buildIDFromResponse);
}, retryDelayBuild);
}
}
The code snippet below shows how to fetch the status of the task from the API using curl
.
# The following calls to the Signaloid API check whether your build has completed its execution.
# Delay for consequtive calls to the API.
delayForAPIPolling=2
# The following call to the Signaloid API gets back the Build status.
# The call queries the `builds` endpoint using the Build ID. The output
# is redirected to the `jq` utility to retrieve the `Status` field.
buildStatus=$(curl -s --request GET --location https://api.signaloid.io/builds/$buildID \
--header "Authorization: $signaloidAPIKey" \
| jq -r '.Status')
while [ "$buildStatus" != "Completed" ] && [ "$buildStatus" != "Cancelled" ] && [ "$buildStatus" != "Stopped" ];
do
echo "Specified build is: \""$buildStatus\"". Waiting."
sleep $delayForAPIPolling
# The following call to the Signaloid API gets back the Build status.
buildStatus=$(curl -s --request GET --location https://api.signaloid.io/builds/$buildID \
--header "Authorization: $signaloidAPIKey" \
| jq -r '.Status')
done
echo "Build "$buildID" completed with status: \""$buildStatus"\""
Retrieving build outputs from the API
Once the build is in a terminal state, you can retrieve the outputs of the build from the API by making a GET
request to /builds/{buildsID}/outputs
. The output is the standard error output of the compilation process of the single file source code or GitHub repository that you chose.
- TypeScript
- cURL
The code snippet below shows an implementation of the getBuildOutputs
placeholder function referred to in the previous step that will fetch the outputs of the build from the API using the axios
library.
// Helper function for fetching task output
async function getBuildOutputs(buildID: string) {
console.log("Fetching build outputs...");
try {
// get build data from API
const buildOutputsResponse = await signaloidClient.get(`/builds/${buildID}/outputs`);
if (buildOutputsResponse.data.Build) {
const outputStream = await axios.get(buildOutputsResponse.data.Build);
buildLog = outputStream.data;
console.log(`Build Log: \n${buildLog}`);
}
} catch (error) {
console.log("Error:", error);
}
}
The code snippet below shows how to get the build standard error log (build
) from the API using curl
.
# Use this API endpoint to get the Build output using a BuildID
buildOutputsEndpointResponse=$(curl --request GET \
--location 'https://api.signaloid.io/builds/'$buildID'/outputs?sanitized=false' \
--header "Authorization: $signaloidAPIKey")
# The build outputs endpoint may contain multiple fields. You can use the following to get the field corresponding to `build`
buildLog=$(echo $buildOutputsEndpointResponse | jq -r '.Build')
if [ "$buildLog" == "null" ];
then
errorMessage=$(echo $buildOutputsEndpointResponse | jq -r '.error')
echo "Build outputs endpoint returned error message: \""$errorMessage"\""
exit 1
else
# Use curl to download and print the Build output
curl "$buildLog"
fi
The build outputs response will have the following shape:
{
"data": {
"Build": "...",
},
"status": 200,
"statusText": "Ok"
}
The Build
key of the data
key in this object is a URL to the file containing the build standard error output of the application. The stream may be empty for a build with no warnings or errors.