2016-02-07 04:23:23 -06:00
|
|
|
'use strict'
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const mongoose = require('mongoose')
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const logger = require('../helpers/logger')
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const poolRequestsSchema = mongoose.Schema({
|
2016-02-07 04:23:23 -06:00
|
|
|
type: String,
|
|
|
|
id: String, // Special id to find duplicates (video created we want to remove...)
|
|
|
|
request: mongoose.Schema.Types.Mixed
|
|
|
|
})
|
2016-03-16 16:29:27 -05:00
|
|
|
const PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema)
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const PoolRequests = {
|
2016-02-07 04:23:23 -06:00
|
|
|
create: create,
|
|
|
|
findById: findById,
|
|
|
|
list: list,
|
|
|
|
removeRequestById: removeRequestById,
|
|
|
|
removeRequests: removeRequests
|
|
|
|
}
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function create (id, type, request, callback) {
|
|
|
|
PoolRequestsDB.create({ id: id, type: type, request: request }, callback)
|
|
|
|
}
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function findById (id, callback) {
|
|
|
|
PoolRequestsDB.findOne({ id: id }, callback)
|
|
|
|
}
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function list (callback) {
|
|
|
|
PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
|
|
|
|
}
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function removeRequestById (id, callback) {
|
|
|
|
PoolRequestsDB.remove({ id: id }, callback)
|
|
|
|
}
|
2016-02-05 12:41:22 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function removeRequests (ids) {
|
|
|
|
PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot remove requests from the pool requests database.', { error: err })
|
|
|
|
return // Abort
|
|
|
|
}
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
logger.info('Pool requests flushed.')
|
|
|
|
})
|
|
|
|
}
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
module.exports = PoolRequests
|