UNPKG

@medusajs/index

Version:
195 lines • 9.09 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _Orchestrator_instances, _Orchestrator_lockingModule, _Orchestrator_logger, _Orchestrator_lockingOwner, _Orchestrator_state, _Orchestrator_options, _Orchestrator_currentIndex, _Orchestrator_entities, _Orchestrator_acquireLock, _Orchestrator_processEntity; Object.defineProperty(exports, "__esModule", { value: true }); exports.Orchestrator = void 0; class Orchestrator { /** * The current state of the orchestrator */ get state() { return __classPrivateFieldGet(this, _Orchestrator_state, "f"); } /** * Reference to the currently processed entity */ get current() { return __classPrivateFieldGet(this, _Orchestrator_entities, "f")[__classPrivateFieldGet(this, _Orchestrator_currentIndex, "f")]; } /** * Reference to the number of entities left for processing */ get remainingCount() { return __classPrivateFieldGet(this, _Orchestrator_entities, "f").length - (__classPrivateFieldGet(this, _Orchestrator_currentIndex, "f") + 1); } constructor(lockingModule, entities, options) { _Orchestrator_instances.add(this); /** * Reference to the locking module */ _Orchestrator_lockingModule.set(this, void 0); /** * Reference to the logger */ _Orchestrator_logger.set(this, void 0); /** * Owner id when acquiring locks */ _Orchestrator_lockingOwner.set(this, `index-sync-${process.pid}` /** * The current state of the orchestrator * * - In "idle" state, one can call the "run" method. * - In "processing" state, the orchestrator is looping over the entities * and processing them. * - In "completed" state, the provided entities have been processed. * - The "error" state is set when the task runner throws an error. */ ); /** * The current state of the orchestrator * * - In "idle" state, one can call the "run" method. * - In "processing" state, the orchestrator is looping over the entities * and processing them. * - In "completed" state, the provided entities have been processed. * - The "error" state is set when the task runner throws an error. */ _Orchestrator_state.set(this, "idle" /** * Options for the locking module and the task runner to execute the * task. * * - Lock duration is the maximum duration for which to hold the lock. * After this the lock will be removed. * * The entity is provided to the taskRunner only when the orchestrator * is able to acquire a lock. */ ); /** * Options for the locking module and the task runner to execute the * task. * * - Lock duration is the maximum duration for which to hold the lock. * After this the lock will be removed. * * The entity is provided to the taskRunner only when the orchestrator * is able to acquire a lock. */ _Orchestrator_options.set(this, void 0); /** * Index of the entity that is currently getting processed. */ _Orchestrator_currentIndex.set(this, 0 /** * Collection of entities to process in sequence. A lock is obtained * while an entity is getting synced to avoid multiple processes * from syncing the same entity */ ); /** * Collection of entities to process in sequence. A lock is obtained * while an entity is getting synced to avoid multiple processes * from syncing the same entity */ _Orchestrator_entities.set(this, [] /** * The current state of the orchestrator */ ); __classPrivateFieldSet(this, _Orchestrator_lockingModule, lockingModule, "f"); __classPrivateFieldSet(this, _Orchestrator_entities, entities, "f"); __classPrivateFieldSet(this, _Orchestrator_options, options, "f"); __classPrivateFieldSet(this, _Orchestrator_logger, options.logger, "f"); } /** * Acquires or renew the lock for a given key. */ async renewLock(forKey) { return __classPrivateFieldGet(this, _Orchestrator_instances, "m", _Orchestrator_acquireLock).call(this, forKey); } /** * Run the orchestrator to process the entities one by one. * * - Task runner is the implementation function to execute a task. * Orchestrator has no inbuilt execution logic and it relies on * the task runner for the same. */ async process(taskRunner) { if (this.state !== "idle") { throw new Error("Cannot re-run an already running orchestrator instance"); } __classPrivateFieldSet(this, _Orchestrator_state, "processing", "f"); for (let i = 0; i < __classPrivateFieldGet(this, _Orchestrator_entities, "f").length; i++) { __classPrivateFieldSet(this, _Orchestrator_currentIndex, i, "f"); const entity = __classPrivateFieldGet(this, _Orchestrator_entities, "f")[i]; if (!entity) { __classPrivateFieldSet(this, _Orchestrator_state, "completed", "f"); break; } await __classPrivateFieldGet(this, _Orchestrator_instances, "m", _Orchestrator_processEntity).call(this, taskRunner, entity); } __classPrivateFieldSet(this, _Orchestrator_state, "completed", "f"); } } exports.Orchestrator = Orchestrator; _Orchestrator_lockingModule = new WeakMap(), _Orchestrator_logger = new WeakMap(), _Orchestrator_lockingOwner = new WeakMap(), _Orchestrator_state = new WeakMap(), _Orchestrator_options = new WeakMap(), _Orchestrator_currentIndex = new WeakMap(), _Orchestrator_entities = new WeakMap(), _Orchestrator_instances = new WeakSet(), _Orchestrator_acquireLock = /** * Acquires using the lock module. */ async function _Orchestrator_acquireLock(forKey) { try { await __classPrivateFieldGet(this, _Orchestrator_lockingModule, "f").acquire(forKey, { expire: __classPrivateFieldGet(this, _Orchestrator_options, "f").lockDuration, ownerId: __classPrivateFieldGet(this, _Orchestrator_lockingOwner, "f"), }); return true; } catch { return false; } }, _Orchestrator_processEntity = /** * Processes the entity. If there are no entities * left, the orchestrator state will be set to completed. * * - Task runner is the implementation function to execute a task. * Orchestrator has no inbuilt execution logic and it relies on * the task runner for the same. */ async function _Orchestrator_processEntity(taskRunner, entity) { const lockAcquired = await __classPrivateFieldGet(this, _Orchestrator_instances, "m", _Orchestrator_acquireLock).call(this, entity); if (lockAcquired) { try { await taskRunner(entity); } catch (error) { __classPrivateFieldSet(this, _Orchestrator_state, "error", "f"); throw error; } finally { await __classPrivateFieldGet(this, _Orchestrator_lockingModule, "f") .release(entity, { ownerId: __classPrivateFieldGet(this, _Orchestrator_lockingOwner, "f"), }) .catch(() => { __classPrivateFieldGet(this, _Orchestrator_logger, "f").error(`[Index engine] failed to release lock for entity '${entity}'`); }); } } else { __classPrivateFieldGet(this, _Orchestrator_logger, "f").warn(`[Index engine] failed to acquire lock for entity '${entity}' on pid ${process.pid}. It means another process is already processing this entity or a lock is still present in your locking provider.`); } }; //# sourceMappingURL=orchestrator.js.map