UNPKG

agilite-core

Version:

A NodeJS module that provides reusable backend functionality to the Agilit-e environment

84 lines (71 loc) 1.95 kB
'use strict' import Async from 'async' import ServicePool from 'service-pool' let pool: any = null let tempPool = {} // Create Queue Manager for Service Pool const queue = Async.queue((params, callback) => { _getExtended(params, callback) }) queue.drain(() => {}) // NATIVE FUNTIONS const get = (params) => { // console.log('--START: Service Pool Manager - Running Get') if (!pool) { pool = new ServicePool(params.servicePoolConfig) } return new Promise((resolve, reject) => { const service = pool.get(params.id) if (service) { delete tempPool[params.id] // console.log('Pool Entry exists for - ' + params.id) resolve(service) } else { // console.log('Queuing request to create new Service Pool for - ' + params.id) queue.push(params, (err, service2) => { if (err) { reject(err) } else { resolve(service2) } }) } }) } const reset = () => { pool.reset() tempPool = {} } const destroy = (id) => { if (pool) { pool.destroy(id) } delete tempPool[id] } // PRIVATE FUNTIONS const _getExtended = async (params, callback) => { let service = pool.get(params.id) if (service) { delete tempPool[params.id] // console.log('Pool Entry exists for - ' + params.id) callback(null, service) } else { // console.log('Pool Entry doesn\'t exist for - ' + params.id + '. Checking if we are allowed to create one') if (tempPool[params.id]) { // console.log('Someone else is trying to create the same request. Wait and try again for - ' + params.id) setTimeout(() => { _getExtended(params, callback) }, 10) } else { // console.log('No one\'s creating request. We can create for - ' + params.id) tempPool[params.id] = true service = await pool.add(params.servicePoolObject) callback(null, service) } } } export default { get, reset, destroy }