Skip to content

Commit

Permalink
feat(#36): create event to handle the scaleDown of objects
Browse files Browse the repository at this point in the history
Co-authored-by: nukdown <nukdown@aluno.unb.br>
  • Loading branch information
matheusbsilva and nukdown committed Jun 29, 2019
1 parent cf1ee1b commit 8ff2b9e
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions lib/service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const axios = require('axios');
const EventEmitter = require('events')

class Service {
constructor(name, baseUrl) {
Expand All @@ -15,30 +16,42 @@ class Service {
}

class ServicePool {
constructor(numMaxServices, servicesInfo){
this.numMaxServices = numMaxServices
const services = Objects.keys(servicesInfo)
constructor(servicesInfos){
this.emitter = EventEmmiter()
this.servicesInfos = servicesInfos
this._eventRelease = 'releaseService'
this.emitter.on(this._eventRelease, this.scaleDownServices(serviceName))

services.forEach((service) => {
let s = new Service(service, servicesInfo[service])
const services = Objects.keys(servicesInfos)

services.forEach((service) => {
this.serviceList[service] = []

let s = new Service(service, servicesInfos[service])
this.servicesList[service].push(s)
})
}

acquire(serviceName) {
if(this.serviceList[serviceName].length === 0 ){
// TODO: Create new
if(this.serviceList[serviceName].length === 0){
if(this.servicesInfos[serviceName] !== undefined) {
let s = new Service(serviceName, servicesInfos[service])
this.servicesList[service].push(s)
}
}
return this.serviceList[serviceName].pop()
}

release(service) {
this.serviceList[service.name].push(service)
// TODO: emit event
this.emmiter.emit(this._eventRelease, service.name)
}

scaleDownServices(serviceName) {
if(this.serviceList[serviceName].length > 1) {
delete this.serviceList[serviceName].pop()
}
}
}

module.exports = {
Expand Down

0 comments on commit 8ff2b9e

Please sign in to comment.