Skip to content

Commit

Permalink
feat(#36): create scaler emitter to handle scaling down services
Browse files Browse the repository at this point in the history
Co-authored-by: nukdown <belchior.emanoel@gmail.com>
  • Loading branch information
matheusbsilva and nukdown committed Jun 29, 2019
1 parent 0f05c67 commit 48bde65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
24 changes: 24 additions & 0 deletions events/scalerEmmiter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const EventEmitter = require('events')

class ScaleEmitter extends EventEmitter {
constructor() {
super()
this._eventRelease = 'releaseService'
this.on(this._eventRelease, this.scaleDownServices)
}

scaleDownServices(serviceName, servicePool) {
if(servicePool.servicesList[serviceName].length > 1) {
console.log('Releasing service')
delete servicePool.servicesList[serviceName].pop()
}
}

emit(serviceName, servicePool) {
super.emit(this._eventType, serviceName, servicePool)
}
}

module.exports = ScaleEmitter
13 changes: 4 additions & 9 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const axios = require('axios');
const EventEmitter = require('events')
const ScaleEmitter = require('../events/scalerEmmiter.js')

class Service {
constructor(name, baseUrl) {
Expand All @@ -16,10 +17,8 @@ class Service {

class ServicePool {
constructor(servicesInfos){
this.emitter = new EventEmitter()
this.emitter = new ScaleEmitter()
this.servicesInfos = servicesInfos
this._eventRelease = 'releaseService'
this.emitter.on(this._eventRelease, this.scaleDownServices)
this.availableServices = Object.keys(servicesInfos)
this.servicesList = {}

Expand All @@ -34,6 +33,7 @@ class ServicePool {
acquire(serviceName) {
if(this.servicesList[serviceName].length === 0){
if(this.servicesInfos[serviceName] !== undefined) {
console.log('Creating new service')
let s = new Service(serviceName, servicesInfos[service])
this.servicesList[service].push(s)
}
Expand All @@ -43,14 +43,9 @@ class ServicePool {

release(service) {
this.servicesList[service.name].push(service)
this.emitter.emit(this._eventRelease, service.name, this)
this.emitter.emit(service.name, this)
}

scaleDownServices(serviceName, servicePool) {
if(servicePool.servicesList[serviceName].length > 1) {
delete servicePool.servicesList[serviceName].pop()
}
}
}

module.exports = {
Expand Down

0 comments on commit 48bde65

Please sign in to comment.