@agility/cli
Version:
Agility CLI for working with your content. (Public Beta)
263 lines • 16.7 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pollBatchUntilComplete = pollBatchUntilComplete;
exports.extractBatchResults = extractBatchResults;
exports.prettyException = prettyException;
exports.logBatchError = logBatchError;
var ansi_colors_1 = __importDefault(require("ansi-colors"));
/**
* Simple batch polling function - polls until batch status is 3 (complete)
*/
function pollBatchUntilComplete(apiClient_1, batchID_1, targetGuid_1, originalPayloads_1) {
return __awaiter(this, arguments, void 0, function (apiClient, batchID, targetGuid, originalPayloads, // Original payloads for error matching
maxAttempts, // 10 minutes at 2s intervals - increased from 120
intervalMs, // 2 seconds
batchType // Type of batch for better logging
) {
var attempts, consecutiveErrors, _loop_1, state_1;
if (maxAttempts === void 0) { maxAttempts = 300; }
if (intervalMs === void 0) { intervalMs = 2000; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
attempts = 0;
consecutiveErrors = 0;
_loop_1 = function () {
var batchStatus_1, dots, batchTypeStr, error_1, finalCheck, finalError_1, backoffMs_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 5, , 11]);
return [4 /*yield*/, apiClient.batchMethods.getBatch(batchID, targetGuid)];
case 1:
batchStatus_1 = _b.sent();
// Reset consecutive errors on successful API call
consecutiveErrors = 0;
if (!!batchStatus_1) return [3 /*break*/, 3];
// console.warn(`⚠️ No batch status returned for batch ${batchID} (attempt ${attempts + 1}/${maxAttempts})`);
attempts++;
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, intervalMs); })];
case 2:
_b.sent();
return [2 /*return*/, "continue"];
case 3:
if (batchStatus_1.batchState === 3) {
// console.log(`✅ Batch ${batchID} completed successfully after ${attempts + 1} attempts`);
// check for batch item errors
if (Array.isArray(batchStatus_1.items)) {
batchStatus_1.items.forEach(function (item, index) {
if (item.errorMessage) {
// show the error and the item separately
var itemClean = __assign({}, item);
delete itemClean.errorMessage;
console.error(ansi_colors_1.default.red("\u26A0\uFE0F Item ".concat(item.itemID, " (index ").concat(index, ") failed with error: ").concat(item.errorMessage)));
console.log(ansi_colors_1.default.gray.italic('📋 Batch Item Details:'));
console.log(ansi_colors_1.default.gray.italic(JSON.stringify(itemClean, null, 2)));
// FIFO matching: Show the original payload that caused this error
if (originalPayloads && originalPayloads[index]) {
console.log(ansi_colors_1.default.yellow.italic('🔍 Original Payload that Failed:'));
console.log(ansi_colors_1.default.yellow.italic(JSON.stringify(originalPayloads[index], null, 2)));
}
else if (originalPayloads) {
console.warn(ansi_colors_1.default.yellow("\u26A0\uFE0F Could not match payload at index ".concat(index, " (total payloads: ").concat(originalPayloads.length, ")")));
}
if (batchStatus_1.errorData) {
console.log(ansi_colors_1.default.red.italic('🔍 Additional Error Data:'));
console.log(batchStatus_1.errorData + "\n");
}
}
});
}
return [2 /*return*/, { value: batchStatus_1 }];
}
else {
dots = '.'.repeat((attempts % 3) + 1);
batchTypeStr = batchType ? "".concat(batchType, " batch") : 'Batch';
console.log(ansi_colors_1.default.yellow.dim("".concat(batchTypeStr, " ").concat(batchID, " in progress ").concat(dots)));
if (batchStatus_1.errorData) {
console.log("Error: ".concat(batchStatus_1.errorData));
}
}
attempts++;
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, intervalMs); })];
case 4:
_b.sent();
return [3 /*break*/, 11];
case 5:
error_1 = _b.sent();
consecutiveErrors++;
console.warn("\u26A0\uFE0F Error checking batch status (attempt ".concat(attempts + 1, "/").concat(maxAttempts, ", consecutive errors: ").concat(consecutiveErrors, "): ").concat(error_1.message));
if (!(consecutiveErrors >= 10)) return [3 /*break*/, 9];
console.warn("\u26A0\uFE0F ".concat(consecutiveErrors, " consecutive errors - batch ").concat(batchID, " may have failed or been deleted"));
_b.label = 6;
case 6:
_b.trys.push([6, 8, , 9]);
return [4 /*yield*/, apiClient.batchMethods.getBatch(batchID, targetGuid)];
case 7:
finalCheck = _b.sent();
if ((finalCheck === null || finalCheck === void 0 ? void 0 : finalCheck.batchState) === 3) {
console.log("\u2705 Batch ".concat(batchID, " was actually successful! Polling errors were transient."));
return [2 /*return*/, { value: finalCheck }];
}
return [3 /*break*/, 9];
case 8:
finalError_1 = _b.sent();
console.warn("Final batch check also failed: ".concat(finalError_1.message));
return [3 /*break*/, 9];
case 9:
attempts++;
if (attempts >= maxAttempts) {
throw new Error("Failed to poll batch ".concat(batchID, " after ").concat(maxAttempts, " attempts (").concat(consecutiveErrors, " consecutive errors): ").concat(error_1.message));
}
backoffMs_1 = Math.min(intervalMs * Math.pow(1.5, consecutiveErrors), 10000);
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, backoffMs_1); })];
case 10:
_b.sent();
return [3 /*break*/, 11];
case 11: return [2 /*return*/];
}
});
};
_a.label = 1;
case 1:
if (!(attempts < maxAttempts)) return [3 /*break*/, 3];
return [5 /*yield**/, _loop_1()];
case 2:
state_1 = _a.sent();
if (typeof state_1 === "object")
return [2 /*return*/, state_1.value];
return [3 /*break*/, 1];
case 3: throw new Error("Batch ".concat(batchID, " polling timed out after ").concat(maxAttempts, " attempts (~").concat(Math.round(maxAttempts * intervalMs / 60000), " minutes)"));
}
});
});
}
/**
* Extract results from completed batch
*/
function extractBatchResults(batch, originalItems) {
var successfulItems = [];
var failedItems = [];
if (!(batch === null || batch === void 0 ? void 0 : batch.items) || !Array.isArray(batch.items)) {
// All items failed if no items array
return {
successfulItems: [],
failedItems: originalItems.map(function (item, index) { return ({
originalItem: item,
error: 'No batch items returned',
index: index
}); })
};
}
// Process each batch item
batch.items.forEach(function (item, index) {
var originalItem = originalItems[index];
if (item.itemID > 0 && !item.itemNull) {
// Successful item
successfulItems.push({
originalItem: originalItem,
newId: item.itemID,
newItem: item,
index: index
});
}
else {
// Failed item
failedItems.push({
originalItem: originalItem,
newItem: null,
error: item.itemNull ? 'Item creation returned null' : "Invalid ID: ".concat(item.itemID),
index: index
});
}
});
return { successfulItems: successfulItems, failedItems: failedItems };
}
function prettyException(error) {
// TODO: regex out the exception type and message
// Item -1 failed with error: Agility.Shared.Exceptions.ManagementValidationException: The maximum length for the Message field is 1500 characters.
// at Agility.Shared.Engines.BatchProcessing.BatchInsertContentitem(String languageCode, BatchImportContentItem batchImportContentItem) in D:\a\_work\1\s\Agility CMS 2014\Agility.Shared\Engines\BatchProcessing\BatchProcessing_InsertContentItem.cs:line 398
// at Agility.Shared.Engines.BatchProcessing.BatchInsertContent(Batch batch) in D:\a\_work\1\s\Agility CMS 2014\Agility.Shared\Engines\BatchProcessing\BatchProcessing.cs:line 1212
}
/**
* Enhanced error logging for batch items with payload matching
* This helps identify which specific payload caused the error using FIFO matching
*/
function logBatchError(batchItem, index, originalPayload) {
console.error(ansi_colors_1.default.red("\u26A0\uFE0F Item ".concat(batchItem.itemID, " (index ").concat(index, ") failed with error:")));
console.error(ansi_colors_1.default.red(batchItem.errorMessage));
// Clean batch item for display
var itemClean = __assign({}, batchItem);
delete itemClean.errorMessage;
console.log(ansi_colors_1.default.gray.italic('📋 Batch Item Details:'));
console.log(ansi_colors_1.default.gray.italic(JSON.stringify(itemClean, null, 2)));
// Show the original payload that caused this error (FIFO matching)
if (originalPayload) {
console.log(ansi_colors_1.default.yellow.italic('🔍 Original Payload that Failed:'));
// Highlight key fields that might be causing issues
var keyFields = ['properties', 'fields', 'contentID', 'referenceName'];
var highlightedPayload_1 = {};
keyFields.forEach(function (field) {
if (originalPayload[field] !== undefined) {
highlightedPayload_1[field] = originalPayload[field];
}
});
// Show highlighted fields first
console.log(ansi_colors_1.default.yellow.italic('Key Fields:'));
console.log(ansi_colors_1.default.yellow.italic(JSON.stringify(highlightedPayload_1, null, 2)));
// Show full payload if needed for debugging
console.log(ansi_colors_1.default.gray.italic('Full Payload:'));
console.log(ansi_colors_1.default.gray.italic(JSON.stringify(originalPayload, null, 2)));
}
}
//# sourceMappingURL=batch-polling.js.map