2016-02-07 05:23:23 -05:00
|
|
|
'use strict'
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const mongoose = require('mongoose')
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const logger = require('../helpers/logger')
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const poolRequestsSchema = mongoose.Schema({
|
2016-02-07 05:23:23 -05:00
|
|
|
type: String,
|
|
|
|
id: String, // Special id to find duplicates (video created we want to remove...)
|
|
|
|
request: mongoose.Schema.Types.Mixed
|
|
|
|
})
|
2016-03-16 17:29:27 -04:00
|
|
|
const PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema)
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const PoolRequests = {
|
2016-02-07 05:23:23 -05:00
|
|
|
create: create,
|
|
|
|
findById: findById,
|
|
|
|
list: list,
|
|
|
|
removeRequestById: removeRequestById,
|
|
|
|
removeRequests: removeRequests
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function create (id, type, request, callback) {
|
|
|
|
PoolRequestsDB.create({ id: id, type: type, request: request }, callback)
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function findById (id, callback) {
|
|
|
|
PoolRequestsDB.findOne({ id: id }, callback)
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function list (callback) {
|
|
|
|
PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function removeRequestById (id, callback) {
|
|
|
|
PoolRequestsDB.remove({ id: id }, callback)
|
|
|
|
}
|
2016-02-05 13:41:22 -05:00
|
|
|
|
2016-02-07 05:23:23 -05: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 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.info('Pool requests flushed.')
|
|
|
|
})
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
module.exports = PoolRequests
|