diff --git a/apps/data-ingestion/src/handlers/qm/qm.handler.ts b/apps/data-ingestion/src/handlers/qm/qm.handler.ts index 378b700..6433566 100644 --- a/apps/data-ingestion/src/handlers/qm/qm.handler.ts +++ b/apps/data-ingestion/src/handlers/qm/qm.handler.ts @@ -92,7 +92,7 @@ export class QMHandler extends BaseHandler { // this.logger.debug('Spider job stored in cache', { spiderJobId, ttl: 3600 }); // // Schedule follow-up processing if needed - // await this.scheduleOperation('search-symbols', { source: 'spider', spiderJobId }, 5000); + // await this.scheduleOperation('search-symbols', { source: 'spider', spiderJobId }, { delay: 5000 }); // return { // success: true, diff --git a/libs/core/handlers/src/base/BaseHandler.ts b/libs/core/handlers/src/base/BaseHandler.ts index 09d8a58..3ee72b2 100644 --- a/libs/core/handlers/src/base/BaseHandler.ts +++ b/libs/core/handlers/src/base/BaseHandler.ts @@ -10,6 +10,28 @@ import { createNamespacedCache } from '@stock-bot/cache'; import type { IServiceContainer } from '../types/service-container'; import type { ExecutionContext, IHandler } from '../types/types'; +/** + * Job scheduling options + */ +export interface JobScheduleOptions { + delay?: number; + priority?: number; + attempts?: number; + removeOnComplete?: number; + removeOnFail?: number; + backoff?: { + type: 'exponential' | 'fixed'; + delay: number; + }; + repeat?: { + pattern?: string; + key?: string; + limit?: number; + every?: number; + immediately?: boolean; + }; +} + /** * Abstract base class for all handlers with improved DI * Provides common functionality and structure for queue/event operations @@ -83,7 +105,11 @@ export abstract class BaseHandler implements IHandler { return await method.call(this, input, context); } - async scheduleOperation(operation: string, payload: unknown, delay?: number): Promise { + async scheduleOperation( + operation: string, + payload: unknown, + options?: JobScheduleOptions + ): Promise { if (!this.queue) { throw new Error('Queue service is not available'); } @@ -93,7 +119,8 @@ export abstract class BaseHandler implements IHandler { operation, payload, }; - await queue.add(operation, jobData, { delay }); + + await queue.add(operation, jobData, options || {}); } /** @@ -171,9 +198,13 @@ export abstract class BaseHandler implements IHandler { protected async scheduleIn( operation: string, payload: unknown, - delaySeconds: number + delaySeconds: number, + additionalOptions?: Omit ): Promise { - return this.scheduleOperation(operation, payload, delaySeconds * 1000); + return this.scheduleOperation(operation, payload, { + delay: delaySeconds * 1000, + ...additionalOptions + }); } /** diff --git a/libs/core/handlers/src/index.ts b/libs/core/handlers/src/index.ts index 69868f2..0db14d2 100644 --- a/libs/core/handlers/src/index.ts +++ b/libs/core/handlers/src/index.ts @@ -1,5 +1,6 @@ // Base handler classes export { BaseHandler, ScheduledHandler } from './base/BaseHandler'; +export type { JobScheduleOptions } from './base/BaseHandler'; // Handler registry (re-exported from types to avoid circular deps) export { handlerRegistry } from '@stock-bot/types';