Fix for pool connection issues on cron

This commit is contained in:
austinkelsay 2024-10-28 15:16:22 -05:00
parent e96e0ba1dc
commit 523eaf5b6a
No known key found for this signature in database
GPG Key ID: 44CB4EC6D9F2FA02
3 changed files with 82 additions and 55 deletions

@ -7,6 +7,7 @@ datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL")
directUrl = env("POSTGRES_URL_NON_POOLING")
relationMode = "prisma"
}
generator client {

@ -151,6 +151,7 @@ export const deleteUser = async (id) => {
};
export const updateUserSubscription = async (userId, isSubscribed, nwc) => {
try {
const now = new Date();
return await prisma.user.update({
where: { id: userId },
@ -178,18 +179,20 @@ export const updateUserSubscription = async (userId, isSubscribed, nwc) => {
role: true,
},
});
} finally {
await prisma.$disconnect();
}
};
export const findExpiredSubscriptions = async () => {
try {
const now = new Date();
// const thirtyOneDaysAgo = new Date(now.getTime() - 31 * 24 * 60 * 60 * 1000);
const oneDayAndOneHourAgo = new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000 - 1 * 60 * 60 * 1000);
return await prisma.role.findMany({
const result = await prisma.role.findMany({
where: {
subscribed: true,
lastPaymentAt: {
// lt: thirtyOneDaysAgo
lt: oneDayAndOneHourAgo
}
},
@ -198,9 +201,15 @@ export const findExpiredSubscriptions = async () => {
nwc: true
}
});
return result;
} finally {
await prisma.$disconnect();
}
};
export const expireUserSubscriptions = async (userIds) => {
try {
const now = new Date();
const updatePromises = userIds.map((userId) =>
prisma.role.update({
@ -217,6 +226,9 @@ export const expireUserSubscriptions = async (userIds) => {
await prisma.$transaction(updatePromises);
return userIds.length;
} finally {
await prisma.$disconnect();
}
};
export const getUserByEmail = async (email) => {

@ -7,7 +7,21 @@ let prisma;
// If not, create a new instance of PrismaClient and attach it to the global object.
// This ensures that the same instance of PrismaClient is reused across multiple invocations.
if (!global.prisma) {
global.prisma = new PrismaClient();
global.prisma = new PrismaClient({
datasources: {
db: {
url: process.env.POSTGRES_PRISMA_URL
}
},
// Add connection pool configuration
connection: {
pool: {
min: 0,
max: 1,
idleTimeoutMillis: 5000
}
}
});
}
// Assign the global PrismaClient instance to the prisma variable.