diff options
Diffstat (limited to 'server/lib/request')
-rw-r--r-- | server/lib/request/base-request-scheduler.ts (renamed from server/lib/request/base-request-scheduler.js) | 64 | ||||
-rw-r--r-- | server/lib/request/index.ts | 3 | ||||
-rw-r--r-- | server/lib/request/request-scheduler.ts (renamed from server/lib/request/request-scheduler.js) | 25 | ||||
-rw-r--r-- | server/lib/request/request-video-event-scheduler.ts (renamed from server/lib/request/request-video-event-scheduler.js) | 28 | ||||
-rw-r--r-- | server/lib/request/request-video-qadu-scheduler.ts (renamed from server/lib/request/request-video-qadu-scheduler.js) | 37 |
5 files changed, 101 insertions, 56 deletions
diff --git a/server/lib/request/base-request-scheduler.js b/server/lib/request/base-request-scheduler.ts index 782448340..7fc88b5f1 100644 --- a/server/lib/request/base-request-scheduler.js +++ b/server/lib/request/base-request-scheduler.ts | |||
@@ -1,19 +1,31 @@ | |||
1 | 'use strict' | 1 | import { eachLimit } from 'async/eachLimit' |
2 | 2 | ||
3 | const eachLimit = require('async/eachLimit') | ||
4 | |||
5 | const constants = require('../../initializers/constants') | ||
6 | const db = require('../../initializers/database') | 3 | const db = require('../../initializers/database') |
7 | const logger = require('../../helpers/logger') | 4 | import { logger, makeSecureRequest } from '../../helpers' |
8 | const requests = require('../../helpers/requests') | 5 | import { |
9 | 6 | API_VERSION, | |
10 | module.exports = class BaseRequestScheduler { | 7 | REQUESTS_IN_PARALLEL, |
11 | constructor (options) { | 8 | REQUESTS_INTERVAL |
9 | } from '../../initializers' | ||
10 | |||
11 | abstract class BaseRequestScheduler { | ||
12 | protected lastRequestTimestamp: number | ||
13 | protected timer: NodeJS.Timer | ||
14 | protected requestInterval: number | ||
15 | protected limitPods: number | ||
16 | protected limitPerPod: number | ||
17 | protected description: string | ||
18 | |||
19 | constructor () { | ||
12 | this.lastRequestTimestamp = 0 | 20 | this.lastRequestTimestamp = 0 |
13 | this.timer = null | 21 | this.timer = null |
14 | this.requestInterval = constants.REQUESTS_INTERVAL | 22 | this.requestInterval = REQUESTS_INTERVAL |
15 | } | 23 | } |
16 | 24 | ||
25 | abstract getRequestModel () | ||
26 | abstract getRequestToPodModel () | ||
27 | abstract buildRequestObjects (requests: any) | ||
28 | |||
17 | activate () { | 29 | activate () { |
18 | logger.info('Requests scheduler activated.') | 30 | logger.info('Requests scheduler activated.') |
19 | this.lastRequestTimestamp = Date.now() | 31 | this.lastRequestTimestamp = Date.now() |
@@ -38,30 +50,34 @@ module.exports = class BaseRequestScheduler { | |||
38 | remainingMilliSeconds () { | 50 | remainingMilliSeconds () { |
39 | if (this.timer === null) return -1 | 51 | if (this.timer === null) return -1 |
40 | 52 | ||
41 | return constants.REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp) | 53 | return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp) |
42 | } | 54 | } |
43 | 55 | ||
44 | remainingRequestsCount (callback) { | 56 | remainingRequestsCount (callback) { |
45 | return this.getRequestModel().countTotalRequests(callback) | 57 | return this.getRequestModel().countTotalRequests(callback) |
46 | } | 58 | } |
47 | 59 | ||
60 | flush (callback) { | ||
61 | this.getRequestModel().removeAll(callback) | ||
62 | } | ||
63 | |||
48 | // --------------------------------------------------------------------------- | 64 | // --------------------------------------------------------------------------- |
49 | 65 | ||
50 | // Make a requests to friends of a certain type | 66 | // Make a requests to friends of a certain type |
51 | makeRequest (toPod, requestEndpoint, requestsToMake, callback) { | 67 | protected makeRequest (toPod, requestEndpoint, requestsToMake, callback) { |
52 | if (!callback) callback = function () {} | 68 | if (!callback) callback = function () { /* empty */ } |
53 | 69 | ||
54 | const params = { | 70 | const params = { |
55 | toPod: toPod, | 71 | toPod: toPod, |
56 | sign: true, // Prove our identity | 72 | sign: true, // Prove our identity |
57 | method: 'POST', | 73 | method: 'POST', |
58 | path: '/api/' + constants.API_VERSION + '/remote/' + requestEndpoint, | 74 | path: '/api/' + API_VERSION + '/remote/' + requestEndpoint, |
59 | data: requestsToMake // Requests we need to make | 75 | data: requestsToMake // Requests we need to make |
60 | } | 76 | } |
61 | 77 | ||
62 | // Make multiple retry requests to all of pods | 78 | // Make multiple retry requests to all of pods |
63 | // The function fire some useful callbacks | 79 | // The function fire some useful callbacks |
64 | requests.makeSecureRequest(params, (err, res) => { | 80 | makeSecureRequest(params, (err, res) => { |
65 | if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { | 81 | if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { |
66 | err = err ? err.message : 'Status code not 20x : ' + res.statusCode | 82 | err = err ? err.message : 'Status code not 20x : ' + res.statusCode |
67 | logger.error('Error sending secure request to %s pod.', toPod.host, { error: err }) | 83 | logger.error('Error sending secure request to %s pod.', toPod.host, { error: err }) |
@@ -74,7 +90,7 @@ module.exports = class BaseRequestScheduler { | |||
74 | } | 90 | } |
75 | 91 | ||
76 | // Make all the requests of the scheduler | 92 | // Make all the requests of the scheduler |
77 | makeRequests () { | 93 | protected makeRequests () { |
78 | this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod, (err, requests) => { | 94 | this.getRequestModel().listWithLimitAndRandom(this.limitPods, this.limitPerPod, (err, requests) => { |
79 | if (err) { | 95 | if (err) { |
80 | logger.error('Cannot get the list of "%s".', this.description, { err: err }) | 96 | logger.error('Cannot get the list of "%s".', this.description, { err: err }) |
@@ -95,7 +111,7 @@ module.exports = class BaseRequestScheduler { | |||
95 | const goodPods = [] | 111 | const goodPods = [] |
96 | const badPods = [] | 112 | const badPods = [] |
97 | 113 | ||
98 | eachLimit(Object.keys(requestsToMakeGrouped), constants.REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => { | 114 | eachLimit(Object.keys(requestsToMakeGrouped), REQUESTS_IN_PARALLEL, (hashKey, callbackEach) => { |
99 | const requestToMake = requestsToMakeGrouped[hashKey] | 115 | const requestToMake = requestsToMakeGrouped[hashKey] |
100 | const toPod = requestToMake.toPod | 116 | const toPod = requestToMake.toPod |
101 | 117 | ||
@@ -122,15 +138,17 @@ module.exports = class BaseRequestScheduler { | |||
122 | }) | 138 | }) |
123 | } | 139 | } |
124 | 140 | ||
125 | flush (callback) { | 141 | protected afterRequestHook () { |
126 | this.getRequestModel().removeAll(callback) | ||
127 | } | ||
128 | |||
129 | afterRequestHook () { | ||
130 | // Nothing to do, let children reimplement it | 142 | // Nothing to do, let children reimplement it |
131 | } | 143 | } |
132 | 144 | ||
133 | afterRequestsHook () { | 145 | protected afterRequestsHook () { |
134 | // Nothing to do, let children reimplement it | 146 | // Nothing to do, let children reimplement it |
135 | } | 147 | } |
136 | } | 148 | } |
149 | |||
150 | // --------------------------------------------------------------------------- | ||
151 | |||
152 | export { | ||
153 | BaseRequestScheduler | ||
154 | } | ||
diff --git a/server/lib/request/index.ts b/server/lib/request/index.ts new file mode 100644 index 000000000..c98f956db --- /dev/null +++ b/server/lib/request/index.ts | |||
@@ -0,0 +1,3 @@ | |||
1 | export * from './request-scheduler' | ||
2 | export * from './request-video-event-scheduler' | ||
3 | export * from './request-video-qadu-scheduler' | ||
diff --git a/server/lib/request/request-scheduler.js b/server/lib/request/request-scheduler.ts index 555ec3e54..2006a6f03 100644 --- a/server/lib/request/request-scheduler.js +++ b/server/lib/request/request-scheduler.ts | |||
@@ -1,17 +1,18 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const constants = require('../../initializers/constants') | ||
4 | const BaseRequestScheduler = require('./base-request-scheduler') | ||
5 | const db = require('../../initializers/database') | 1 | const db = require('../../initializers/database') |
6 | const logger = require('../../helpers/logger') | 2 | import { BaseRequestScheduler } from './base-request-scheduler' |
7 | 3 | import { logger } from '../../helpers' | |
8 | module.exports = class RequestScheduler extends BaseRequestScheduler { | 4 | import { |
5 | REQUESTS_LIMIT_PODS, | ||
6 | REQUESTS_LIMIT_PER_POD | ||
7 | } from '../../initializers' | ||
8 | |||
9 | class RequestScheduler extends BaseRequestScheduler { | ||
9 | constructor () { | 10 | constructor () { |
10 | super() | 11 | super() |
11 | 12 | ||
12 | // We limit the size of the requests | 13 | // We limit the size of the requests |
13 | this.limitPods = constants.REQUESTS_LIMIT_PODS | 14 | this.limitPods = REQUESTS_LIMIT_PODS |
14 | this.limitPerPod = constants.REQUESTS_LIMIT_PER_POD | 15 | this.limitPerPod = REQUESTS_LIMIT_PER_POD |
15 | 16 | ||
16 | this.description = 'requests' | 17 | this.description = 'requests' |
17 | } | 18 | } |
@@ -95,3 +96,9 @@ module.exports = class RequestScheduler extends BaseRequestScheduler { | |||
95 | }) | 96 | }) |
96 | } | 97 | } |
97 | } | 98 | } |
99 | |||
100 | // --------------------------------------------------------------------------- | ||
101 | |||
102 | export { | ||
103 | RequestScheduler | ||
104 | } | ||
diff --git a/server/lib/request/request-video-event-scheduler.js b/server/lib/request/request-video-event-scheduler.ts index e54d34f4a..6e5306c7d 100644 --- a/server/lib/request/request-video-event-scheduler.js +++ b/server/lib/request/request-video-event-scheduler.ts | |||
@@ -1,16 +1,18 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const BaseRequestScheduler = require('./base-request-scheduler') | ||
4 | const constants = require('../../initializers/constants') | ||
5 | const db = require('../../initializers/database') | 1 | const db = require('../../initializers/database') |
6 | 2 | import { BaseRequestScheduler } from './base-request-scheduler' | |
7 | module.exports = class RequestVideoEventScheduler extends BaseRequestScheduler { | 3 | import { |
4 | REQUESTS_VIDEO_EVENT_LIMIT_PODS, | ||
5 | REQUESTS_VIDEO_EVENT_LIMIT_PER_POD, | ||
6 | REQUEST_VIDEO_EVENT_ENDPOINT | ||
7 | } from '../../initializers' | ||
8 | |||
9 | class RequestVideoEventScheduler extends BaseRequestScheduler { | ||
8 | constructor () { | 10 | constructor () { |
9 | super() | 11 | super() |
10 | 12 | ||
11 | // We limit the size of the requests | 13 | // We limit the size of the requests |
12 | this.limitPods = constants.REQUESTS_VIDEO_EVENT_LIMIT_PODS | 14 | this.limitPods = REQUESTS_VIDEO_EVENT_LIMIT_PODS |
13 | this.limitPerPod = constants.REQUESTS_VIDEO_EVENT_LIMIT_PER_POD | 15 | this.limitPerPod = REQUESTS_VIDEO_EVENT_LIMIT_PER_POD |
14 | 16 | ||
15 | this.description = 'video event requests' | 17 | this.description = 'video event requests' |
16 | } | 18 | } |
@@ -45,7 +47,7 @@ module.exports = class RequestVideoEventScheduler extends BaseRequestScheduler { | |||
45 | if (!requestsToMakeGrouped[toPodId]) { | 47 | if (!requestsToMakeGrouped[toPodId]) { |
46 | requestsToMakeGrouped[toPodId] = { | 48 | requestsToMakeGrouped[toPodId] = { |
47 | toPod: eventToProcess.pod, | 49 | toPod: eventToProcess.pod, |
48 | endpoint: constants.REQUEST_VIDEO_EVENT_ENDPOINT, | 50 | endpoint: REQUEST_VIDEO_EVENT_ENDPOINT, |
49 | ids: [], // request ids, to delete them from the DB in the future | 51 | ids: [], // request ids, to delete them from the DB in the future |
50 | datas: [] // requests data | 52 | datas: [] // requests data |
51 | } | 53 | } |
@@ -94,7 +96,7 @@ module.exports = class RequestVideoEventScheduler extends BaseRequestScheduler { | |||
94 | 96 | ||
95 | if (count === undefined) count = 1 | 97 | if (count === undefined) count = 1 |
96 | 98 | ||
97 | const dbRequestOptions = {} | 99 | const dbRequestOptions: { transaction?: any } = {} |
98 | if (transaction) dbRequestOptions.transaction = transaction | 100 | if (transaction) dbRequestOptions.transaction = transaction |
99 | 101 | ||
100 | const createQuery = { | 102 | const createQuery = { |
@@ -106,3 +108,9 @@ module.exports = class RequestVideoEventScheduler extends BaseRequestScheduler { | |||
106 | return db.RequestVideoEvent.create(createQuery, dbRequestOptions).asCallback(callback) | 108 | return db.RequestVideoEvent.create(createQuery, dbRequestOptions).asCallback(callback) |
107 | } | 109 | } |
108 | } | 110 | } |
111 | |||
112 | // --------------------------------------------------------------------------- | ||
113 | |||
114 | export { | ||
115 | RequestVideoEventScheduler | ||
116 | } | ||
diff --git a/server/lib/request/request-video-qadu-scheduler.js b/server/lib/request/request-video-qadu-scheduler.ts index 17402b556..d81822723 100644 --- a/server/lib/request/request-video-qadu-scheduler.js +++ b/server/lib/request/request-video-qadu-scheduler.ts | |||
@@ -1,17 +1,20 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const BaseRequestScheduler = require('./base-request-scheduler') | ||
4 | const constants = require('../../initializers/constants') | ||
5 | const db = require('../../initializers/database') | 1 | const db = require('../../initializers/database') |
6 | const logger = require('../../helpers/logger') | 2 | import { BaseRequestScheduler } from './base-request-scheduler' |
7 | 3 | import { logger } from '../../helpers' | |
8 | module.exports = class RequestVideoQaduScheduler extends BaseRequestScheduler { | 4 | import { |
5 | REQUESTS_VIDEO_QADU_LIMIT_PODS, | ||
6 | REQUESTS_VIDEO_QADU_LIMIT_PER_POD, | ||
7 | REQUEST_VIDEO_QADU_ENDPOINT, | ||
8 | REQUEST_VIDEO_QADU_TYPES | ||
9 | } from '../../initializers' | ||
10 | |||
11 | class RequestVideoQaduScheduler extends BaseRequestScheduler { | ||
9 | constructor () { | 12 | constructor () { |
10 | super() | 13 | super() |
11 | 14 | ||
12 | // We limit the size of the requests | 15 | // We limit the size of the requests |
13 | this.limitPods = constants.REQUESTS_VIDEO_QADU_LIMIT_PODS | 16 | this.limitPods = REQUESTS_VIDEO_QADU_LIMIT_PODS |
14 | this.limitPerPod = constants.REQUESTS_VIDEO_QADU_LIMIT_PER_POD | 17 | this.limitPerPod = REQUESTS_VIDEO_QADU_LIMIT_PER_POD |
15 | 18 | ||
16 | this.description = 'video QADU requests' | 19 | this.description = 'video QADU requests' |
17 | } | 20 | } |
@@ -37,7 +40,7 @@ module.exports = class RequestVideoQaduScheduler extends BaseRequestScheduler { | |||
37 | if (!requestsToMakeGrouped[hashKey]) { | 40 | if (!requestsToMakeGrouped[hashKey]) { |
38 | requestsToMakeGrouped[hashKey] = { | 41 | requestsToMakeGrouped[hashKey] = { |
39 | toPod: pod, | 42 | toPod: pod, |
40 | endpoint: constants.REQUEST_VIDEO_QADU_ENDPOINT, | 43 | endpoint: REQUEST_VIDEO_QADU_ENDPOINT, |
41 | ids: [], // request ids, to delete them from the DB in the future | 44 | ids: [], // request ids, to delete them from the DB in the future |
42 | datas: [], // requests data | 45 | datas: [], // requests data |
43 | videos: {} | 46 | videos: {} |
@@ -49,15 +52,15 @@ module.exports = class RequestVideoQaduScheduler extends BaseRequestScheduler { | |||
49 | if (!videoData) videoData = {} | 52 | if (!videoData) videoData = {} |
50 | 53 | ||
51 | switch (request.type) { | 54 | switch (request.type) { |
52 | case constants.REQUEST_VIDEO_QADU_TYPES.LIKES: | 55 | case REQUEST_VIDEO_QADU_TYPES.LIKES: |
53 | videoData.likes = video.likes | 56 | videoData.likes = video.likes |
54 | break | 57 | break |
55 | 58 | ||
56 | case constants.REQUEST_VIDEO_QADU_TYPES.DISLIKES: | 59 | case REQUEST_VIDEO_QADU_TYPES.DISLIKES: |
57 | videoData.dislikes = video.dislikes | 60 | videoData.dislikes = video.dislikes |
58 | break | 61 | break |
59 | 62 | ||
60 | case constants.REQUEST_VIDEO_QADU_TYPES.VIEWS: | 63 | case REQUEST_VIDEO_QADU_TYPES.VIEWS: |
61 | videoData.views = video.views | 64 | videoData.views = video.views |
62 | break | 65 | break |
63 | 66 | ||
@@ -99,7 +102,7 @@ module.exports = class RequestVideoQaduScheduler extends BaseRequestScheduler { | |||
99 | const videoId = options.videoId | 102 | const videoId = options.videoId |
100 | const transaction = options.transaction | 103 | const transaction = options.transaction |
101 | 104 | ||
102 | const dbRequestOptions = {} | 105 | const dbRequestOptions: { transaction?: any } = {} |
103 | if (transaction) dbRequestOptions.transaction = transaction | 106 | if (transaction) dbRequestOptions.transaction = transaction |
104 | 107 | ||
105 | // Send the update to all our friends | 108 | // Send the update to all our friends |
@@ -115,3 +118,9 @@ module.exports = class RequestVideoQaduScheduler extends BaseRequestScheduler { | |||
115 | }) | 118 | }) |
116 | } | 119 | } |
117 | } | 120 | } |
121 | |||
122 | // --------------------------------------------------------------------------- | ||
123 | |||
124 | export { | ||
125 | RequestVideoQaduScheduler | ||
126 | } | ||