huge refactor done

This commit is contained in:
Boki 2025-06-24 11:59:35 -04:00
parent 843a7b9b9b
commit 60d7de1da8
16 changed files with 472 additions and 443 deletions

View file

@ -149,7 +149,12 @@ export class ServiceContainerBuilder {
// Register handler infrastructure first
container.register({
handlerRegistry: asClass(HandlerRegistry).singleton(),
handlerScanner: asClass(HandlerScanner).singleton(),
handlerScanner: asFunction(({ handlerRegistry }) => {
return new HandlerScanner(handlerRegistry, container, {
serviceName: config.service?.serviceName || config.service?.name,
autoRegister: true,
});
}).singleton(),
});
registerCoreServices(container, config);
@ -177,7 +182,7 @@ export class ServiceContainerBuilder {
globalCache,
proxy: proxyManager, // Map proxyManager to proxy
browser,
queue: queueManager, // Map queueManager to queue
queueManager, // Provide queueManager directly
mongodb: mongoClient, // Map mongoClient to mongodb
postgres: postgresClient, // Map postgresClient to postgres
questdb: questdbClient, // Map questdbClient to questdb

View file

@ -76,7 +76,7 @@ export function registerApplicationServices(
defaultJobOptions: config.queue!.defaultJobOptions,
},
enableScheduledJobs: config.queue!.enableScheduledJobs ?? true,
delayWorkerStart: config.queue!.delayWorkerStart ?? false,
delayWorkerStart: config.queue!.delayWorkerStart ?? true, // Changed to true so ServiceApplication can start workers
autoDiscoverHandlers: true,
};
return new SmartQueueManager(queueConfig, handlerRegistry, logger);

View file

@ -3,7 +3,7 @@
* Discovers and registers handlers with the DI container
*/
import { asClass, type AwilixContainer } from 'awilix';
import { asClass, asFunction, type AwilixContainer } from 'awilix';
import { glob } from 'glob';
import type {
HandlerConfiguration,
@ -157,7 +157,9 @@ export class HandlerScanner {
// Register with DI container if auto-register is enabled
if (this.options.autoRegister !== false) {
this.container.register({
[handlerName]: asClass(HandlerClass).singleton(),
[handlerName]: asFunction(({ serviceContainer }) => {
return new HandlerClass(serviceContainer);
}).singleton(),
});
}

View file

@ -278,7 +278,11 @@ export class ServiceApplication {
// Initialize handlers if enabled
if (this.serviceConfig.enableHandlers && handlerInitializer) {
this.logger.debug('Initializing handlers...');
await handlerInitializer(this.serviceContainer);
// Pass the service container with the DI container attached
const containerWithDI = Object.assign({}, this.serviceContainer, {
_diContainer: this.container
});
await handlerInitializer(containerWithDI);
this.logger.info('Handlers initialized');
}
@ -335,6 +339,8 @@ export class ServiceApplication {
this.logger.debug('Creating scheduled jobs from registered handlers...');
const handlerRegistry = this.container.resolve<HandlerRegistry>('handlerRegistry');
const allHandlers = handlerRegistry.getAllHandlersWithSchedule();
this.logger.info(`Found ${allHandlers.size} handlers with scheduled jobs: ${Array.from(allHandlers.keys()).join(', ')}`);
let totalScheduledJobs = 0;
for (const [handlerName, config] of allHandlers) {
@ -356,7 +362,16 @@ export class ServiceApplication {
this.logger.error('Queue manager is not initialized, cannot create scheduled jobs');
continue;
}
const queue = queueManager.getQueue(handlerName);
// Pass the handler registry explicitly when creating queues for scheduled jobs
this.logger.debug('Creating queue for scheduled jobs', {
handlerName,
hasHandlerRegistry: !!handlerRegistry,
registeredHandlers: handlerRegistry.getHandlerNames(),
});
const queue = queueManager.getQueue(handlerName, {
handlerRegistry: handlerRegistry
});
for (const scheduledJob of config.scheduledJobs) {
// Include handler and operation info in job data
@ -375,6 +390,12 @@ export class ServiceApplication {
},
};
this.logger.debug('Adding scheduled job', {
handler: handlerName,
operation: scheduledJob.operation,
hasOperation: !!handlerRegistry.getOperation(handlerName, scheduledJob.operation),
});
await queue.addScheduledJob(
scheduledJob.operation,
jobData,