100 lines
3.2 KiB
TypeScript
100 lines
3.2 KiB
TypeScript
import { asClass, asFunction, asValue, type AwilixContainer } from 'awilix';
|
|
import { Browser } from '@stock-bot/browser';
|
|
import { defined } from '@stock-bot/invariants';
|
|
import { ProxyManager } from '@stock-bot/proxy';
|
|
import type { AppConfig } from '../config/schemas';
|
|
import type { ServiceDefinitions } from '../container/types';
|
|
|
|
export function registerApplicationServices(
|
|
container: AwilixContainer<ServiceDefinitions>,
|
|
config: AppConfig
|
|
): void {
|
|
// Browser
|
|
if (config.browser) {
|
|
container.register({
|
|
browser: asClass(Browser)
|
|
.singleton()
|
|
.inject(() => ({
|
|
options: {
|
|
headless: defined(config.browser).headless,
|
|
timeout: defined(config.browser).timeout,
|
|
},
|
|
})),
|
|
});
|
|
} else {
|
|
container.register({
|
|
browser: asValue(null), // Required field
|
|
});
|
|
}
|
|
|
|
// Proxy Manager
|
|
if (config.proxy && config.redis?.enabled) {
|
|
container.register({
|
|
proxyCache: asFunction(({ logger }) => {
|
|
// Create a separate cache instance for proxy with global prefix
|
|
const { createCache } = require('@stock-bot/cache');
|
|
return createCache({
|
|
redisConfig: {
|
|
host: config.redis.host,
|
|
port: config.redis.port,
|
|
password: config.redis.password,
|
|
username: config.redis.username,
|
|
maxRetriesPerRequest: config.redis.maxRetries,
|
|
retryDelay: config.redis.retryDelay,
|
|
db: 1, // Use cache DB (usually DB 1)
|
|
},
|
|
shared: false,
|
|
keyPrefix: 'cache:proxy:',
|
|
ttl: 86400, // 24 hours default
|
|
enableMetrics: true,
|
|
logger,
|
|
});
|
|
})
|
|
.singleton()
|
|
.disposer(cache => cache.disconnect?.()),
|
|
proxyManager: asFunction(
|
|
({ logger, proxyCache }) => new ProxyManager(defined(proxyCache), config.proxy, logger)
|
|
).singleton(),
|
|
});
|
|
} else {
|
|
container.register({
|
|
proxyCache: asValue(null),
|
|
proxyManager: asValue(null),
|
|
});
|
|
}
|
|
|
|
// Queue Manager
|
|
if (config.queue?.enabled && config.redis?.enabled) {
|
|
container.register({
|
|
queueManager: asFunction(({ logger, handlerRegistry }) => {
|
|
const { QueueManager } = require('@stock-bot/queue');
|
|
|
|
const queueConfig = {
|
|
serviceName: config.service?.serviceName || config.service?.name || 'unknown',
|
|
redis: {
|
|
host: config.redis.host,
|
|
port: config.redis.port,
|
|
password: config.redis.password,
|
|
username: config.redis.username,
|
|
maxRetriesPerRequest: config.redis.maxRetries,
|
|
retryDelay: config.redis.retryDelay,
|
|
db: config.redis.db,
|
|
},
|
|
defaultQueueOptions: {
|
|
workers: defined(config.queue).workers ?? 1,
|
|
concurrency: defined(config.queue).concurrency ?? 1,
|
|
defaultJobOptions: defined(config.queue).defaultJobOptions,
|
|
},
|
|
enableScheduledJobs: defined(config.queue).enableScheduledJobs ?? true,
|
|
autoDiscoverHandlers: true,
|
|
};
|
|
|
|
return new QueueManager(queueConfig, handlerRegistry, logger);
|
|
}).singleton(),
|
|
});
|
|
} else {
|
|
container.register({
|
|
queueManager: asValue(null),
|
|
});
|
|
}
|
|
}
|