1
- import { SuggestedChallengeEmailTemplate } from '@/components/templates /daily-challenge' ;
1
+ import { SuggestedChallengeEmailTemplate } from '@/components/emails /daily-challenge' ;
2
2
import { prisma } from '@/lib/prisma' ;
3
3
import { resend } from '@/lib/resend' ;
4
4
import { QuestionWithTags } from '@/types/Questions' ;
@@ -14,6 +14,8 @@ import { type NextRequest, NextResponse } from 'next/server';
14
14
import React from 'react' ;
15
15
16
16
export const dynamic = 'force-dynamic' ;
17
+ export const runtime = 'nodejs' ;
18
+ export const maxDuration = 300 ; // 5 minutes
17
19
18
20
const getRandomElement = ( arr : string [ ] ) => arr [ Math . floor ( Math . random ( ) * arr . length ) ] ;
19
21
@@ -53,7 +55,7 @@ async function sendEmail(user: UserRecord, challenge: QuestionWithTags) {
53
55
} ) ;
54
56
}
55
57
56
- export async function GET ( request : NextRequest ) {
58
+ export async function POST ( request : NextRequest ) {
57
59
const authHeader = request . headers . get ( 'authorization' ) ;
58
60
if ( authHeader !== `Bearer ${ process . env . CRON_SECRET } ` ) {
59
61
return new Response ( 'Unauthorized' , {
@@ -64,25 +66,26 @@ export async function GET(request: NextRequest) {
64
66
console . log ( 'Sending daily challenge email' ) ;
65
67
66
68
try {
67
- const users = await prisma . users . findMany ( {
68
- where : {
69
- sendPushNotifications : true ,
70
- } ,
71
- } ) ;
72
-
73
- if ( ! users . length ) {
74
- return NextResponse . json ( { message : 'No users found' } , { status : 404 } ) ;
75
- }
76
-
77
- // Process users in batches of 5 to avoid connection pool exhaustion
78
- const batchSize = 5 ;
69
+ // Process users in larger batches with pagination
70
+ const batchSize = 50 ; // Increased from 5 to 50
79
71
const results = [ ] ;
80
-
81
- for ( let i = 0 ; i < users . length ; i += batchSize ) {
82
- const batch = users . slice ( i , i + batchSize ) ;
72
+ let skip = 0 ;
73
+
74
+ while ( true ) {
75
+ const users = await prisma . users . findMany ( {
76
+ where : {
77
+ sendPushNotifications : true ,
78
+ } ,
79
+ take : batchSize ,
80
+ skip,
81
+ } ) ;
82
+
83
+ if ( ! users . length ) {
84
+ break ;
85
+ }
83
86
84
87
const batchResults = await Promise . allSettled (
85
- batch . map ( async ( user ) => {
88
+ users . map ( async ( user ) => {
86
89
try {
87
90
const challenge = await getChallenge ( user . uid ) ;
88
91
if ( ! challenge ) {
@@ -103,10 +106,11 @@ export async function GET(request: NextRequest) {
103
106
) ;
104
107
105
108
results . push ( ...batchResults ) ;
109
+ skip += batchSize ;
106
110
107
- // Add a small delay between batches to allow connections to be released
108
- if ( i + batchSize < users . length ) {
109
- await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
111
+ // Add a smaller delay between batches to prevent rate limiting
112
+ if ( skip < ( await prisma . users . count ( { where : { sendPushNotifications : true } } ) ) ) {
113
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
110
114
}
111
115
}
112
116
0 commit comments