Centralize configure-able factory classes

This commit is contained in:
2022-09-26 11:34:23 -05:00
parent 5557aae543
commit c0595f3ef9
23 changed files with 262 additions and 682 deletions

View File

@@ -1,86 +1,28 @@
import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
StaticInstantiable,
FactoryProducer, getPropertyInjectionMetadata,
} from '../../../di'
import {Collection, ErrorWithContext} from '../../../util'
import {Logging} from '../../../service/Logging'
import {Config} from '../../../service/Config'
import {FactoryProducer, Instantiable} from '../../../di'
import {Maybe} from '../../../util'
import {Queue} from './Queue'
import {SyncQueue} from './SyncQueue'
import {ConfiguredSingletonFactory} from '../../../di/factory/ConfiguredSingletonFactory'
/**
* Dependency container factory that matches the abstract Queue token, but
* produces an instance of whatever Queue driver is configured in the `server.queue.driver` config.
*/
@FactoryProducer()
export class QueueFactory extends AbstractFactory<Queue> {
/** true if we have printed the synchronous queue driver warning once. */
private static loggedSyncQueueWarningOnce = false
private di(): [Logging, Config] {
return [
Container.getContainer().make(Logging),
Container.getContainer().make(Config),
]
export class QueueFactory extends ConfiguredSingletonFactory<Queue> {
protected getConfigKey(): string {
return 'server.queue.driver'
}
produce(): Queue {
return new (this.getQueueClass())()
protected getDefaultImplementation(): Instantiable<Queue> {
return SyncQueue
}
match(something: unknown): boolean {
return something === Queue
protected getAbstractImplementation(): any {
return Queue
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getQueueClass())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getQueueClass()
do {
const loadedMeta = getPropertyInjectionMetadata(currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
/**
* Get the configured queue driver and return some Instantiable<Queue>.
* @protected
*/
protected getQueueClass(): StaticInstantiable<Queue> {
const [logging, config] = this.di()
const QueueClass = config.get('server.queue.driver', SyncQueue)
if ( QueueClass === SyncQueue && !QueueFactory.loggedSyncQueueWarningOnce ) {
logging.warn(`You are using the default synchronous queue driver. It is recommended you configure a background queue driver instead.`)
QueueFactory.loggedSyncQueueWarningOnce = true
}
if ( !isInstantiable(QueueClass) || !(QueueClass.prototype instanceof Queue) ) {
const e = new ErrorWithContext('Provided queue class does not extend from @extollo/lib.Queue')
e.context = {
configKey: 'server.queue.driver',
class: QueueClass.toString(),
}
}
return QueueClass
protected getDefaultImplementationWarning(): Maybe<string> {
return 'You are using the default synchronous queue driver. It is recommended you configure a background queue driver instead.'
}
}

View File

@@ -1,86 +1,27 @@
import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
StaticClass, Instantiable, getPropertyInjectionMetadata,
} from '../../di'
import {Collection, ErrorWithContext} from '../../util'
import {Logging} from '../../service/Logging'
import {Config} from '../../service/Config'
import {Instantiable, FactoryProducer} from '../../di'
import {InMemCache, Maybe} from '../../util'
import {Cache} from '../../util'
import {MemoryCache} from './MemoryCache'
import {ConfiguredSingletonFactory} from '../../di/factory/ConfiguredSingletonFactory'
/**
* Dependency container factory that matches the abstract Cache token, but
* produces an instance of whatever Cache driver is configured in the `server.cache.driver` config.
*/
export class CacheFactory extends AbstractFactory<Cache> {
protected readonly logging: Logging
protected readonly config: Config
/** true if we have printed the memory-based cache driver warning once. */
private static loggedMemoryCacheWarningOnce = false
constructor() {
super({})
this.logging = Container.getContainer().make<Logging>(Logging)
this.config = Container.getContainer().make<Config>(Config)
@FactoryProducer()
export class CacheFactory extends ConfiguredSingletonFactory<Cache> {
protected getConfigKey(): string {
return 'server.cache.driver'
}
produce(): Cache {
return new (this.getCacheClass())()
protected getDefaultImplementation(): Instantiable<Cache> {
return InMemCache
}
match(something: unknown): boolean {
return something === Cache
protected getAbstractImplementation(): any {
return Cache
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getCacheClass())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getCacheClass()
do {
const loadedMeta = getPropertyInjectionMetadata(currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
/**
* Get the configured cache driver and return some Instantiable<Cache>.
* @protected
*/
protected getCacheClass(): StaticClass<Cache, Instantiable<Cache>> {
const CacheClass = this.config.get('server.cache.driver', MemoryCache)
if ( CacheClass === MemoryCache && !CacheFactory.loggedMemoryCacheWarningOnce ) {
this.logging.warn(`You are using the default memory-based cache driver. It is recommended you configure a persistent cache driver instead.`)
CacheFactory.loggedMemoryCacheWarningOnce = true
}
if ( !isInstantiable(CacheClass) || !(CacheClass.prototype instanceof Cache) ) {
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Cache')
e.context = {
configKey: 'server.cache.driver',
class: CacheClass.toString(),
}
}
return CacheClass
protected getDefaultImplementationWarning(): Maybe<string> {
return 'You are using the default memory-based cache driver. It is recommended you configure a persistent cache driver instead.'
}
}