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

View File

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

View File

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

View File

@ -7,7 +7,21 @@ let prisma;
// If not, create a new instance of PrismaClient and attach it to the global object. // 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. // This ensures that the same instance of PrismaClient is reused across multiple invocations.
if (!global.prisma) { 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. // Assign the global PrismaClient instance to the prisma variable.