Skip to main content

Querying Task Status and Output

Fetching task status using the API

Once a task, has been accepted, you can use its TaskID to query the status of the task from the API by making a GET request to /tasks/{taskID}. The Signaloid Cloud Compute Engine returns the task status in the Status field of the response object. You should continue to poll the API until the task status is in a terminal state; i.e., either Completed, Cancelled, or Stopped.

The code snippet below shows how to fetch the status of the task from the API using the axios library.

interface TaskAPIResponse {
data: {
Status: string;
// Add other properties here
};
}

let taskID = taskPostResponse.data.TaskID;
let taskStatus = "";
let retryCount = 0;
let retryDelay = 2000;
let taskStdout: string;
const maxRetryDelay = 30000;

console.log("Waiting for the task to finish...");
await getTaskStatus(taskID);

// Helper functions for fetching task status with backoff
async function getTaskStatus(taskID: string) {
try {
// get task data from API
const response = await signaloidClient.get(`/tasks/${taskID}`);
taskAPIResponseHandler(response);
} catch (error) {
console.log("Error:", error);
}
}

async function taskAPIResponseHandler(response: TaskAPIResponse) {
if ("Status" in response.data) {
taskStatus = response.data.Status;
console.log(`...task status : ${taskStatus}`);
} else {
throw new Error("Task status not found in response");
}

if (["Completed", "Cancelled", "Stopped"].includes(taskStatus)) {
// task is in a terminal state
console.log(`Task in terminal state : ${taskStatus}. Can fetch task outputs.`);
await getTaskOutputs(taskID); // `getTaskOutputs` is defined below.
await getTaskOutputsChunks(taskID) // `getTaskOutputsChunks` is defined below. To be used as an alternative if stdout is large.
} else {
// task is not in a terminal state. retry with delay
retryCount += 1;
retryDelay = Math.min(Math.pow(2, retryCount) * 1000, maxRetryDelay);
console.log(`Task still active : ${taskStatus}. Waiting for ${retryDelay}ms and retying...`);
setTimeout(() => {
getTaskStatus(taskID);
}, retryDelay);
}
}

Retrieving task outputs from the API

Once the task is in a terminal state, you can retrieve the outputs of the task from the API by making a GET request to /tasks/{taskID}/outputs.

The code snippet below shows an implementation of the getTaskOutputs placeholder function referred to in the previous step that will fetch the outputs of the task from the API using the axios library.

// Helper function for fetching task output
async function getTaskOutputs(taskID: string) {
console.log("Fetching task outputs...");
try {
// get task data from API
const taskOutputsResponse = await signaloidClient.get(`/tasks/${taskID}/outputs`);
if (taskOutputsResponse.data.Stdout) {
const outputStream = await axios.get(taskOutputsResponse.data.Stdout);
taskStdout = outputStream.data;
console.log(`Task Stdout: \n${taskStdout}`);
}
} catch (error) {
console.log("Error:", error);
}
}

The code snippet below shows how to get the first compressed chunk of standard output (stdout) of the task from the API using the axios library.

// Replace the import with dynamic import after: npm install pako
const getPako = async () => {
try {
return require('pako');
} catch (e) {
// If pako isn't installed, install it on the fly
require('child_process').execSync('npm install pako --no-save');
return require('pako');
}
};

async function getTaskOutputsChunks(taskID: string) {
console.log("Fetching task outputs...");
try {
const pako = await getPako();

// Rest of your function remains the same
const taskOutputsResponse = await signaloidClient.get(`/tasks/${taskID}/outputs`);
if (taskOutputsResponse.data.StdoutChunks.length) {
const response = await axios.get(taskOutputsResponse.data.StdoutChunks[0], {
responseType: "arraybuffer",
});
const decompressed = pako.inflate(new Uint8Array(response.data));
const decompressedStdOutChunk = new TextDecoder("utf-8").decode(decompressed);
console.log(`Task Stdout First Chunk: \n${decompressedStdOutChunk}`);
}
} catch (error) {
console.log("Error :>> ", error);
}
}

The task outputs response will have the following shape:

{
"data": {
"Stdout": "...",
"Stderr": "...",
"StdoutChunks": "..."
},
"status": 200,
"statusText": "Ok"
}

Each key of the data key in this object will be a URL to the file containing the output of the task from corresponding output stream. The Stdout and Stderr streams may not be available for tasks that failed.

Output of an uncertainty-tracking (UT) C0 or C0 Pro core

