2021-05-05 18:49:34 +02:00
|
|
|
const env = require("../environment")
|
|
|
|
// ioredis mock is all in memory
|
|
|
|
const Redis = env.isTest() ? require("ioredis-mock") : require("ioredis")
|
2021-07-06 19:10:04 +02:00
|
|
|
const { addDbPrefix, removeDbPrefix, getRedisOptions, SEPARATOR } = require("./utils")
|
2021-04-27 18:29:05 +02:00
|
|
|
|
2021-05-24 15:54:47 +02:00
|
|
|
const RETRY_PERIOD_MS = 2000
|
2021-05-24 17:30:24 +02:00
|
|
|
const STARTUP_TIMEOUT_MS = 5000
|
2021-04-27 18:29:05 +02:00
|
|
|
const CLUSTERED = false
|
|
|
|
|
2021-05-05 18:49:34 +02:00
|
|
|
// for testing just generate the client once
|
2021-05-24 18:05:46 +02:00
|
|
|
let CLOSED = false
|
2021-05-05 18:49:34 +02:00
|
|
|
let CLIENT = env.isTest() ? new Redis(getRedisOptions()) : null
|
2021-05-24 18:05:46 +02:00
|
|
|
// if in test always connected
|
|
|
|
let CONNECTED = !!env.isTest()
|
|
|
|
|
|
|
|
function connectionError(timeout, err) {
|
|
|
|
// manually shut down, ignore errors
|
|
|
|
if (CLOSED) {
|
|
|
|
return
|
|
|
|
}
|
2021-06-25 16:32:59 +02:00
|
|
|
CLIENT.end()
|
|
|
|
CLOSED = true
|
2021-05-24 18:05:46 +02:00
|
|
|
// always clear this on error
|
|
|
|
clearTimeout(timeout)
|
|
|
|
CONNECTED = false
|
|
|
|
console.error("Redis connection failed - " + err)
|
|
|
|
setTimeout(() => {
|
|
|
|
init()
|
|
|
|
}, RETRY_PERIOD_MS)
|
|
|
|
}
|
2021-04-27 18:29:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inits the system, will error if unable to connect to redis cluster (may take up to 10 seconds) otherwise
|
|
|
|
* will return the ioredis client which will be ready to use.
|
|
|
|
*/
|
|
|
|
function init() {
|
2021-05-24 17:30:24 +02:00
|
|
|
let timeout
|
2021-05-24 18:05:46 +02:00
|
|
|
CLOSED = false
|
2021-05-24 15:54:47 +02:00
|
|
|
// testing uses a single in memory client
|
|
|
|
if (env.isTest() || (CLIENT && CONNECTED)) {
|
|
|
|
return
|
|
|
|
}
|
2021-05-24 17:30:24 +02:00
|
|
|
// start the timer - only allowed 5 seconds to connect
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
if (!CONNECTED) {
|
2021-06-25 16:32:59 +02:00
|
|
|
connectionError(timeout, "Did not successfully connect in timeout")
|
2021-05-24 17:30:24 +02:00
|
|
|
}
|
|
|
|
}, STARTUP_TIMEOUT_MS)
|
|
|
|
|
|
|
|
// disconnect any lingering client
|
2021-05-24 15:54:47 +02:00
|
|
|
if (CLIENT) {
|
|
|
|
CLIENT.disconnect()
|
|
|
|
}
|
|
|
|
const { opts, host, port } = getRedisOptions(CLUSTERED)
|
|
|
|
if (CLUSTERED) {
|
|
|
|
CLIENT = new Redis.Cluster([{ host, port }], opts)
|
|
|
|
} else {
|
|
|
|
CLIENT = new Redis(opts)
|
|
|
|
}
|
2021-05-24 17:30:24 +02:00
|
|
|
// attach handlers
|
2021-05-24 15:54:47 +02:00
|
|
|
CLIENT.on("end", err => {
|
2021-05-24 18:05:46 +02:00
|
|
|
connectionError(timeout, err)
|
2021-05-24 15:54:47 +02:00
|
|
|
})
|
|
|
|
CLIENT.on("error", err => {
|
2021-05-24 18:05:46 +02:00
|
|
|
connectionError(timeout, err)
|
2021-05-24 15:54:47 +02:00
|
|
|
})
|
|
|
|
CLIENT.on("connect", () => {
|
2021-05-24 17:30:24 +02:00
|
|
|
clearTimeout(timeout)
|
2021-05-24 15:54:47 +02:00
|
|
|
CONNECTED = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForConnection() {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
if (CLIENT == null) {
|
|
|
|
init()
|
|
|
|
} else if (CONNECTED) {
|
|
|
|
resolve()
|
|
|
|
return
|
2021-04-27 18:29:05 +02:00
|
|
|
}
|
2021-05-24 17:31:50 +02:00
|
|
|
// check if the connection is ready
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
if (CONNECTED) {
|
|
|
|
clearInterval(interval)
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
}, 500)
|
2021-04-27 18:29:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility function, takes a redis stream and converts it to a promisified response -
|
|
|
|
* this can only be done with redis streams because they will have an end.
|
|
|
|
* @param stream A redis stream, specifically as this type of stream will have an end.
|
|
|
|
* @return {Promise<object>} The final output of the stream
|
|
|
|
*/
|
|
|
|
function promisifyStream(stream) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const outputKeys = new Set()
|
|
|
|
stream.on("data", keys => {
|
|
|
|
keys.forEach(key => {
|
|
|
|
outputKeys.add(key)
|
|
|
|
})
|
|
|
|
})
|
2021-04-27 18:30:19 +02:00
|
|
|
stream.on("error", err => {
|
2021-04-27 18:29:05 +02:00
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
stream.on("end", async () => {
|
|
|
|
const keysArray = Array.from(outputKeys)
|
|
|
|
try {
|
|
|
|
let getPromises = []
|
|
|
|
for (let key of keysArray) {
|
|
|
|
getPromises.push(CLIENT.get(key))
|
|
|
|
}
|
|
|
|
const jsonArray = await Promise.all(getPromises)
|
2021-04-27 18:30:19 +02:00
|
|
|
resolve(
|
|
|
|
keysArray.map(key => ({
|
|
|
|
key: removeDbPrefix(key),
|
|
|
|
value: JSON.parse(jsonArray.shift()),
|
|
|
|
}))
|
|
|
|
)
|
2021-04-27 18:29:05 +02:00
|
|
|
} catch (err) {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
class RedisWrapper {
|
|
|
|
constructor(db) {
|
|
|
|
this._db = db
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2021-05-24 18:05:46 +02:00
|
|
|
CLOSED = false
|
2021-05-24 15:54:47 +02:00
|
|
|
init()
|
|
|
|
await waitForConnection()
|
2021-04-27 18:29:05 +02:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:11:06 +02:00
|
|
|
async finish() {
|
2021-05-24 18:05:46 +02:00
|
|
|
CLOSED = true
|
2021-05-24 15:54:47 +02:00
|
|
|
CLIENT.disconnect()
|
2021-05-05 13:11:06 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 19:10:04 +02:00
|
|
|
async scan(key = "") {
|
2021-05-24 15:54:47 +02:00
|
|
|
const db = this._db
|
2021-07-06 19:10:04 +02:00
|
|
|
key = `${db}${SEPARATOR}${key}`
|
2021-04-27 18:29:05 +02:00
|
|
|
let stream
|
|
|
|
if (CLUSTERED) {
|
2021-05-24 15:54:47 +02:00
|
|
|
let node = CLIENT.nodes("master")
|
2021-07-06 19:10:04 +02:00
|
|
|
stream = node[0].scanStream({ match: key + "*", count: 100 })
|
2021-04-27 18:29:05 +02:00
|
|
|
} else {
|
2021-07-06 19:10:04 +02:00
|
|
|
stream = CLIENT.scanStream({ match: key + "*", count: 100 })
|
2021-04-27 18:29:05 +02:00
|
|
|
}
|
|
|
|
return promisifyStream(stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(key) {
|
2021-05-24 15:54:47 +02:00
|
|
|
const db = this._db
|
|
|
|
let response = await CLIENT.get(addDbPrefix(db, key))
|
2021-04-27 18:29:05 +02:00
|
|
|
// overwrite the prefixed key
|
|
|
|
if (response != null && response.key) {
|
|
|
|
response.key = key
|
|
|
|
}
|
2021-05-05 18:49:34 +02:00
|
|
|
// if its not an object just return the response
|
|
|
|
try {
|
|
|
|
return JSON.parse(response)
|
|
|
|
} catch (err) {
|
|
|
|
return response
|
|
|
|
}
|
2021-04-27 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async store(key, value, expirySeconds = null) {
|
2021-05-24 15:54:47 +02:00
|
|
|
const db = this._db
|
2021-04-27 18:30:19 +02:00
|
|
|
if (typeof value === "object") {
|
2021-04-27 18:29:05 +02:00
|
|
|
value = JSON.stringify(value)
|
|
|
|
}
|
|
|
|
const prefixedKey = addDbPrefix(db, key)
|
2021-05-24 15:54:47 +02:00
|
|
|
await CLIENT.set(prefixedKey, value)
|
2021-04-27 18:29:05 +02:00
|
|
|
if (expirySeconds) {
|
2021-05-24 15:54:47 +02:00
|
|
|
await CLIENT.expire(prefixedKey, expirySeconds)
|
2021-04-27 18:29:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 19:10:04 +02:00
|
|
|
async setExpiry(key, expirySeconds) {
|
|
|
|
const db = this._db
|
|
|
|
const prefixedKey = addDbPrefix(db, key)
|
|
|
|
await CLIENT.expire(prefixedKey, expirySeconds)
|
|
|
|
}
|
|
|
|
|
2021-04-27 18:29:05 +02:00
|
|
|
async delete(key) {
|
2021-05-24 15:54:47 +02:00
|
|
|
const db = this._db
|
|
|
|
await CLIENT.del(addDbPrefix(db, key))
|
2021-04-27 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async clear() {
|
2021-05-13 15:34:04 +02:00
|
|
|
let items = await this.scan()
|
|
|
|
await Promise.all(items.map(obj => this.delete(obj.key)))
|
2021-04-27 18:29:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RedisWrapper
|