Skip to content

Commit aa05ee6

Browse files
authored
Merge pull request #557 from captableinc/feat/queue-refactor
feat: supporting both long running process and serverless, and 100% test coverage
2 parents 745ee01 + 3b6f0e9 commit aa05ee6

File tree

21 files changed

+3176
-349
lines changed

21 files changed

+3176
-349
lines changed

apps/captable/app/api/cron/process-jobs/route.ts

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { logger } from "@captable/logger";
2-
import { processJobs } from "@captable/queue";
2+
import { processJobsServerless } from "@captable/queue";
33
import { type NextRequest, NextResponse } from "next/server";
44
import "@/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 },

bun.lock

Lines changed: 34 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"db:migrate": "dotenv -- turbo run db:migrate",
1414
"db:studio": "dotenv -- turbo run db:studio",
1515
"email:dev": "bun run --cwd packages/email dev --port 3001",
16+
"test": "dotenv -- turbo run test:run",
17+
"test:watch": "dotenv -- turbo run test",
1618

1719
"// Parallel execution scripts": "",
1820
"dx": "dotenv -- turbo run dev db:studio email:dev jobs:dev --parallel",

packages/queue/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Captable
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)