jsm-core
Version:
Core library for JSM project
72 lines (71 loc) • 2.72 kB
TypeScript
import { JsmLogger } from 'jsm-logger';
export type TQueueItemStatus = 'pending' | 'processing' | 'failed' | 'retrying' | 'completed';
export type TQueueItemType = string;
export interface TQueueItem<T extends TQueueItemType = TQueueItemType, D extends object = any> {
app: any;
id: string;
type: T;
status: TQueueItemStatus;
data: D;
}
export type TQueueItemWithConfig<T extends TQueueItemType = TQueueItemType, D extends object = any> = TQueueItem<T, D> & {
createdAt: Date;
updatedAt: Date;
error?: Error;
processor: (item: TQueueItem<T, D>) => Promise<void>;
retryCount: number;
retryDelay: (retries: number) => number;
maxRetries: number;
};
export type TQueueStatus = 'idle' | 'running';
export declare class QueueManager<T extends TQueueItemType = TQueueItemType, D extends object = any> {
private queueEmitter;
logger: JsmLogger;
status: TQueueStatus;
_queue: TQueueItemWithConfig<T, D>[];
constructor();
/**
* Adds an item to the processing queue.
* @param item The item to add to the queue.
* @returns The ID of the added item.
*/
addItem(item: TQueueItem<T, D>, processor: (item: TQueueItem<T, D>) => Promise<void>, opt?: {
retryCount?: number;
retryDelay?: (retries: number) => number;
maxRetries?: number;
}): string;
/**
* Removes an item from the processing queue by its ID.
* @param itemId The ID of the item to remove.
*/
removeItem(itemId: string): void;
/**
* Updates an item in the processing queue by its ID.
* @param itemId The ID of the item to update.
* @param updates The updates to apply to the item.
*/
updateItem(itemId: string, updates: Partial<TQueueItem<T, D>>): void;
/**
* Retrieves items from the processing queue based on optional filters.
* @param status Optional status to filter items by.
* @param type Optional type to filter items by.
* @param appId Optional app ID to filter items by.
* @param storeId Optional store ID to filter items by.
* @returns An array of matching TQueueItem objects.
*/
getItems(status?: TQueueItemStatus | TQueueItemStatus[], type?: T | T[], appId?: string, storeId?: string): TQueueItem[];
getStatus(): {
status: TQueueStatus;
queueLength: number;
pendingItems: number;
processingItems: number;
completedItems: number;
retryingItems: number;
failedItems: number;
};
/**
* Processes the queue, handling each item based on its type and status.
* This method should be implemented in subclasses to define specific processing logic.
*/
private process;
}