UNPKG

nodejs-cloud-taskmq

Version:

Node.js TypeScript library for integrating Google Cloud Tasks with MongoDB/Redis/Memory/Custom for a BullMQ-like queue system. Compatible with NestJS but framework-agnostic.

175 lines (174 loc) 4.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CloudTask = void 0; const storage_adapter_interface_1 = require("../interfaces/storage-adapter.interface"); /** * CloudTask model representing a task in the queue system */ class CloudTask { constructor(task) { this.id = task.id; this.queueName = task.queueName; this.data = task.data; this.status = task.status; this.createdAt = task.createdAt; this.updatedAt = task.updatedAt; this.completedAt = task.completedAt; this.failedAt = task.failedAt; this.attempts = task.attempts; this.maxAttempts = task.maxAttempts; this.error = task.error; this.progress = task.progress; this.result = task.result; this.delay = task.delay; this.scheduledFor = task.scheduledFor; this.chain = task.chain; this.uniquenessKey = task.uniquenessKey; this.options = task.options; } /** * Update task progress */ updateProgress(progress) { this.progress = { percentage: Math.max(0, Math.min(100, progress.percentage)), data: progress.data, }; this.updatedAt = new Date(); } /** * Mark task as active */ markAsActive() { this.status = storage_adapter_interface_1.TaskStatus.ACTIVE; this.updatedAt = new Date(); } /** * Mark task as completed */ markAsCompleted(result) { this.status = storage_adapter_interface_1.TaskStatus.COMPLETED; this.result = result; this.completedAt = new Date(); this.updatedAt = new Date(); } /** * Mark task as failed */ markAsFailed(error) { this.status = storage_adapter_interface_1.TaskStatus.FAILED; this.error = { message: typeof error === 'string' ? error : error.message, stack: typeof error === 'string' ? undefined : error.stack, timestamp: new Date(), }; this.failedAt = new Date(); this.updatedAt = new Date(); } /** * Increment attempt counter */ incrementAttempts() { this.attempts++; this.updatedAt = new Date(); } /** * Check if task has exceeded maximum attempts */ hasExceededMaxAttempts() { return this.attempts >= this.maxAttempts; } /** * Check if task is in a chain */ isInChain() { return !!this.chain; } /** * Check if task is the first in chain */ isFirstInChain() { return this.chain?.index === 0; } /** * Check if task is the last in chain */ isLastInChain() { return this.chain ? this.chain.index === this.chain.total - 1 : false; } /** * Get next task index in chain */ getNextChainIndex() { if (!this.chain || this.isLastInChain()) { return null; } return this.chain.index + 1; } /** * Check if task should be removed on completion */ shouldRemoveOnComplete() { return this.options?.removeOnComplete ?? false; } /** * Check if task should be removed on failure */ shouldRemoveOnFail() { return this.options?.removeOnFail ?? false; } /** * Get task priority */ getPriority() { return this.options?.priority ?? 0; } /** * Get task age in milliseconds */ getAge() { return Date.now() - this.createdAt.getTime(); } /** * Get task duration (if completed or failed) */ getDuration() { const endTime = this.completedAt || this.failedAt; if (!endTime) { return null; } return endTime.getTime() - this.createdAt.getTime(); } /** * Convert to plain object */ toObject() { return { id: this.id, queueName: this.queueName, data: this.data, status: this.status, createdAt: this.createdAt, updatedAt: this.updatedAt, completedAt: this.completedAt, failedAt: this.failedAt, attempts: this.attempts, maxAttempts: this.maxAttempts, error: this.error, progress: this.progress, result: this.result, delay: this.delay, scheduledFor: this.scheduledFor, chain: this.chain, uniquenessKey: this.uniquenessKey, options: this.options, }; } /** * Create CloudTask from plain object */ static fromObject(obj) { return new CloudTask(obj); } } exports.CloudTask = CloudTask;