1
0
Fork 0
mirror of synced 2024-07-04 22:11:23 +12:00

Hook new Redis clients into init/shutdown flow.

This commit is contained in:
Sam Rose 2023-11-10 11:24:55 +00:00
parent d98e217c6c
commit dd2f68d099
No known key found for this signature in database
3 changed files with 32 additions and 30 deletions

View file

@ -7,7 +7,9 @@ let userClient: Client,
cacheClient: Client,
writethroughClient: Client,
lockClient: Client,
socketClient: Client
socketClient: Client,
inviteClient: Client,
passwordResetClient: Client
async function init() {
userClient = await new Client(utils.Databases.USER_CACHE).init()
@ -20,6 +22,8 @@ async function init() {
utils.Databases.SOCKET_IO,
utils.SelectableDatabase.SOCKET_IO
).init()
inviteClient = await new Client(utils.Databases.INVITATIONS).init()
passwordResetClient = await new Client(utils.Databases.PW_RESETS).init()
}
export async function shutdown() {
@ -30,6 +34,8 @@ export async function shutdown() {
if (writethroughClient) await writethroughClient.finish()
if (lockClient) await lockClient.finish()
if (socketClient) await socketClient.finish()
if (inviteClient) await inviteClient.finish()
if (passwordResetClient) await passwordResetClient.finish()
}
process.on("exit", async () => {
@ -84,3 +90,17 @@ export async function getSocketClient() {
}
return socketClient
}
export async function getInviteClient() {
if (!inviteClient) {
await init()
}
return inviteClient
}
export async function getPasswordResetClient() {
if (!passwordResetClient) {
await init()
}
return passwordResetClient
}

View file

@ -1,5 +1,6 @@
import { redis, utils, tenancy } from "../"
import { utils, tenancy } from "../"
import env from "../environment"
import { getInviteClient } from "./init"
const TTL_SECONDS = 60 * 60 * 24 * 7
@ -12,23 +13,13 @@ interface InviteWithCode extends Invite {
code: string
}
let client: redis.Client
async function getClient(): Promise<redis.Client> {
if (!client) {
client = new redis.Client(redis.utils.Databases.INVITATIONS)
await client.init()
}
return client
}
/**
* Given an invite code and invite body, allow the update an existing/valid invite in redis
* @param inviteCode The invite code for an invite in redis
* @param value The body of the updated user invitation
*/
export async function updateInviteCode(code: string, value: Invite) {
const client = await getClient()
const client = await getInviteClient()
await client.store(code, value, TTL_SECONDS)
}
@ -42,7 +33,7 @@ export async function createInviteCode(
email: string,
info: any
): Promise<string> {
const client = await getClient()
const client = await getInviteClient()
const code = utils.newid()
await client.store(code, { email, info }, TTL_SECONDS)
return code
@ -54,7 +45,7 @@ export async function createInviteCode(
* @return If the code is valid then an email address will be returned.
*/
export async function getInviteCode(code: string): Promise<Invite> {
const client = await getClient()
const client = await getInviteClient()
const value = (await client.get(code)) as Invite | undefined
if (!value) {
throw "Invitation is not valid or has expired, please request a new one."
@ -63,7 +54,7 @@ export async function getInviteCode(code: string): Promise<Invite> {
}
export async function deleteInviteCode(code: string) {
const client = await getClient()
const client = await getInviteClient()
await client.delete(code)
}
@ -71,7 +62,7 @@ export async function deleteInviteCode(code: string) {
Get all currently available user invitations for the current tenant.
**/
export async function getInviteCodes(): Promise<InviteWithCode[]> {
const client = await getClient()
const client = await getInviteClient()
const invites: { key: string; value: Invite }[] = await client.scan()
const results: InviteWithCode[] = invites.map(invite => {

View file

@ -1,4 +1,5 @@
import { redis, utils } from "../"
import { utils } from "../"
import { getPasswordResetClient } from "./init"
const TTL_SECONDS = 60 * 60
@ -7,16 +8,6 @@ interface PasswordReset {
info: any
}
let client: redis.Client
async function getClient(): Promise<redis.Client> {
if (!client) {
client = new redis.Client(redis.utils.Databases.PW_RESETS)
await client.init()
}
return client
}
/**
* Given a user ID this will store a code (that is returned) for an hour in redis.
* The user can then return this code for resetting their password (through their reset link).
@ -28,7 +19,7 @@ export async function createResetPasswordCode(
userId: string,
info: any
): Promise<string> {
const client = await getClient()
const client = await getPasswordResetClient()
const code = utils.newid()
await client.store(code, { userId, info }, TTL_SECONDS)
return code
@ -42,7 +33,7 @@ export async function createResetPasswordCode(
export async function getResetPasswordCode(
code: string
): Promise<PasswordReset> {
const client = await getClient()
const client = await getPasswordResetClient()
const value = (await client.get(code)) as PasswordReset | undefined
if (!value) {
throw "Provided information is not valid, cannot reset password - please try again."