fabric-ias
Version:
Node.JS Service for Microsoft Fabric supporting infrastructure as code
64 lines (61 loc) • 2.31 kB
JavaScript
;
const Base = require("./base");
/**
* @class Operation
* @classdesc
* Handles polling and status tracking for long-running operations in Microsoft Fabric.
* Extends the Base class for authentication and API request handling.
*
* @extends Base
*
* @property {string} id - The operation ID.
* @property {number} _retry - The retry interval in milliseconds.
* @property {string} endpoint - The API endpoint for the operation.
*/
class Operation extends Base {
/**
* Constructs an Operation instance for a given operation ID.
* @param {AzOauth} OAuthHandler - OAuth handler for authentication.
* @param {string} operations_id - The operation ID.
* @param {number} [retryAfter=20000] - The retry interval in milliseconds.
*/
constructor(OAuthHandler, handler, operations_id, retryAfter = 20000) {
super(OAuthHandler, handler, retryAfter);
this.id = operations_id || '';
this._retry = retryAfter;
this.endpoint = `https://api.fabric.microsoft.com/v1/operations/${this.id}`;
}
/**
* Polls the operation status until it succeeds or fails.
* Calls the provided callback with status and percent complete on each poll.
*
* @param {function} [callback] - Optional callback(status, percentComplete) for progress updates.
* @returns {Promise<Object|Error>} The final operation status data if succeeded.
* @throws {Error} If the operation fails or times out, or if the API returns an error.
*/
async pollStatus(callback = null) {
let status = "",
response,
progress,
error;
timeout = 90;
while (status != "Succeeded") {
if (timeout <= 0) {
throw Error('Operation Timed Out: Unable to complete operation');
}
await this.wait();
response = await this._get('', {}, function (response) {
if (response.data.error != undefined || response.data.status == 'Failed') throw Error(`${response.data.error.message} Code: ${response.data.error.errorCode}`);
});
status = response.data.status;
progress = response.data.percentComplete || 0;
error = response.data.error;
if (callback != undefined) {
callback(status, progress);
}
timeout -= 1;
}
return response.data;
}
}
module.exports = Operation;