@mdf.js/core
Version:
MMS - API Core - Common types, classes and functions
171 lines • 6.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JobHandler = void 0;
/**
* Copyright 2024 Mytra Control S.L. All rights reserved.
*
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
* or at https://opensource.org/licenses/MIT.
*/
const crash_1 = require("@mdf.js/crash");
const events_1 = require("events");
const uuid_1 = require("uuid");
const const_1 = require("../const");
const types_1 = require("./types");
/**
* JobHandler class
* @category @mdf.js/core
* @typeParam Type - Job type, used as selector for strategies in job processors
* @typeParam Data - Job payload
* @typeParam CustomHeaders - Custom headers, used to pass specific information for job processors
* @typeParam CustomOptions - Custom options, used to pass specific information for job processors
*/
class JobHandler extends events_1.EventEmitter {
constructor(jobUserIdOrJobRequest, data, type, options) {
var _a, _b;
super();
/** Job processing status */
this._status = types_1.Status.PENDING;
this.uuid = (0, uuid_1.v4)();
if (typeof jobUserIdOrJobRequest === 'string') {
this.jobUserId = jobUserIdOrJobRequest;
this.type = type !== null && type !== void 0 ? type : 'default';
this.options = options;
this._data = data;
}
else if (typeof jobUserIdOrJobRequest === 'object') {
this.jobUserId = jobUserIdOrJobRequest.jobUserId;
this.type = (_a = jobUserIdOrJobRequest.type) !== null && _a !== void 0 ? _a : 'default';
this.options = jobUserIdOrJobRequest.options;
this._data = jobUserIdOrJobRequest.data;
}
else {
throw new crash_1.Crash(`Error creating a valid JobHandler, the first parameter must be a jobUserId or a JobRequest object`, this.uuid, { name: 'ValidationError' });
}
if (this._data === undefined || this._data === null) {
throw new crash_1.Crash('Error creating a valid JobHandler, data is mandatory', this.uuid, {
name: 'ValidationError',
});
}
if (typeof this.type !== 'string') {
throw new crash_1.Crash('Error creating a valid JobHandler, type must be a string', this.uuid, {
name: 'ValidationError',
});
}
if (this.options && typeof this.options !== 'object') {
throw new crash_1.Crash('Error creating a valid JobHandler, options should be a object', (0, uuid_1.v4)(), {
name: 'ValidationError',
});
}
this.jobUserUUID = (0, uuid_1.v5)(this.jobUserId, const_1.MDF_NAMESPACE_OID);
this.createdAt = new Date();
this.pendingDone = (_b = options === null || options === void 0 ? void 0 : options.numberOfHandlers) !== null && _b !== void 0 ? _b : 1;
}
/** Job payload */
get data() {
this.updateStatusToProcessing();
return this._data;
}
set data(value) {
this.updateStatusToProcessing();
this._data = value;
}
/** True if the job task raised any error */
get hasErrors() {
if (this._errors) {
return true;
}
else {
return false;
}
}
/** Errors raised during the job */
get errors() {
return this._errors;
}
/** Return the process time in msec */
get processTime() {
if (this.resolvedAt) {
return this.resolvedAt.getTime() - this.createdAt.getTime();
}
else {
return -1;
}
}
/** Return the job processing status */
get status() {
return this._status;
}
/**
* Add a new error in the job
* @param error - error to be added to the job
*/
addError(error) {
if (this._errors) {
this._errors.push(error);
}
else {
this._errors = new crash_1.Multi('Errors in job processing', this.uuid, {
name: 'ValidationError',
causes: error,
});
}
this.updateStatusToProcessing();
}
/**
* Notify the results of the process
* @param error - conditional parameter for error notification
*/
done(error) {
this.pendingDone--;
if (error) {
this.addError(error);
}
if (this.pendingDone <= 0 && !this.resolvedAt) {
this.resolvedAt = new Date();
if (this.hasErrors) {
this._status = types_1.Status.FAILED;
}
else {
this._status = types_1.Status.COMPLETED;
}
this.emit('done', this.uuid, this.result(), this._errors);
}
}
/** Return the result of the publication process */
result() {
var _a, _b;
return {
uuid: this.uuid,
createdAt: this.createdAt.toISOString(),
resolvedAt: (_b = (_a = this.resolvedAt) === null || _a === void 0 ? void 0 : _a.toISOString()) !== null && _b !== void 0 ? _b : '',
quantity: Array.isArray(this._data) ? this._data.length : 1,
hasErrors: this._errors ? this._errors.size > 0 : false,
errors: this._errors ? this._errors.toJSON() : undefined,
jobUserId: this.jobUserId,
jobUserUUID: this.jobUserUUID,
type: this.type,
status: this._status,
};
}
/** Return an object with the key information of the job, this information is used by the plugs */
toObject() {
return {
uuid: this.uuid,
data: this.data,
type: this.type,
jobUserId: this.jobUserId,
jobUserUUID: this.jobUserUUID,
status: this._status,
options: this.options,
};
}
/** Update the job status to processing if it is in pending state */
updateStatusToProcessing() {
if (this._status === types_1.Status.PENDING) {
this._status = types_1.Status.PROCESSING;
}
}
}
exports.JobHandler = JobHandler;
//# sourceMappingURL=JobHandler.js.map