aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/request/request-scheduler.ts
blob: 0dd796fb070d4c0149232e7771cec83d01299fab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as Sequelize from 'sequelize'

import { database as db } from '../../initializers/database'
import { AbstractRequestScheduler } from './abstract-request-scheduler'
import { logger } from '../../helpers'
import { REQUESTS_LIMIT_PODS, REQUESTS_LIMIT_PER_POD } from '../../initializers'
import { RequestsGrouped } from '../../models'
import { RequestEndpoint } from '../../../shared'

export type RequestSchedulerOptions = {
  type: string
  endpoint: RequestEndpoint
  data: Object
  toIds: number[]
  transaction: Sequelize.Transaction
}

class RequestScheduler extends AbstractRequestScheduler<RequestsGrouped> {
  constructor () {
    super()

    // We limit the size of the requests
    this.limitPods = REQUESTS_LIMIT_PODS
    this.limitPerPod = REQUESTS_LIMIT_PER_POD

    this.description = 'requests'
  }

  getRequestModel () {
    return db.Request
  }

  getRequestToPodModel () {
    return db.RequestToPod
  }

  buildRequestObjects (requestsGrouped: RequestsGrouped) {
    const requestsToMakeGrouped = {}

    Object.keys(requestsGrouped).forEach(toPodId => {
      requestsGrouped[toPodId].forEach(data => {
        const request = data.request
        const pod = data.pod
        const hashKey = toPodId + request.endpoint

        if (!requestsToMakeGrouped[hashKey]) {
          requestsToMakeGrouped[hashKey] = {
            toPod: pod,
            endpoint: request.endpoint,
            ids: [], // request ids, to delete them from the DB in the future
            datas: [] // requests data,
          }
        }

        requestsToMakeGrouped[hashKey].ids.push(request.id)
        requestsToMakeGrouped[hashKey].datas.push(request.request)
      })
    })

    return requestsToMakeGrouped
  }

  createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions) {
    // TODO: check the setPods works
    const podIds = []

    // If there are no destination pods abort
    if (toIds.length === 0) return undefined

    toIds.forEach(toPod => {
      podIds.push(toPod)
    })

    const createQuery = {
      endpoint,
      request: {
        type: type,
        data: data
      }
    }

    const dbRequestOptions: Sequelize.CreateOptions = {
      transaction
    }

    return db.Request.create(createQuery, dbRequestOptions)
      .then(request => {
        return request.setPods(podIds, dbRequestOptions)
      })
  }

  // ---------------------------------------------------------------------------

  afterRequestsHook () {
    // Flush requests with no pod
    this.getRequestModel().removeWithEmptyTo()
      .catch(err => logger.error('Error when removing requests with no pods.', { error: err }))
  }
}

// ---------------------------------------------------------------------------

export {
  RequestScheduler
}