]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/request/request-scheduler.ts
Better typescript typing for a better world
[github/Chocobozzz/PeerTube.git] / server / lib / request / request-scheduler.ts
index 575e0227c8e397cb60c31703c159d660bb7a008d..696875dcf4e27317771581d8b9d8e55ca92e3882 100644 (file)
@@ -1,13 +1,11 @@
 import * as Sequelize from 'sequelize'
 
 import { database as db } from '../../initializers/database'
-import { AbstractRequestScheduler } from './abstract-request-scheduler'
+import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler'
 import { logger } from '../../helpers'
-import {
-  REQUESTS_LIMIT_PODS,
-  REQUESTS_LIMIT_PER_POD
-} from '../../initializers'
-import { RequestEndpoint } from '../../../shared'
+import { REQUESTS_LIMIT_PODS, REQUESTS_LIMIT_PER_POD } from '../../initializers'
+import { RequestsGrouped } from '../../models'
+import { RequestEndpoint, RemoteVideoRequest } from '../../../shared'
 
 export type RequestSchedulerOptions = {
   type: string
@@ -17,7 +15,7 @@ export type RequestSchedulerOptions = {
   transaction: Sequelize.Transaction
 }
 
-class RequestScheduler extends AbstractRequestScheduler {
+class RequestScheduler extends AbstractRequestScheduler<RequestsGrouped> {
   constructor () {
     super()
 
@@ -36,11 +34,11 @@ class RequestScheduler extends AbstractRequestScheduler {
     return db.RequestToPod
   }
 
-  buildRequestObjects (requests: { [ toPodId: number ]: any }) {
-    const requestsToMakeGrouped = {}
+  buildRequestsObjects (requestsGrouped: RequestsGrouped) {
+    const requestsToMakeGrouped: RequestsObjects<RemoteVideoRequest> = {}
 
-    Object.keys(requests).forEach(toPodId => {
-      requests[toPodId].forEach(data => {
+    Object.keys(requestsGrouped).forEach(toPodId => {
+      requestsGrouped[toPodId].forEach(data => {
         const request = data.request
         const pod = data.pod
         const hashKey = toPodId + request.endpoint
@@ -62,16 +60,9 @@ class RequestScheduler extends AbstractRequestScheduler {
     return requestsToMakeGrouped
   }
 
-  createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions, callback: (err: Error) => void) {
-    // TODO: check the setPods works
-    const podIds = []
-
+  createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions) {
     // If there are no destination pods abort
-    if (toIds.length === 0) return callback(null)
-
-    toIds.forEach(toPod => {
-      podIds.push(toPod)
-    })
+    if (toIds.length === 0) return undefined
 
     const createQuery = {
       endpoint,
@@ -85,20 +76,18 @@ class RequestScheduler extends AbstractRequestScheduler {
       transaction
     }
 
-    return db.Request.create(createQuery, dbRequestOptions).asCallback((err, request) => {
-      if (err) return callback(err)
-
-      return request.setPods(podIds, dbRequestOptions).asCallback(callback)
-    })
+    return db.Request.create(createQuery, dbRequestOptions)
+      .then(request => {
+        return request.setPods(toIds, dbRequestOptions)
+      })
   }
 
   // ---------------------------------------------------------------------------
 
   afterRequestsHook () {
     // Flush requests with no pod
-    this.getRequestModel().removeWithEmptyTo(err => {
-      if (err) logger.error('Error when removing requests with no pods.', { error: err })
-    })
+    this.getRequestModel().removeWithEmptyTo()
+      .catch(err => logger.error('Error when removing requests with no pods.', err))
   }
 }