@adobe/lightroom-apis
Version:
Adobe Firefly Services library for consuming Lightroom Service APIs.
135 lines (134 loc) • 5.91 kB
JavaScript
"use strict";
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
**************************************************************************/
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.LrAsyncJob = exports.LrAsyncJobError = void 0;
const internal_1 = require("@adobe/firefly-services-sdk-core/internal");
const JobStatus_1 = require("../src/models/JobStatus");
class LrAsyncJobError extends Error {
constructor(message, details) {
const errorMsg = `message : ${JSON.stringify(details, null, 2)}`;
super(errorMsg);
this.details = details;
}
}
exports.LrAsyncJobError = LrAsyncJobError;
/**
* This is manually created class to handle the async job status for Lightroom APIs
*/
class LrAsyncJob extends internal_1.AsyncJobPromise {
constructor(_jobLinkPromise, _statusFetcher) {
super();
this._jobLinkPromise = _jobLinkPromise;
this._statusFetcher = _statusFetcher;
this._jobId = undefined;
}
_fetchJobStatus() {
return __awaiter(this, void 0, void 0, function* () {
if (!this._jobId) {
const valid = yield this.ensureJobId();
if (!valid) {
return;
}
}
this._response = yield this._statusFetcher(this._jobId);
this._updateStatus();
});
}
_getNextRetryTimeInMilliSecs() {
// Retry every 2 seconds, default valuse set in aio-lib-photoshop
return 2000;
}
/**
* Check the status returned by the server and update the state of the job.
* initial state is PENDING
* if atleast one output is RUNNING, state is IN_PROGRESS
* when all jobs are finished and if any output is FAILED, state is FAILED and error is set
* if all outputs are COMPLETED, state is COMPLETED
*/
_updateStatus() {
var _a, _b, _c, _d, _e;
const outputStatuses = {
pending: 0,
running: 0,
failed: 0,
completed: 0
};
for (const output of ((_b = (_a = this._response) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.outputs) || []) {
if (output.status === JobStatus_1.JobStatus.PENDING) {
outputStatuses.pending += 1;
}
else if (output.status === JobStatus_1.JobStatus.RUNNING) {
outputStatuses.running += 1;
}
else if (output.status === JobStatus_1.JobStatus.FAILED) {
outputStatuses.failed += 1;
}
else if (output.status === JobStatus_1.JobStatus.SUCCEEDED) {
outputStatuses.completed += 1;
}
}
// If any output is running, the job is in progress
if (outputStatuses.running > 0) {
this._state = internal_1.AsyncJobState.IN_PROGRESS;
return;
}
// When all jobs are finished and If any output is failed, the job has failed
if (outputStatuses.failed > 0 && outputStatuses.pending === 0) {
this._state = internal_1.AsyncJobState.FAILED;
this._error = new LrAsyncJobError("Job failed", (_c = this._response) === null || _c === void 0 ? void 0 : _c.result);
return;
}
// If all outputs are completed, the job has completed
if (outputStatuses.completed === ((_e = (_d = this._response) === null || _d === void 0 ? void 0 : _d.result) === null || _e === void 0 ? void 0 : _e.outputs.length)) {
this._state = internal_1.AsyncJobState.COMPLETED;
return;
}
}
ensureJobId() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
try {
const { result } = yield this._jobLinkPromise;
const statusUrl = (_b = (_a = result === null || result === void 0 ? void 0 : result._links) === null || _a === void 0 ? void 0 : _a.self) === null || _b === void 0 ? void 0 : _b.href;
if (!statusUrl) {
this._state = internal_1.AsyncJobState.FAILED;
this._error = new LrAsyncJobError("Job link does not contain status url", result);
return false;
}
this._jobId = statusUrl.split("/").pop();
}
catch (error) {
this._state = internal_1.AsyncJobState.FAILED;
this._error = error;
return false;
}
return true;
});
}
}
exports.LrAsyncJob = LrAsyncJob;