Skip to content

Commit

Permalink
refactor: Drop InversifyJS: BatchReader (#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennylavender authored Dec 5, 2018
1 parent bfedb26 commit f0f594c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 52 deletions.
65 changes: 38 additions & 27 deletions src/BatchReader/BatchReader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Container } from 'inversify'
import { Db, MongoClient, Collection } from 'mongodb'
import * as Pino from 'pino'
import { pick } from 'ramda'
Expand All @@ -23,7 +22,6 @@ export interface BatchReaderConfiguration extends LoggingConfiguration, ServiceC
export class BatchReader {
private readonly logger: Pino.Logger
private readonly configuration: BatchReaderConfiguration
private readonly container = new Container()
private mongoClient: MongoClient
private dbConnection: Db
private router: Router
Expand All @@ -44,15 +42,48 @@ export class BatchReader {
this.messaging = new Messaging(this.configuration.rabbitmqUrl, exchangesMessaging)
await this.messaging.start()

this.initializeContainer()
const ipfs = new IPFS({
configuration: {
ipfsUrl: this.configuration.ipfsUrl,
},
})

const directoryCollection = this.dbConnection.collection('batchReader')

const directoryDAO = new DirectoryDAO({
dependencies: {
directoryCollection,
},
})

this.router = this.container.get('Router')
const claimController = new ClaimController({
dependencies: {
directoryDAO,
ipfs,
},
})

this.router = new Router({
dependencies: {
logger: this.logger,
messaging: this.messaging,
claimController,
},
exchange: this.configuration.exchanges,
})
await this.router.start()

this.service = this.container.get('Service')
this.service = new Service({
dependencies: {
logger: this.logger,
messaging: this.messaging,
},
configuration: {
readNextDirectoryIntervalInSeconds: this.configuration.readNextDirectoryIntervalInSeconds,
},
exchange: this.configuration.exchanges,
})
await this.service.start()
const directoryDAO: DirectoryDAO = this.container.get('DirectoryDAO')
await directoryDAO.start()

this.logger.info('BatchReader Started')
}
Expand All @@ -64,24 +95,4 @@ export class BatchReader {
await this.router.stop()
await this.service.stop()
}

initializeContainer() {
this.container.bind<Pino.Logger>('Logger').toConstantValue(this.logger)
this.container.bind<ClaimController>('ClaimController').to(ClaimController)
this.container.bind<Collection>('directoryCollection').toConstantValue(this.dbConnection.collection('batchReader'))
this.container.bind<Db>('DB').toConstantValue(this.dbConnection)
this.container.bind<DirectoryDAO>('DirectoryDAO').to(DirectoryDAO)
this.container.bind<IPFS>('IPFS').to(IPFS)
this.container.bind<IPFSConfiguration>('IPFSConfiguration').toConstantValue({
ipfsUrl: this.configuration.ipfsUrl,
})
this.container.bind<Router>('Router').to(Router)
this.container.bind<Messaging>('Messaging').toConstantValue(this.messaging)
this.container.bind<Service>('Service').to(Service)
this.container.bind<ServiceConfiguration>('ServiceConfiguration').toConstantValue({
readNextDirectoryIntervalInSeconds: this.configuration.readNextDirectoryIntervalInSeconds,
})

this.container.bind<ExchangeConfiguration>('ExchangeConfiguration').toConstantValue(this.configuration.exchanges)
}
}
18 changes: 15 additions & 3 deletions src/BatchReader/ClaimController.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { inject, injectable } from 'inversify'
import { InsertWriteOpResult } from 'mongodb'

import { DirectoryDAO } from './DirectoryDAO'
import { IPFS } from './IPFS'

@injectable()
export interface Dependencies {
readonly directoryDAO: DirectoryDAO
readonly ipfs: IPFS
}

export interface Arguments {
readonly dependencies: Dependencies
}

