+ function activate () {
+ logger.info('Pool requests activated.')
+ timer = setInterval(makePoolRequests, constants.INTERVAL)
+ }
+
+ function addRequest (id, type, request) {
+ logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
+
+ PoolRequests.findById(id, function (err, entity) {
+ if (err) {
+ logger.error('Cannot find one pool request.', { error: err })
+ return // Abort
+ }
+
+ if (entity) {
+ if (entity.type === type) {
+ logger.error('Cannot insert two same requests.')
+ return // Abort
+ }
+
+ // Remove the request of the other type
+ PoolRequests.removeById(id, function (err) {
+ if (err) {
+ logger.error('Cannot remove a pool request.', { error: err })
+ return // Abort
+ }
+ })
+ } else {
+ PoolRequests.create(id, type, request, function (err) {
+ logger.error('Cannot create a pool request.', { error: err })
+ return // Abort
+ })
+ }
+ })
+ }
+