Rename pool requests --> requests scheduler

This commit is contained in:
Chocobozzz 2016-05-02 17:25:05 +02:00
parent a6e7400f64
commit e3647ae226
4 changed files with 48 additions and 48 deletions

View File

@ -28,7 +28,7 @@ const customValidators = require('./server/helpers/customValidators')
const database = require('./server/initializers/database') const database = require('./server/initializers/database')
const installer = require('./server/initializers/installer') const installer = require('./server/initializers/installer')
const logger = require('./server/helpers/logger') const logger = require('./server/helpers/logger')
const poolRequests = require('./server/lib/poolRequests') const poolRequests = require('./server/lib/requestsScheduler')
const routes = require('./server/controllers') const routes = require('./server/controllers')
const utils = require('./server/helpers/utils') const utils = require('./server/helpers/utils')
const videos = require('./server/lib/videos') const videos = require('./server/lib/videos')

View File

@ -9,7 +9,7 @@ const constants = require('../initializers/constants')
const logger = require('../helpers/logger') const logger = require('../helpers/logger')
const peertubeCrypto = require('../helpers/peertubeCrypto') const peertubeCrypto = require('../helpers/peertubeCrypto')
const Pods = require('../models/pods') const Pods = require('../models/pods')
const poolRequests = require('../lib/poolRequests') const requestsScheduler = require('../lib/requestsScheduler')
const requests = require('../helpers/requests') const requests = require('../helpers/requests')
const Videos = require('../models/videos') const Videos = require('../models/videos')
@ -30,7 +30,7 @@ function addVideoToFriends (video) {
const id = video.name + video.magnetUri const id = video.name + video.magnetUri
// ensure namePath is null // ensure namePath is null
video.namePath = null video.namePath = null
poolRequests.addRequest(id, 'add', video) requestsScheduler.addRequest(id, 'add', video)
} }
function hasFriends (callback) { function hasFriends (callback) {
@ -70,9 +70,9 @@ function makeFriends (callback) {
function quitFriends (callback) { function quitFriends (callback) {
// Stop pool requests // Stop pool requests
poolRequests.deactivate() requestsScheduler.deactivate()
// Flush pool requests // Flush pool requests
poolRequests.forceSend() requestsScheduler.forceSend()
Pods.list(function (err, pods) { Pods.list(function (err, pods) {
if (err) return callback(err) if (err) return callback(err)
@ -90,7 +90,7 @@ function quitFriends (callback) {
// Announce we quit them // Announce we quit them
requests.makeMultipleRetryRequest(request, pods, function () { requests.makeMultipleRetryRequest(request, pods, function () {
Pods.removeAll(function (err) { Pods.removeAll(function (err) {
poolRequests.activate() requestsScheduler.activate()
if (err) return callback(err) if (err) return callback(err)
@ -110,7 +110,7 @@ function quitFriends (callback) {
function removeVideoToFriends (video) { function removeVideoToFriends (video) {
// To avoid duplicates // To avoid duplicates
const id = video.name + video.magnetUri const id = video.name + video.magnetUri
poolRequests.addRequest(id, 'remove', video) requestsScheduler.addRequest(id, 'remove', video)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -164,9 +164,9 @@ function getForeignPodsList (url, callback) {
function makeRequestsToWinningPods (cert, pods_list, callback) { function makeRequestsToWinningPods (cert, pods_list, callback) {
// Stop pool requests // Stop pool requests
poolRequests.deactivate() requestsScheduler.deactivate()
// Flush pool requests // Flush pool requests
poolRequests.forceSend() requestsScheduler.forceSend()
// Get the list of our videos to send to our new friends // Get the list of our videos to send to our new friends
Videos.listOwned(function (err, videos_list) { Videos.listOwned(function (err, videos_list) {
@ -213,7 +213,7 @@ function makeRequestsToWinningPods (cert, pods_list, callback) {
function endRequests (err) { function endRequests (err) {
// Now we made new friends, we can re activate the pool of requests // Now we made new friends, we can re activate the pool of requests
poolRequests.activate() requestsScheduler.activate()
if (err) { if (err) {
logger.error('There was some errors when we wanted to make friends.') logger.error('There was some errors when we wanted to make friends.')

View File

@ -6,13 +6,13 @@ const map = require('lodash/map')
const constants = require('../initializers/constants') const constants = require('../initializers/constants')
const logger = require('../helpers/logger') const logger = require('../helpers/logger')
const Pods = require('../models/pods') const Pods = require('../models/pods')
const PoolRequests = require('../models/poolRequests') const Requests = require('../models/requests')
const requests = require('../helpers/requests') const requests = require('../helpers/requests')
const Videos = require('../models/videos') const Videos = require('../models/videos')
let timer = null let timer = null
const poolRequests = { const requestsScheduler = {
activate: activate, activate: activate,
addRequest: addRequest, addRequest: addRequest,
deactivate: deactivate, deactivate: deactivate,
@ -20,16 +20,16 @@ const poolRequests = {
} }
function activate () { function activate () {
logger.info('Pool requests activated.') logger.info('Requests scheduler activated.')
timer = setInterval(makePoolRequests, constants.INTERVAL) timer = setInterval(makeRequests, constants.INTERVAL)
} }
function addRequest (id, type, request) { 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) { if (err) {
logger.error('Cannot find one pool request.', { error: err }) logger.error('Cannot find one request.', { error: err })
return // Abort return // Abort
} }
@ -40,15 +40,15 @@ function addRequest (id, type, request) {
} }
// Remove the request of the other type // Remove the request of the other type
PoolRequests.removeRequestById(id, function (err) { Requests.removeRequestById(id, function (err) {
if (err) { if (err) {
logger.error('Cannot remove a pool request.', { error: err }) logger.error('Cannot remove a request.', { error: err })
return // Abort return // Abort
} }
}) })
} else { } else {
PoolRequests.create(id, type, request, function (err) { Requests.create(id, type, request, function (err) {
if (err) logger.error('Cannot create a pool request.', { error: err }) if (err) logger.error('Cannot create a request.', { error: err })
return // Abort return // Abort
}) })
} }
@ -56,22 +56,22 @@ function addRequest (id, type, request) {
} }
function deactivate () { function deactivate () {
logger.info('Pool requests deactivated.') logger.info('Requests scheduler deactivated.')
clearInterval(timer) clearInterval(timer)
} }
function forceSend () { function forceSend () {
logger.info('Force pool requests sending.') logger.info('Force requests scheduler sending.')
makePoolRequests() 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 () {} if (!callback) callback = function () {}
Pods.list(function (err, pods) { Pods.list(function (err, pods) {
@ -118,16 +118,16 @@ function makePoolRequest (type, requests_to_make, callback) {
}) })
} }
function makePoolRequests () { function makeRequests () {
logger.info('Making pool requests to friends.') logger.info('Making requests to friends.')
PoolRequests.list(function (err, pool_requests) { Requests.list(function (err, requests) {
if (err) { 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 return // Abort
} }
if (pool_requests.length === 0) return if (requests.length === 0) return
const requests_to_make = { const requests_to_make = {
add: { add: {
@ -140,7 +140,7 @@ function makePoolRequests () {
} }
} }
async.each(pool_requests, function (pool_request, callback_each) { async.each(requests, function (pool_request, callback_each) {
if (pool_request.type === 'add') { if (pool_request.type === 'add') {
requests_to_make.add.requests.push(pool_request.request) requests_to_make.add.requests.push(pool_request.request)
requests_to_make.add.ids.push(pool_request._id) requests_to_make.add.ids.push(pool_request._id)
@ -148,7 +148,7 @@ function makePoolRequests () {
requests_to_make.remove.requests.push(pool_request.request) requests_to_make.remove.requests.push(pool_request.request)
requests_to_make.remove.ids.push(pool_request._id) requests_to_make.remove.ids.push(pool_request._id)
} else { } else {
logger.error('Unkown pool request type.', { request_type: pool_request.type }) logger.error('Unkown request type.', { request_type: pool_request.type })
return // abort return // abort
} }
@ -156,19 +156,19 @@ function makePoolRequests () {
}, function () { }, function () {
// Send the add requests // Send the add requests
if (requests_to_make.add.requests.length !== 0) { if (requests_to_make.add.requests.length !== 0) {
makePoolRequest('add', requests_to_make.add.requests, function (err) { makeRequest('add', requests_to_make.add.requests, function (err) {
if (err) logger.error('Errors when sent add pool requests.', { error: 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 // Send the remove requests
if (requests_to_make.remove.requests.length !== 0) { 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 }) 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)
}) })
} }
}) })

View File

@ -6,16 +6,16 @@ const logger = require('../helpers/logger')
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const poolRequestsSchema = mongoose.Schema({ const requestsSchema = mongoose.Schema({
type: String, type: String,
id: String, // Special id to find duplicates (video created we want to remove...) id: String, // Special id to find duplicates (video created we want to remove...)
request: mongoose.Schema.Types.Mixed request: mongoose.Schema.Types.Mixed
}) })
const PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) const RequestsDB = mongoose.model('requests', requestsSchema)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const PoolRequests = { const Requests = {
create: create, create: create,
findById: findById, findById: findById,
list: list, list: list,
@ -24,25 +24,25 @@ const PoolRequests = {
} }
function create (id, type, request, callback) { 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) { function findById (id, callback) {
PoolRequestsDB.findOne({ id: id }, callback) RequestsDB.findOne({ id: id }, callback)
} }
function list (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) { function removeRequestById (id, callback) {
PoolRequestsDB.remove({ id: id }, callback) RequestsDB.remove({ id: id }, callback)
} }
function removeRequests (ids) { function removeRequests (ids) {
PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { RequestsDB.remove({ _id: { $in: ids } }, function (err) {
if (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 return // Abort
} }
@ -52,4 +52,4 @@ function removeRequests (ids) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
module.exports = PoolRequests module.exports = Requests