11import { logger } from "@captable/logger" ;
2- import { processJobs } from "@captable/queue" ;
2+ import { processJobsServerless } from "@captable/queue" ;
33import { type NextRequest , NextResponse } from "next/server" ;
44import "@/jobs" ; // Import to register all jobs
55
@@ -16,45 +16,27 @@ export async function GET(request: NextRequest) {
1616 }
1717
1818 try {
19- const startTime = Date . now ( ) ;
20-
21- // Process jobs in batches
22- let totalProcessed = 0 ;
23- let batchCount = 0 ;
24- const maxBatches = 10 ; // Prevent infinite loops
25-
26- while ( batchCount < maxBatches ) {
27- const processed = await processJobs ( 20 ) ; // Process 20 jobs per batch
28- totalProcessed += processed ;
29- batchCount ++ ;
30-
31- if ( processed === 0 ) {
32- break ; // No more jobs to process
33- }
34-
35- // Small delay between batches
36- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
37- }
38-
39- const duration = Date . now ( ) - startTime ;
19+ // Use serverless-optimized processing with built-in timeouts and batch management
20+ const result = await processJobsServerless ( {
21+ maxJobs : 200 , // Maximum jobs to process in this run
22+ maxBatches : 10 , // Maximum batches to prevent infinite loops
23+ batchSize : 20 , // Jobs per batch
24+ timeout : 25000 , // 25 second timeout (safe for Vercel)
25+ batchDelay : 100 , // 100ms delay between batches
26+ } ) ;
4027
41- log . info (
42- {
43- totalProcessed,
44- batches : batchCount ,
45- duration,
46- } ,
47- "Cron job processing completed" ,
48- ) ;
28+ log . info ( result , "Serverless cron job processing completed" ) ;
4929
5030 return NextResponse . json ( {
5131 success : true ,
52- processed : totalProcessed ,
53- batches : batchCount ,
54- duration,
32+ processed : result . processed ,
33+ batches : result . batches ,
34+ duration : result . duration ,
35+ timeoutReached : result . timeoutReached ,
36+ errors : result . errors ,
5537 } ) ;
5638 } catch ( error ) {
57- log . error ( { error } , "Cron job processing failed" ) ;
39+ log . error ( { error } , "Serverless cron job processing failed" ) ;
5840 return NextResponse . json (
5941 { error : "Internal server error" } ,
6042 { status : 500 } ,
0 commit comments