Skip to content

Commit

Permalink
refactor: Drop InversifyJS: BatchWriter (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennylavender authored Dec 5, 2018
1 parent f0f594c commit 30093ca
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 54 deletions.
66 changes: 38 additions & 28 deletions src/BatchWriter/BatchWriter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { injectable, Container } from 'inversify'
import { Db, MongoClient, Collection } from 'mongodb'
import * as Pino from 'pino'
import { pick } from 'ramda'
Expand All @@ -20,11 +19,9 @@ export interface BatchWriterConfiguration extends LoggingConfiguration, ServiceC
readonly exchanges: ExchangeConfiguration
}

@injectable()
export class BatchWriter {
private readonly logger: Pino.Logger
private readonly configuration: BatchWriterConfiguration
private readonly container = new Container()
private mongoClient: MongoClient
private dbConnection: Db
private router: Router
Expand All @@ -45,15 +42,48 @@ export class BatchWriter {
this.messaging = new Messaging(this.configuration.rabbitmqUrl, exchangesMessaging)
await this.messaging.start()

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

const fileCollection: Collection = this.dbConnection.collection('batchWriter')

const fileDAO = new FileDAO({
dependencies: {
fileCollection,
},
})

this.router = this.container.get('Router')
const claimController = new ClaimController({
dependencies: {
fileDAO,
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: {
batchCreationIntervalInSeconds: this.configuration.batchCreationIntervalInSeconds,
},
exchange: this.configuration.exchanges,
})
await this.service.start()
const fileDAO: FileDAO = this.container.get('FileDAO')
await fileDAO.start()

this.logger.info('Batcher Writer Started')
}
Expand All @@ -65,24 +95,4 @@ export class BatchWriter {
await this.mongoClient.close()
await this.router.stop()
}

initializeContainer() {
this.container.bind<Pino.Logger>('Logger').toConstantValue(this.logger)
this.container.bind<ClaimController>('ClaimController').to(ClaimController)
this.container.bind<Db>('DB').toConstantValue(this.dbConnection)
this.container.bind<FileDAO>('FileDAO').to(FileDAO)
this.container.bind<Collection>('fileCollection').toConstantValue(this.dbConnection.collection('batchWriter'))
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({
batchCreationIntervalInSeconds: this.configuration.batchCreationIntervalInSeconds,
})

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

import { NoMoreEntriesException } from 'Exceptions'

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

@injectable()
export interface Dependencies {
readonly fileDAO: FileDAO
readonly ipfs: IPFS
}

export interface Arguments {
readonly dependencies: Dependencies
}

export class ClaimController {
private readonly fileDAO: FileDAO
private readonly ipfs: IPFS

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

export interface Entry {
Expand All @@ -18,11 +17,22 @@ type completeEntry = (x: Entry) => Promise<UpdateWriteOpResult>

type completeEntries = (xs: ReadonlyArray<Entry>) => Promise<ReadonlyArray<UpdateWriteOpResult>>

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

export interface Arguments {
readonly dependencies: Dependencies
}

export class FileDAO {
private readonly fileCollection: Collection

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

Expand Down
10 changes: 7 additions & 3 deletions src/BatchWriter/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'

type addFileToDirectory = (directoryhash: string, filehash: string) => Promise<string>
Expand All @@ -11,11 +10,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/BatchWriter/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 { NoMoreEntriesException } from 'Exceptions'
Expand All @@ -8,19 +7,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/BatchWriter/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 batchCreationIntervalInSeconds: 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 30093ca

Please sign in to comment.