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.
99 lines (98 loc) • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EVENT_HANDLERS_KEY = void 0;
exports.OnTaskActive = OnTaskActive;
exports.OnTaskCompleted = OnTaskCompleted;
exports.OnTaskFailed = OnTaskFailed;
exports.OnTaskProgress = OnTaskProgress;
require("reflect-metadata");
/**
* Metadata keys for event decorators
*/
exports.EVENT_HANDLERS_KEY = 'cloud_taskmq:event_handlers';
/**
* Base event decorator factory
*/
function createEventDecorator(eventName) {
return (target, propertyKey, descriptor) => {
const existingHandlers = Reflect.getMetadata(exports.EVENT_HANDLERS_KEY, target) || [];
existingHandlers.push({
event: eventName,
methodName: propertyKey,
handler: descriptor.value,
});
Reflect.defineMetadata(exports.EVENT_HANDLERS_KEY, existingHandlers, target);
};
}
/**
* Decorator for handling task active events.
* This method will be called when a task becomes active.
*
* @example
* ```typescript
* @Processor('email-queue')
* export class EmailProcessor {
* @OnTaskActive()
* async onTaskActive(task: CloudTask<EmailData>) {
* console.log(`Task ${task.id} is now active`);
* }
* }
* ```
*/
function OnTaskActive() {
return createEventDecorator('active');
}
/**
* Decorator for handling task completed events.
* This method will be called when a task completes successfully.
*
* @example
* ```typescript
* @Processor('email-queue')
* export class EmailProcessor {
* @OnTaskCompleted()
* async onTaskCompleted(task: CloudTask<EmailData>, result: any) {
* console.log(`Task ${task.id} completed with result:`, result);
* }
* }
* ```
*/
function OnTaskCompleted() {
return createEventDecorator('completed');
}
/**
* Decorator for handling task failed events.
* This method will be called when a task fails.
*
* @example
* ```typescript
* @Processor('email-queue')
* export class EmailProcessor {
* @OnTaskFailed()
* async onTaskFailed(task: CloudTask<EmailData>, error: Error) {
* console.error(`Task ${task.id} failed:`, error);
* }
* }
* ```
*/
function OnTaskFailed() {
return createEventDecorator('failed');
}
/**
* Decorator for handling task progress events.
* This method will be called when a task reports progress.
*
* @example
* ```typescript
* @Processor('email-queue')
* export class EmailProcessor {
* @OnTaskProgress()
* async onTaskProgress(task: CloudTask<EmailData>, progress: { percentage: number; data?: any }) {
* console.log(`Task ${task.id} progress: ${progress.percentage}%`);
* }
* }
* ```
*/
function OnTaskProgress() {
return createEventDecorator('progress');
}