]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/redis.ts
Fix job panel sorting in administration
[github/Chocobozzz/PeerTube.git] / server / lib / redis.ts
CommitLineData
ecb4e35f
C
1import { createClient, RedisClient } from 'redis'
2import { logger } from '../helpers/logger'
3import { generateRandomString } from '../helpers/utils'
b5c0e955 4import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers'
ecb4e35f
C
5
6class Redis {
7
8 private static instance: Redis
9 private initialized = false
10 private client: RedisClient
11 private prefix: string
12
13 private constructor () {}
14
15 init () {
16 // Already initialized
17 if (this.initialized === true) return
18 this.initialized = true
19
20 this.client = createClient({
21 host: CONFIG.REDIS.HOSTNAME,
22 port: CONFIG.REDIS.PORT
23 })
24
25 this.client.on('error', err => {
26 logger.error('Error in Redis client.', err)
27 process.exit(-1)
28 })
29
30 if (CONFIG.REDIS.AUTH) {
31 this.client.auth(CONFIG.REDIS.AUTH)
32 }
33
34 this.prefix = 'redis-' + CONFIG.WEBSERVER.HOST + '-'
35 }
36
37 async setResetPasswordVerificationString (userId: number) {
38 const generatedString = await generateRandomString(32)
39
40 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
41
42 return generatedString
43 }
44
45 async getResetPasswordLink (userId: number) {
46 return this.getValue(this.generateResetPasswordKey(userId))
47 }
48
b5c0e955
C
49 setView (ip: string, videoUUID: string) {
50 return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
51 }
52
53 async isViewExists (ip: string, videoUUID: string) {
54 return this.exists(this.buildViewKey(ip, videoUUID))
55 }
56
2c29ad4f
C
57 listJobs (jobsPrefix: string, state: string, mode: 'alpha', order: 'ASC' | 'DESC', offset: number, count: number) {
58 return new Promise<string[]>((res, rej) => {
59 this.client.sort(jobsPrefix + ':jobs:' + state, 'by', mode, order, 'LIMIT', offset.toString(), count.toString(), (err, values) => {
60 if (err) return rej(err)
61
62
63
64 return res(values)
65 })
66 })
67 }
68
ecb4e35f
C
69 private getValue (key: string) {
70 return new Promise<string>((res, rej) => {
71 this.client.get(this.prefix + key, (err, value) => {
72 if (err) return rej(err)
73
74 return res(value)
75 })
76 })
77 }
78
79 private setValue (key: string, value: string, expirationMilliseconds: number) {
80 return new Promise<void>((res, rej) => {
81 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
82 if (err) return rej(err)
83
84 if (ok !== 'OK') return rej(new Error('Redis result is not OK.'))
85
86 return res()
87 })
88 })
89 }
90
b5c0e955
C
91 private exists (key: string) {
92 return new Promise<boolean>((res, rej) => {
93 this.client.exists(this.prefix + key, (err, existsNumber) => {
94 if (err) return rej(err)
95
96 return res(existsNumber === 1)
97 })
98 })
99 }
100
ecb4e35f
C
101 private generateResetPasswordKey (userId: number) {
102 return 'reset-password-' + userId
103 }
104
b5c0e955
C
105 private buildViewKey (ip: string, videoUUID: string) {
106 return videoUUID + '-' + ip
107 }
108
ecb4e35f
C
109 static get Instance () {
110 return this.instance || (this.instance = new this())
111 }
112}
113
114// ---------------------------------------------------------------------------
115
116export {
117 Redis
118}