Skip to content

Instantly share code, notes, and snippets.

@hoangsetup
Created March 26, 2022 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoangsetup/571c79b7cb2f8f1022749737bc959779 to your computer and use it in GitHub Desktop.
Save hoangsetup/571c79b7cb2f8f1022749737bc959779 to your computer and use it in GitHub Desktop.
import { ISearchResult, ITransaction } from './interfaces';
import getRedisClient from './redis';
export async function addTransaction(transactionId: string): Promise<void> {
const redisClient = await getRedisClient();
await redisClient.set(
transactionId,
JSON.stringify({ status: 'ON_QUEUE' } as ITransaction),
{ EX: 15 * 60 },
);
}
export async function updateTransactionToProcessing(transactionId: string): Promise<void> {
const redisClient = await getRedisClient();
await redisClient.set(
transactionId,
JSON.stringify({ status: 'PROCESSING' } as ITransaction),
{ KEEPTTL: true },
);
}
export async function updateTransactionToDone(
transactionId: string,
result: ISearchResult,
): Promise<void> {
const redisClient = await getRedisClient();
await redisClient.set(
transactionId,
JSON.stringify({ status: 'DONE', data: result } as ITransaction),
{ KEEPTTL: true },
);
}
export async function getTransactionResult(transactionId: string): Promise<ITransaction> {
const redisClient = await getRedisClient();
const valueString = await redisClient.get(transactionId);
if (!valueString) {
throw new Error('Transaction not found!');
}
return JSON.parse(valueString) as ITransaction;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment