@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
148 lines (147 loc) • 7.34 kB
JavaScript
;
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.default = handle;
const types_1 = require("../util/types");
const util_1 = require("../../../util");
const storage_1 = require("../../../storage");
const cron_1 = require("../util/cron");
const defaults_1 = require("../util/defaults");
const connection_1 = require("../../../util/connection");
const logger_1 = require("../../../util/logger");
const MINUTES = 60;
function getHumanETA(ms) {
const seconds = Math.floor(ms / 1000);
let minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
if (seconds < 60) {
return 'Less than a minute remaining';
}
minutes = minutes % 60;
const timeParts = [];
if (hours) {
timeParts.push(`${hours} ${hours > 1 ? 'hours' : 'hour'}`);
}
if (minutes) {
timeParts.push(`${minutes} ${minutes > 1 ? 'minutes' : 'minute'}`);
}
return `About ${timeParts.join(' and ')} remaining`;
}
function handle(config) {
return (0, util_1.runAsyncWrapper)((req, res) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const isApi = req.isApiCall;
const id = ((_a = req.query) === null || _a === void 0 ? void 0 : _a.jobId) || ((_b = req.body) === null || _b === void 0 ? void 0 : _b.jobId);
if (!id) {
req.logInfo('Get active jobs');
const jobs = yield (0, storage_1.getStorage)().getActiveJobs({
integrationId: req.crowdinContext.clientId,
crowdinId: req.crowdinContext.crowdinId,
});
if (isApi && jobs) {
const filteredJobs = jobs.map((job) => ({
id: job.id,
progress: job.progress,
status: job.status,
title: job.title,
}));
req.logInfo(`Returning active filtered jobs info ${JSON.stringify(filteredJobs, null, 2)}`);
res.send(filteredJobs);
return;
}
req.logInfo(`Returning active jobs info ${JSON.stringify(jobs, null, 2)}`);
res.send(jobs);
return;
}
req.logInfo(`Get job info for id ${id}`);
const job = yield (0, storage_1.getStorage)().getJob({ id });
if (isApi && !job) {
return res.status(404).json({
error: {
message: `Job with ID ${id} not found`,
},
});
}
if (job && job.status === types_1.JobStatus.IN_PROGRESS && job.progress > 5 && job.updatedAt) {
job.eta = ((Date.now() - job.createdAt) / job.progress) * (100 - job.progress);
job.info = getHumanETA(job.eta) + (job.info ? `\n${job.info}` : '');
}
if (isApi && job) {
const filteredJob = {
id: job.id,
progress: job.progress,
status: job.status,
title: job.title,
};
req.logInfo(`Returning filtered job info ${JSON.stringify(filteredJob, null, 2)}`);
res.send([filteredJob]);
return;
}
const jobTimestamp = (_c = job === null || job === void 0 ? void 0 : job.updatedAt) !== null && _c !== void 0 ? _c : job === null || job === void 0 ? void 0 : job.createdAt;
const isJobStuck = !!job && !!jobTimestamp && Date.now() - jobTimestamp >= MINUTES * 60 * 1000;
if (isJobStuck) {
const settingsJobTypes = [types_1.JobType.CROWDIN_SYNC_SETTINGS_SAVE, types_1.JobType.INTEGRATION_SYNC_SETTINGS_SAVE];
if (settingsJobTypes.includes(job.type)) {
req.logInfo(`Canceling stuck settings job '${job.id}' after no updates for more than '${MINUTES}' minutes.`);
yield (0, storage_1.getStorage)().updateJob({ id: job.id, status: types_1.JobStatus.CANCELED });
return res.send(Object.assign(Object.assign({}, job), { status: types_1.JobStatus.CANCELED }));
}
const context = req.crowdinContext;
const projectId = context.jwtPayload.context.project_id;
const integration = config.projectIntegration;
const crowdinId = req.crowdinContext.crowdinId;
const integrationId = req.crowdinContext.clientId;
try {
const integrationCredentials = yield (0, storage_1.getStorage)().getIntegrationCredentials(integrationId);
const credentials = yield (0, connection_1.prepareIntegrationCredentials)(config, integration, integrationCredentials);
const integrationConfig = yield (0, storage_1.getStorage)().getIntegrationConfig(integrationId);
const intConfig = (integrationConfig === null || integrationConfig === void 0 ? void 0 : integrationConfig.config)
? JSON.parse(integrationConfig.config)
: { schedule: '0', condition: '0' };
const rootFolder = yield (0, defaults_1.getRootFolder)(config, integration, req.crowdinApiClient, projectId);
req.logInfo(`Restarting the job after no updates for more than ${MINUTES} minutes.`);
return (0, cron_1.runUpdateProviderJob)({
integrationId,
crowdinId,
type: job.type,
title: job.title,
payload: JSON.parse(job.payload),
jobType: types_1.JobClientType.RERUN,
projectId,
client: req.crowdinApiClient,
integration,
context,
credentials,
rootFolder,
settings: intConfig,
reRunJobId: id,
});
}
catch (e) {
(0, logger_1.logError)(e);
yield (0, storage_1.getStorage)().updateJob({
id: job.id,
status: types_1.JobStatus.FAILED,
info: (0, logger_1.getErrorMessage)(e),
});
return res.status(500).json({
error: {
message: `Job was failed after attempt to restart it due to inactivity (no updates for ${MINUTES} minutes). Please try to restart the job manually.`,
jobId: job.id,
status: types_1.JobStatus.FAILED,
},
});
}
}
req.logInfo(`Returning job info ${JSON.stringify(job, null, 2)}`);
res.send(job);
}));
}