export class ClaimController {
private readonly directoryDAO: DirectoryDAO
private readonly ipfs: IPFS

constructor(@inject('DirectoryDAO') directoryDAO: DirectoryDAO, @inject('IPFS') ipfs: IPFS) {
constructor({
dependencies: {
directoryDAO,
ipfs,
},
}: Arguments) {
this.directoryDAO = directoryDAO
this.ipfs = ipfs
}
Expand Down
16 changes: 13 additions & 3 deletions src/BatchReader/DirectoryDAO.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { inject, injectable } from 'inversify'
import { Collection, InsertWriteOpResult, UpdateWriteOpResult } from 'mongodb'

import { ErrorCodes } from 'Helpers/MongoDB'
Expand Down Expand Up @@ -31,11 +30,22 @@ type incEntryAttempts = (x: Entry) => Promise<UpdateWriteOpResult>

type updateFileHashes = (x: Entry) => Promise<UpdateWriteOpResult>

@injectable()
export interface Dependencies {
readonly directoryCollection: Collection
}

export interface Arguments {
readonly dependencies: Dependencies
}

export class DirectoryDAO {
private readonly directoryCollection: Collection

constructor(@inject('directoryCollection') directoryCollection: Collection) {
constructor({
dependencies: {
directoryCollection,
},
}: Arguments) {
this.directoryCollection = directoryCollection
}

Expand Down
10 changes: 7 additions & 3 deletions src/BatchReader/IPFS.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { inject, injectable } from 'inversify'
import fetch from 'node-fetch'

import { isNotNil } from 'Helpers/isNotNil'
Expand Down Expand Up @@ -40,11 +39,16 @@ export interface IPFSConfiguration {
readonly ipfsUrl: string
}

@injectable()
export interface Arguments {
readonly configuration: IPFSConfiguration
}

export class IPFS {
private readonly url: string

constructor(@inject('IPFSConfiguration') configuration: IPFSConfiguration) {
constructor({
configuration,
}: Arguments) {
this.url = configuration.ipfsUrl
}

Expand Down
27 changes: 19 additions & 8 deletions src/BatchReader/Router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { inject, injectable } from 'inversify'
import * as Pino from 'pino'
import { isNil } from 'ramda'

Expand All @@ -9,19 +8,31 @@ import { Messaging } from 'Messaging/Messaging'
import { ClaimController } from './ClaimController'
import { ExchangeConfiguration } from './ExchangeConfiguration'

@injectable()
export interface Dependencies {
readonly logger: Pino.Logger
readonly messaging: Messaging
readonly claimController: ClaimController
}

export interface Arguments {
readonly dependencies: Dependencies
readonly exchange: ExchangeConfiguration
}

export class Router {
private readonly logger: Pino.Logger
private readonly messaging: Messaging
private readonly claimController: ClaimController
private readonly exchange: ExchangeConfiguration

constructor(
@inject('Logger') logger: Pino.Logger,
@inject('Messaging') messaging: Messaging,
@inject('ClaimController') claimController: ClaimController,
@inject('ExchangeConfiguration') exchange: ExchangeConfiguration,
) {
constructor({
dependencies: {
logger,
messaging,
claimController,
},
exchange,
}: Arguments) {
this.logger = childWithFileName(logger, __filename)
this.messaging = messaging
this.claimController = claimController
Expand Down
27 changes: 19 additions & 8 deletions src/BatchReader/Service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Interval } from '@po.et/poet-js'
import { inject, injectable } from 'inversify'
import * as Pino from 'pino'

import { childWithFileName } from 'Helpers/Logging'
Expand All @@ -12,19 +11,31 @@ export interface ServiceConfiguration {
readonly readNextDirectoryIntervalInSeconds: number
}

@injectable()
export interface Dependencies {
readonly messaging: Messaging
readonly logger: Pino.Logger
}

export interface Arguments {
readonly dependencies: Dependencies
readonly configuration: ServiceConfiguration
readonly exchange: ExchangeConfiguration
}

export class Service {
private readonly interval: Interval
private readonly messaging: Messaging
private readonly logger: Pino.Logger
private readonly exchange: ExchangeConfiguration

constructor(
@inject('Logger') logger: Pino.Logger,
@inject('ServiceConfiguration') configuration: ServiceConfiguration,
@inject('Messaging') messaging: Messaging,
@inject('ExchangeConfiguration') exchange: ExchangeConfiguration,
) {
constructor({
dependencies: {
logger,
messaging,
},
configuration,
exchange,
}: Arguments) {
this.logger = childWithFileName(logger, __filename)
this.messaging = messaging
this.exchange = exchange
Expand Down

0 comments on commit f0f594c

Please sign in to comment.