-
Notifications
You must be signed in to change notification settings - Fork 0
feat(auth+queue+jobs): add email verification job, split queue roles, fix query keys and a lot of small fixes #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { | ||
| SEND_VERIFICATION_EMAIL, | ||
| type SendVerificationEmailPayload, | ||
| } from "@/features/jobs/definitions/email.job"; | ||
| import { getQueueClient } from "@/lib/server/queue"; | ||
|
|
||
| export async function enqueueSendVerificationEmail( | ||
| payload: SendVerificationEmailPayload | ||
| ) { | ||
| const client = await getQueueClient(); | ||
|
|
||
| await client.send(SEND_VERIFICATION_EMAIL, payload, { | ||
| priority: 1, // Higher priority for verification emails | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| export default function TodoSkeleton() { | ||
| return ( | ||
| <div className="animate-pulse space-y-8 pt-4"> | ||
| <div className="h-10 w-1/3 rounded bg-muted" /> | ||
| <div className="space-y-4"> | ||
| {[1, 2, 3].map((i) => ( | ||
| <div className="h-16 rounded bg-muted" key={i} /> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { UseSend } from "usesend-js"; | ||
| import { env } from "@/lib/server/env"; | ||
|
|
||
| export const useSend = new UseSend(env.USESEND_API_KEY); | ||
| export const useSend = new UseSend(env.USESEND_API_KEY, env.USESEND_BASE_URL); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,78 @@ | ||
| import { PgBoss } from "pg-boss"; | ||
| import { registerJobs } from "@/features/jobs/register"; | ||
|
|
||
| let boss: PgBoss | null = null; | ||
| let producerClient: PgBoss | null = null; | ||
| let workerClient: PgBoss | null = null; | ||
|
|
||
| async function createQueue(databaseUrl: string) { | ||
| boss = new PgBoss(databaseUrl); | ||
| function getDatabaseUrl(): string { | ||
| if (!process.env.DATABASE_URL) { | ||
| throw new Error("DATABASE_URL is not set"); | ||
| } | ||
| return process.env.DATABASE_URL; | ||
| } | ||
|
|
||
| boss.on("error", (error) => { | ||
| // Wire up to your error tracking system here | ||
| console.error(error); | ||
| /** | ||
| * Get a queue client for enqueueing jobs (producer mode). | ||
| * This is lightweight - it only connects to pg-boss without registering handlers. | ||
| * Safe to use in the main app process. | ||
| */ | ||
| export async function getQueueClient(): Promise<PgBoss> { | ||
| if (producerClient) { | ||
| return producerClient; | ||
| } | ||
|
|
||
| producerClient = new PgBoss(getDatabaseUrl()); | ||
|
|
||
| producerClient.on("error", (error) => { | ||
| console.error("[pg-boss] Producer client error:", error); | ||
| }); | ||
|
|
||
| await boss.start(); | ||
| await producerClient.start(); | ||
|
|
||
| await registerJobs(boss); | ||
| console.log("[pg-boss] Producer client connected"); | ||
| return producerClient; | ||
| } | ||
|
|
||
| console.log("[pg-boss] Queue started"); | ||
| return boss; | ||
| /** | ||
| * Initialize the queue worker (worker mode). | ||
| * This connects to pg-boss AND registers all job handlers. | ||
| * Should only be called from the dedicated worker process. | ||
| */ | ||
| export async function initQueueWorker(): Promise<PgBoss> { | ||
| if (workerClient) { | ||
| return workerClient; | ||
| } | ||
|
|
||
| workerClient = new PgBoss(getDatabaseUrl()); | ||
|
|
||
| workerClient.on("error", (error) => { | ||
| console.error("[pg-boss] Worker error:", error); | ||
| }); | ||
|
|
||
| await workerClient.start(); | ||
| await registerJobs(workerClient); | ||
|
|
||
| console.log("[pg-boss] Worker initialized with job handlers"); | ||
| return workerClient; | ||
| } | ||
|
|
||
| export async function initQueue() { | ||
| if (!process.env.DATABASE_URL) { | ||
| throw new Error("DATABASE_URL is not set"); | ||
| /** | ||
| * Gracefully shutdown queue connections. | ||
| * Call this on process termination. | ||
| */ | ||
| export async function shutdownQueue(): Promise<void> { | ||
| const shutdownPromises: Promise<void>[] = []; | ||
|
|
||
| if (producerClient) { | ||
| shutdownPromises.push(producerClient.stop()); | ||
| producerClient = null; | ||
| } | ||
|
|
||
| if (!boss) { | ||
| boss = await createQueue(process.env.DATABASE_URL as string); | ||
| if (workerClient) { | ||
| shutdownPromises.push(workerClient.stop()); | ||
| workerClient = null; | ||
| } | ||
|
|
||
| return boss; | ||
| await Promise.all(shutdownPromises); | ||
| console.log("[pg-boss] Queue connections closed"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,24 @@ | ||
| import { initQueue } from "@/lib/server/queue"; | ||
| import { initQueueWorker, shutdownQueue } from "@/lib/server/queue"; | ||
|
|
||
| export async function startQueueWorker() { | ||
| await initQueue(); | ||
| console.log("[pg-boss] Queue worker started"); | ||
| async function startWorker() { | ||
| await initQueueWorker(); | ||
| console.log("[pg-boss] Queue worker started and listening for jobs"); | ||
| } | ||
|
|
||
| startQueueWorker().catch((error) => { | ||
| console.error("[pg-boss] Error starting queue worker", error); | ||
| // Graceful shutdown handlers | ||
| process.on("SIGTERM", async () => { | ||
| console.log("[pg-boss] Received SIGTERM, shutting down..."); | ||
| await shutdownQueue(); | ||
| process.exit(0); | ||
| }); | ||
|
|
||
| process.on("SIGINT", async () => { | ||
| console.log("[pg-boss] Received SIGINT, shutting down..."); | ||
| await shutdownQueue(); | ||
| process.exit(0); | ||
| }); | ||
|
|
||
| startWorker().catch((error) => { | ||
| console.error("[pg-boss] Error starting queue worker:", error); | ||
| process.exit(1); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Wrapping error loses stack trace - just rethrow the original error
Prompt To Fix With AI