const logger = require('../helpers/logger')
const peertubeCrypto = require('../helpers/peertubeCrypto')
const Pods = require('../models/pods')
-const poolRequests = require('../lib/poolRequests')
+const requestsScheduler = require('../lib/requestsScheduler')
const requests = require('../helpers/requests')
const Videos = require('../models/videos')
const id = video.name + video.magnetUri
// ensure namePath is null
video.namePath = null
- poolRequests.addRequest(id, 'add', video)
+ requestsScheduler.addRequest(id, 'add', video)
}
function hasFriends (callback) {
function quitFriends (callback) {
// Stop pool requests
- poolRequests.deactivate()
+ requestsScheduler.deactivate()
// Flush pool requests
- poolRequests.forceSend()
+ requestsScheduler.forceSend()
Pods.list(function (err, pods) {
if (err) return callback(err)
// Announce we quit them
requests.makeMultipleRetryRequest(request, pods, function () {
Pods.removeAll(function (err) {
- poolRequests.activate()
+ requestsScheduler.activate()
if (err) return callback(err)
function removeVideoToFriends (video) {
// To avoid duplicates
const id = video.name + video.magnetUri
- poolRequests.addRequest(id, 'remove', video)
+ requestsScheduler.addRequest(id, 'remove', video)
}
// ---------------------------------------------------------------------------
function makeRequestsToWinningPods (cert, pods_list, callback) {
// Stop pool requests
- poolRequests.deactivate()
+ requestsScheduler.deactivate()
// Flush pool requests
- poolRequests.forceSend()
+ requestsScheduler.forceSend()
// Get the list of our videos to send to our new friends
Videos.listOwned(function (err, videos_list) {
function endRequests (err) {
// Now we made new friends, we can re activate the pool of requests
- poolRequests.activate()
+ requestsScheduler.activate()
if (err) {
logger.error('There was some errors when we wanted to make friends.')
const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
const Pods = require('../models/pods')
-const PoolRequests = require('../models/poolRequests')
+const Requests = require('../models/requests')
const requests = require('../helpers/requests')
const Videos = require('../models/videos')
let timer = null
-const poolRequests = {
+const requestsScheduler = {
activate: activate,
addRequest: addRequest,
deactivate: deactivate,
}
function activate () {
- logger.info('Pool requests activated.')
- timer = setInterval(makePoolRequests, constants.INTERVAL)
+ logger.info('Requests scheduler activated.')
+ timer = setInterval(makeRequests, constants.INTERVAL)
}
function addRequest (id, type, request) {
- logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
+ logger.debug('Add request to the requests scheduler.', { id: id, type: type, request: request })
- PoolRequests.findById(id, function (err, entity) {
+ Requests.findById(id, function (err, entity) {
if (err) {
- logger.error('Cannot find one pool request.', { error: err })
+ logger.error('Cannot find one request.', { error: err })
return // Abort
}
}
// Remove the request of the other type
- PoolRequests.removeRequestById(id, function (err) {
+ Requests.removeRequestById(id, function (err) {
if (err) {
- logger.error('Cannot remove a pool request.', { error: err })
+ logger.error('Cannot remove a request.', { error: err })
return // Abort
}
})
} else {
- PoolRequests.create(id, type, request, function (err) {
- if (err) logger.error('Cannot create a pool request.', { error: err })
+ Requests.create(id, type, request, function (err) {
+ if (err) logger.error('Cannot create a request.', { error: err })
return // Abort
})
}
}
function deactivate () {
- logger.info('Pool requests deactivated.')
+ logger.info('Requests scheduler deactivated.')
clearInterval(timer)
}
function forceSend () {
- logger.info('Force pool requests sending.')
- makePoolRequests()
+ logger.info('Force requests scheduler sending.')
+ makeRequests()
}
// ---------------------------------------------------------------------------
-module.exports = poolRequests
+module.exports = requestsScheduler
// ---------------------------------------------------------------------------
-function makePoolRequest (type, requests_to_make, callback) {
+function makeRequest (type, requests_to_make, callback) {
if (!callback) callback = function () {}
Pods.list(function (err, pods) {
})
}
-function makePoolRequests () {
- logger.info('Making pool requests to friends.')
+function makeRequests () {
+ logger.info('Making requests to friends.')
- PoolRequests.list(function (err, pool_requests) {
+ Requests.list(function (err, requests) {
if (err) {
- logger.error('Cannot get the list of pool requests.', { err: err })
+ logger.error('Cannot get the list of requests.', { err: err })
return // Abort
}
- if (pool_requests.length === 0) return
+ if (requests.length === 0) return
const requests_to_make = {
add: {
}
}
- async.each(pool_requests, function (pool_request, callback_each) {
+ async.each(requests, function (pool_request, callback_each) {
if (pool_request.type === 'add') {
requests_to_make.add.requests.push(pool_request.request)
requests_to_make.add.ids.push(pool_request._id)
requests_to_make.remove.requests.push(pool_request.request)
requests_to_make.remove.ids.push(pool_request._id)
} else {
- logger.error('Unkown pool request type.', { request_type: pool_request.type })
+ logger.error('Unkown request type.', { request_type: pool_request.type })
return // abort
}
}, function () {
// Send the add requests
if (requests_to_make.add.requests.length !== 0) {
- makePoolRequest('add', requests_to_make.add.requests, function (err) {
- if (err) logger.error('Errors when sent add pool requests.', { error: err })
+ makeRequest('add', requests_to_make.add.requests, function (err) {
+ if (err) logger.error('Errors when sent add requests.', { error: err })
- PoolRequests.removeRequests(requests_to_make.add.ids)
+ Requests.removeRequests(requests_to_make.add.ids)
})
}
// Send the remove requests
if (requests_to_make.remove.requests.length !== 0) {
- makePoolRequest('remove', requests_to_make.remove.requests, function (err) {
+ makeRequest('remove', requests_to_make.remove.requests, function (err) {
if (err) logger.error('Errors when sent remove pool requests.', { error: err })
- PoolRequests.removeRequests(requests_to_make.remove.ids)
+ Requests.removeRequests(requests_to_make.remove.ids)
})
}
})
// ---------------------------------------------------------------------------
-const poolRequestsSchema = mongoose.Schema({
+const requestsSchema = mongoose.Schema({
type: String,
id: String, // Special id to find duplicates (video created we want to remove...)
request: mongoose.Schema.Types.Mixed
})
-const PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema)
+const RequestsDB = mongoose.model('requests', requestsSchema)
// ---------------------------------------------------------------------------
-const PoolRequests = {
+const Requests = {
create: create,
findById: findById,
list: list,
}
function create (id, type, request, callback) {
- PoolRequestsDB.create({ id: id, type: type, request: request }, callback)
+ RequestsDB.create({ id: id, type: type, request: request }, callback)
}
function findById (id, callback) {
- PoolRequestsDB.findOne({ id: id }, callback)
+ RequestsDB.findOne({ id: id }, callback)
}
function list (callback) {
- PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
+ RequestsDB.find({}, { _id: 1, type: 1, request: 1 }, callback)
}
function removeRequestById (id, callback) {
- PoolRequestsDB.remove({ id: id }, callback)
+ RequestsDB.remove({ id: id }, callback)
}
function removeRequests (ids) {
- PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
+ RequestsDB.remove({ _id: { $in: ids } }, function (err) {
if (err) {
- logger.error('Cannot remove requests from the pool requests database.', { error: err })
+ logger.error('Cannot remove requests from the requests database.', { error: err })
return // Abort
}
// ---------------------------------------------------------------------------
-module.exports = PoolRequests
+module.exports = Requests