UNPKG

redis-cloud-api-sdk

Version:
75 lines (74 loc) 3.41 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Task = void 0; class Task { constructor(client) { this.client = client; } /** * Waiting for task status to change to a given status * @param taskId The id of the task * @param expectedStatus The expected status * @param timeoutInSeconds The timeout of waiting for the status. Default: 20 minutes * @param sleepTimeInSeconds The sleep time between requests. Default: 5 seconds */ waitForTaskStatus(taskId, expectedStatus, timeoutInSeconds = 20 * 60, sleepTimeInSeconds = 5) { return __awaiter(this, void 0, void 0, function* () { let task = yield this.getTask(taskId); let timePassedInSeconds = 0; while (task.status !== expectedStatus && task.status !== 'processing-error' && task.status !== undefined && timePassedInSeconds <= timeoutInSeconds) { this.client.log('debug', `Waiting for task ${taskId} status '${task.status}' to be become status '${expectedStatus}' (${timePassedInSeconds}/${timeoutInSeconds}`); yield this.client.sleep(sleepTimeInSeconds); timePassedInSeconds += sleepTimeInSeconds; task = yield this.getTask(taskId); } this.client.log('debug', `Task ${taskId} ended up as '${task.status}' status after ${timePassedInSeconds}/${timeoutInSeconds}`); if (task.status === 'processing-error' && task.response.error !== undefined) { const errorType = task.response.error.type; const errorStatus = task.response.error.status; const errorDescription = task.response.error.description; console.log(`Task ${taskId} ended up in error: type: ${errorType}, status: ${errorStatus}, description: ${errorDescription}`); } return task; }); } /** * Returning a lookup list of tasks owned by the account */ getTasks() { return __awaiter(this, void 0, void 0, function* () { try { const response = yield this.client.get('/tasks'); return response.data; } catch (error) { return error; } }); } /** * Returning a task * @param taskId The id of the task */ getTask(taskId) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield this.client.get(`/tasks/${taskId}`); return response.data; } catch (error) { return error; } }); } } exports.Task = Task;