For each floating-point variable that you print in the stdout or stderr of your application, the Signaloid Cloud Compute Engine also prints the distributional information of the variable in the form of the Uncertainty Hexadecimal (“Ux”) data format. You can instruct the Signaloid Cloud Compute Engine to avoid printing the Ux data format by using the particle %P modifier when printing floating-point values. You can feed the Ux data format in other Signaloid Cloud Compute Engine endpoints, e.g., for sampling or plotting the associated distributional information.

For example, the file containing the output of the Stdout stream should contain text similar to the following:

Alloy strength (σc)     = 6.5E+02Ux0400000000000010004086182D94915B0500000040405417BDA0AC9B250188F2CF171EF8E0405B6757D802080F01AA44F4C3CE568040605E6EDBDD0AA702040E39582CDA604062549EC48E6D54020049C5A87763C040640CD2FABF29F1022269E95ECAD3204065A2D65BCAA361022F84B7CF914A00406727AAD835034E022C821EA01770804068ABA18A9EE5DC0233ECFFBF89D480406A1A71BCBA96AA021309E840103300406B7F731CA16D9D023008CD59709980406CEA500FE458BE0216D7D76EA0CD60406E4625AE7BBFB2020894010FDC81C0406FAE24D94A8ED3022E84F454DD4DC0407091C94C35C8CD021F4E4D67909B6040714ADA1EC604E7020934E1AD5155C04072073BE9D7C24002380BAD55143FA04072C877D44122360219C4964B2DEDC040738B6D24BC0CB3023624DCF65BCEE0407468DAE5A46FD102348E828B9D4040407539A19D9F821B022AA3BD78F283204076167CFC01CEE8021EAFD30F8D4F804076FEEB22272855022FD02B048E12004077F8D99DA024F4021FBFEDEFCF39E0407911F818FF1163024B9A05B43447E0407A44B81557DD0B020AC5B8BAED2980407B8E07EC978C0101FD737CB6050FB0407D0E31FBF77D3601C9D92AB5DDC9C0407EE3846D4850FB01B43118EE7BDFA040807F8E3216120901A47793B2ACECE04081B8804AECCFDC01C654E526CDF2404082F417B2B6626101E413F66F2E0BE040840D1DBA88D35701CEBE2AB6CD70C0408523C73B05C890021438342616F4C04086298D67225EC6020013D66E8E76C040871E9A2AA378710230A511292A23804087F9B09002581701D759C8D68904604088C7B205CAB0AB021E0FB3C15CA2804089965E2291CDE7021DA9F67BE6FF20408A698B9837CD60024A1C4F46724F80408B36CFF7F80044020D42C0D2B48740408BF982A4EA135402177F700F0C7260408CB56FF95BF061020DC70452029680408D7F1051A80FE2024B9180F3388240408E4C026194A08602254479374CCCE0408F11E4C8CC147E0233CFEF28534EA0408FE9FE2B2171910259E3427835FB0040905B657856A26A01FFD69C48900AE04090C529DBCEB142023ECCA96C147E40409131C144D49BCE01F9712FECEDF4E040919F70F107944E022F3F8EEA118040409217F42CBAA75902099DCD5C4478804092925B872D66D202148613EE120C60409321859F3242A901CDABBE98092FD04093B3D8892B772B01C170077C3F2280409447C6AFCE86FE01B05B17344BD5E04094E93148DE8CB301DE50DA37D36D0040959763034D4D0E01C5164F700D8CA040965B0C13AC387B01FD2C401BCDB7E04097247B8B31A85E01A2730BD05949004097FDF4B706DFC001E33E60D12A90D04098F3FDDF43582D01BA699075EEE5D0409A0E41FFBE591C01CABF57DA2AF080409B90AD8877191901BC28A982C0B7C0409E41771B832D090117EFFC3F286460 MPa

(Note that Axios will automatically parse the text to get "un-escape" special characters in the output.)

Output of a C0 or C0 Pro Reference core

For each floating-point variable that you print in the stdout or stderr of your application, the Signaloid Cloud Compute Engine also prints a ValueID field which is a reference to the distributional data. You can feed this ValueID in other Signaloid Cloud Compute Engine endpoints, e.g., for sampling or plotting the associated distributional information.

For example, the file containing the output of the Stdout stream should contain text similar to the following:

Alloy strength (σc)     = 4.5E+02<ValueID>val_80e5b9ed7a2a530eb1d15bc32ef5b337</ValueID> MPa

(Note that Axios will automatically parse the text to get "un-escape" special characters in the output.)