]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.js
Server: improve requests scheduler
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.js
CommitLineData
9f10b292 1'use strict'
8c308c2b 2
cbe2f7c3 3const crypto = require('crypto')
ed04d94f 4const retry = require('async/retry')
cbe2f7c3 5
f0f5567b 6const logger = require('./logger')
8c308c2b 7
f0f5567b 8const utils = {
a6fd2b30 9 badRequest,
c4403b29 10 cleanForExit,
441b66f8 11 generateRandomString,
55fa55a9 12 isTestInstance,
ed04d94f
C
13 getFormatedObjects,
14 transactionRetryer
cbe2f7c3
C
15}
16
a6fd2b30
C
17function badRequest (req, res, next) {
18 res.type('json').status(400).end()
19}
20
cbe2f7c3
C
21function generateRandomString (size, callback) {
22 crypto.pseudoRandomBytes(size, function (err, raw) {
23 if (err) return callback(err)
24
25 callback(null, raw.toString('hex'))
26 })
9f10b292 27}
8c308c2b 28
bc503c2a 29function cleanForExit (webtorrentProcess) {
9f10b292 30 logger.info('Gracefully exiting.')
bc503c2a 31 process.kill(-webtorrentProcess.pid)
9f10b292 32}
0ae2e7f7 33
441b66f8
C
34function isTestInstance () {
35 return (process.env.NODE_ENV === 'test')
36}
37
55fa55a9
C
38function getFormatedObjects (objects, objectsTotal) {
39 const formatedObjects = []
40
41 objects.forEach(function (object) {
42 formatedObjects.push(object.toFormatedJSON())
43 })
44
45 return {
46 total: objectsTotal,
47 data: formatedObjects
48 }
49}
50
ed04d94f
C
51function transactionRetryer (func, callback) {
52 retry({
53 times: 5,
54
55 errorFilter: function (err) {
56 const willRetry = (err.name === 'SequelizeDatabaseError')
57 logger.debug('Maybe retrying the transaction function.', { willRetry })
58 return willRetry
59 }
60 }, func, callback)
61}
62
9f10b292 63// ---------------------------------------------------------------------------
c45f7f84 64
9f10b292 65module.exports = utils