diff options
Diffstat (limited to 'server/lib/request')
-rw-r--r-- | server/lib/request/base-request-scheduler.ts | 16 | ||||
-rw-r--r-- | server/lib/request/index.ts | 1 | ||||
-rw-r--r-- | server/lib/request/request-scheduler.ts | 23 | ||||
-rw-r--r-- | server/lib/request/request-video-event-scheduler.ts | 21 | ||||
-rw-r--r-- | server/lib/request/request-video-qadu-scheduler.ts | 21 |
5 files changed, 47 insertions, 35 deletions
diff --git a/server/lib/request/base-request-scheduler.ts b/server/lib/request/base-request-scheduler.ts index b7ef6abf9..26bdc2bff 100644 --- a/server/lib/request/base-request-scheduler.ts +++ b/server/lib/request/base-request-scheduler.ts | |||
@@ -2,6 +2,7 @@ import * as eachLimit from 'async/eachLimit' | |||
2 | 2 | ||
3 | import { database as db } from '../../initializers/database' | 3 | import { database as db } from '../../initializers/database' |
4 | import { logger, makeSecureRequest } from '../../helpers' | 4 | import { logger, makeSecureRequest } from '../../helpers' |
5 | import { PodInstance } from '../../models' | ||
5 | import { | 6 | import { |
6 | API_VERSION, | 7 | API_VERSION, |
7 | REQUESTS_IN_PARALLEL, | 8 | REQUESTS_IN_PARALLEL, |
@@ -9,11 +10,12 @@ import { | |||
9 | } from '../../initializers' | 10 | } from '../../initializers' |
10 | 11 | ||
11 | abstract class BaseRequestScheduler { | 12 | abstract class BaseRequestScheduler { |
13 | requestInterval: number | ||
14 | limitPods: number | ||
15 | limitPerPod: number | ||
16 | |||
12 | protected lastRequestTimestamp: number | 17 | protected lastRequestTimestamp: number |
13 | protected timer: NodeJS.Timer | 18 | protected timer: NodeJS.Timer |
14 | protected requestInterval: number | ||
15 | protected limitPods: number | ||
16 | protected limitPerPod: number | ||
17 | protected description: string | 19 | protected description: string |
18 | 20 | ||
19 | constructor () { | 21 | constructor () { |
@@ -53,24 +55,24 @@ abstract class BaseRequestScheduler { | |||
53 | return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp) | 55 | return REQUESTS_INTERVAL - (Date.now() - this.lastRequestTimestamp) |
54 | } | 56 | } |
55 | 57 | ||
56 | remainingRequestsCount (callback) { | 58 | remainingRequestsCount (callback: (err: Error, total: number) => void) { |
57 | return this.getRequestModel().countTotalRequests(callback) | 59 | return this.getRequestModel().countTotalRequests(callback) |
58 | } | 60 | } |
59 | 61 | ||
60 | flush (callback) { | 62 | flush (callback: (err: Error) => void) { |
61 | this.getRequestModel().removeAll(callback) | 63 | this.getRequestModel().removeAll(callback) |
62 | } | 64 | } |
63 | 65 | ||
64 | // --------------------------------------------------------------------------- | 66 | // --------------------------------------------------------------------------- |
65 | 67 | ||
66 | // Make a requests to friends of a certain type | 68 | // Make a requests to friends of a certain type |
67 | protected makeRequest (toPod, requestEndpoint, requestsToMake, callback) { | 69 | protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object, callback) { |
68 | if (!callback) callback = function () { /* empty */ } | 70 | if (!callback) callback = function () { /* empty */ } |
69 | 71 | ||
70 | const params = { | 72 | const params = { |
71 | toPod: toPod, | 73 | toPod: toPod, |
72 | sign: true, // Prove our identity | 74 | sign: true, // Prove our identity |
73 | method: 'POST', | 75 | method: 'POST' as 'POST', |
74 | path: '/api/' + API_VERSION + '/remote/' + requestEndpoint, | 76 | path: '/api/' + API_VERSION + '/remote/' + requestEndpoint, |
75 | data: requestsToMake // Requests we need to make | 77 | data: requestsToMake // Requests we need to make |
76 | } | 78 | } |
diff --git a/server/lib/request/index.ts b/server/lib/request/index.ts index c98f956db..110d0ed78 100644 --- a/server/lib/request/index.ts +++ b/server/lib/request/index.ts | |||
@@ -1,3 +1,4 @@ | |||
1 | export * from './base-request-scheduler' | ||
1 | export * from './request-scheduler' | 2 | export * from './request-scheduler' |
2 | export * from './request-video-event-scheduler' | 3 | export * from './request-video-event-scheduler' |
3 | export * from './request-video-qadu-scheduler' | 4 | export * from './request-video-qadu-scheduler' |
diff --git a/server/lib/request/request-scheduler.ts b/server/lib/request/request-scheduler.ts index 26ffbfb86..69d840eeb 100644 --- a/server/lib/request/request-scheduler.ts +++ b/server/lib/request/request-scheduler.ts | |||
@@ -1,3 +1,5 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
1 | import { database as db } from '../../initializers/database' | 3 | import { database as db } from '../../initializers/database' |
2 | import { BaseRequestScheduler } from './base-request-scheduler' | 4 | import { BaseRequestScheduler } from './base-request-scheduler' |
3 | import { logger } from '../../helpers' | 5 | import { logger } from '../../helpers' |
@@ -6,6 +8,14 @@ import { | |||
6 | REQUESTS_LIMIT_PER_POD | 8 | REQUESTS_LIMIT_PER_POD |
7 | } from '../../initializers' | 9 | } from '../../initializers' |
8 | 10 | ||
11 | export type RequestSchedulerOptions = { | ||
12 | type: string | ||
13 | endpoint: string | ||
14 | data: Object | ||
15 | toIds: number[] | ||
16 | transaction: Sequelize.Transaction | ||
17 | } | ||
18 | |||
9 | class RequestScheduler extends BaseRequestScheduler { | 19 | class RequestScheduler extends BaseRequestScheduler { |
10 | constructor () { | 20 | constructor () { |
11 | super() | 21 | super() |
@@ -25,7 +35,7 @@ class RequestScheduler extends BaseRequestScheduler { | |||
25 | return db.RequestToPod | 35 | return db.RequestToPod |
26 | } | 36 | } |
27 | 37 | ||
28 | buildRequestObjects (requests) { | 38 | buildRequestObjects (requests: { [ toPodId: number ]: any }) { |
29 | const requestsToMakeGrouped = {} | 39 | const requestsToMakeGrouped = {} |
30 | 40 | ||
31 | Object.keys(requests).forEach(toPodId => { | 41 | Object.keys(requests).forEach(toPodId => { |
@@ -51,14 +61,7 @@ class RequestScheduler extends BaseRequestScheduler { | |||
51 | return requestsToMakeGrouped | 61 | return requestsToMakeGrouped |
52 | } | 62 | } |
53 | 63 | ||
54 | // { type, endpoint, data, toIds, transaction } | 64 | createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions, callback: (err: Error) => void) { |
55 | createRequest (options, callback) { | ||
56 | const type = options.type | ||
57 | const endpoint = options.endpoint | ||
58 | const data = options.data | ||
59 | const toIds = options.toIds | ||
60 | const transaction = options.transaction | ||
61 | |||
62 | // TODO: check the setPods works | 65 | // TODO: check the setPods works |
63 | const podIds = [] | 66 | const podIds = [] |
64 | 67 | ||
@@ -77,7 +80,7 @@ class RequestScheduler extends BaseRequestScheduler { | |||
77 | } | 80 | } |
78 | } | 81 | } |
79 | 82 | ||
80 | const dbRequestOptions = { | 83 | const dbRequestOptions: Sequelize.CreateOptions = { |
81 | transaction | 84 | transaction |
82 | } | 85 | } |
83 | 86 | ||
diff --git a/server/lib/request/request-video-event-scheduler.ts b/server/lib/request/request-video-event-scheduler.ts index bde50b1d3..9da82585e 100644 --- a/server/lib/request/request-video-event-scheduler.ts +++ b/server/lib/request/request-video-event-scheduler.ts | |||
@@ -1,3 +1,5 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
1 | import { database as db } from '../../initializers/database' | 3 | import { database as db } from '../../initializers/database' |
2 | import { BaseRequestScheduler } from './base-request-scheduler' | 4 | import { BaseRequestScheduler } from './base-request-scheduler' |
3 | import { | 5 | import { |
@@ -6,6 +8,13 @@ import { | |||
6 | REQUEST_VIDEO_EVENT_ENDPOINT | 8 | REQUEST_VIDEO_EVENT_ENDPOINT |
7 | } from '../../initializers' | 9 | } from '../../initializers' |
8 | 10 | ||
11 | export type RequestVideoEventSchedulerOptions = { | ||
12 | type: string | ||
13 | videoId: string | ||
14 | count?: number | ||
15 | transaction?: Sequelize.Transaction | ||
16 | } | ||
17 | |||
9 | class RequestVideoEventScheduler extends BaseRequestScheduler { | 18 | class RequestVideoEventScheduler extends BaseRequestScheduler { |
10 | constructor () { | 19 | constructor () { |
11 | super() | 20 | super() |
@@ -25,7 +34,7 @@ class RequestVideoEventScheduler extends BaseRequestScheduler { | |||
25 | return db.RequestVideoEvent | 34 | return db.RequestVideoEvent |
26 | } | 35 | } |
27 | 36 | ||
28 | buildRequestObjects (eventsToProcess) { | 37 | buildRequestObjects (eventsToProcess: { [ toPodId: number ]: any }[]) { |
29 | const requestsToMakeGrouped = {} | 38 | const requestsToMakeGrouped = {} |
30 | 39 | ||
31 | /* Example: | 40 | /* Example: |
@@ -87,16 +96,10 @@ class RequestVideoEventScheduler extends BaseRequestScheduler { | |||
87 | return requestsToMakeGrouped | 96 | return requestsToMakeGrouped |
88 | } | 97 | } |
89 | 98 | ||
90 | // { type, videoId, count?, transaction? } | 99 | createRequest ({ type, videoId, count, transaction }: RequestVideoEventSchedulerOptions, callback: (err: Error) => void) { |
91 | createRequest (options, callback) { | ||
92 | const type = options.type | ||
93 | const videoId = options.videoId | ||
94 | const transaction = options.transaction | ||
95 | let count = options.count | ||
96 | |||
97 | if (count === undefined) count = 1 | 100 | if (count === undefined) count = 1 |
98 | 101 | ||
99 | const dbRequestOptions: { transaction?: any } = {} | 102 | const dbRequestOptions: Sequelize.CreateOptions = {} |
100 | if (transaction) dbRequestOptions.transaction = transaction | 103 | if (transaction) dbRequestOptions.transaction = transaction |
101 | 104 | ||
102 | const createQuery = { | 105 | const createQuery = { |
diff --git a/server/lib/request/request-video-qadu-scheduler.ts b/server/lib/request/request-video-qadu-scheduler.ts index dab526088..436fd8e50 100644 --- a/server/lib/request/request-video-qadu-scheduler.ts +++ b/server/lib/request/request-video-qadu-scheduler.ts | |||
@@ -1,3 +1,5 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
1 | import { database as db } from '../../initializers/database' | 3 | import { database as db } from '../../initializers/database' |
2 | import { BaseRequestScheduler } from './base-request-scheduler' | 4 | import { BaseRequestScheduler } from './base-request-scheduler' |
3 | import { logger } from '../../helpers' | 5 | import { logger } from '../../helpers' |
@@ -8,6 +10,12 @@ import { | |||
8 | REQUEST_VIDEO_QADU_TYPES | 10 | REQUEST_VIDEO_QADU_TYPES |
9 | } from '../../initializers' | 11 | } from '../../initializers' |
10 | 12 | ||
13 | export type RequestVideoQaduSchedulerOptions = { | ||
14 | type: string | ||
15 | videoId: string | ||
16 | transaction?: Sequelize.Transaction | ||
17 | } | ||
18 | |||
11 | class RequestVideoQaduScheduler extends BaseRequestScheduler { | 19 | class RequestVideoQaduScheduler extends BaseRequestScheduler { |
12 | constructor () { | 20 | constructor () { |
13 | super() | 21 | super() |
@@ -27,7 +35,7 @@ class RequestVideoQaduScheduler extends BaseRequestScheduler { | |||
27 | return db.RequestVideoQadu | 35 | return db.RequestVideoQadu |
28 | } | 36 | } |
29 | 37 | ||
30 | buildRequestObjects (requests) { | 38 | buildRequestObjects (requests: { [ toPodId: number ]: any }[]) { |
31 | const requestsToMakeGrouped = {} | 39 | const requestsToMakeGrouped = {} |
32 | 40 | ||
33 | Object.keys(requests).forEach(toPodId => { | 41 | Object.keys(requests).forEach(toPodId => { |
@@ -96,17 +104,12 @@ class RequestVideoQaduScheduler extends BaseRequestScheduler { | |||
96 | return requestsToMakeGrouped | 104 | return requestsToMakeGrouped |
97 | } | 105 | } |
98 | 106 | ||
99 | // { type, videoId, transaction? } | 107 | createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions, callback: (err: Error) => void) { |
100 | createRequest (options, callback) { | 108 | const dbRequestOptions: Sequelize.BulkCreateOptions = {} |
101 | const type = options.type | ||
102 | const videoId = options.videoId | ||
103 | const transaction = options.transaction | ||
104 | |||
105 | const dbRequestOptions: { transaction?: any } = {} | ||
106 | if (transaction) dbRequestOptions.transaction = transaction | 109 | if (transaction) dbRequestOptions.transaction = transaction |
107 | 110 | ||
108 | // Send the update to all our friends | 111 | // Send the update to all our friends |
109 | db.Pod.listAllIds(options.transaction, function (err, podIds) { | 112 | db.Pod.listAllIds(transaction, function (err, podIds) { |
110 | if (err) return callback(err) | 113 | if (err) return callback(err) |
111 | 114 | ||
112 | const queries = [] | 115 | const queries = [] |