-
Notifications
You must be signed in to change notification settings - Fork 3
(Feat) BullMQ Integration for Background Jobs #124
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
Show all changes
19 commits
Select commit
Hold shift + click to select a range
7e2b044
feat: add queue types for email jobs
MuhammedMagdyy 96f927d
refactor: update MAGIC_NUMBERS and add QUEUES and WORKERS constants
MuhammedMagdyy 210830a
feat: add email queue implementation
MuhammedMagdyy a8d6784
feat: implement email worker for handling email jobs
MuhammedMagdyy 6c28f70
feat: handling email verification and password reset jobs
MuhammedMagdyy c5ec0c1
refactor: enhance error handling and logging in email service methods
MuhammedMagdyy c2cfb45
refactor: replace email service calls with email job implementations …
MuhammedMagdyy aea1f51
chore: add start:worker script to run email worker
MuhammedMagdyy b135d4f
refactor: improve deployment script by adding email worker
MuhammedMagdyy 31cb9e0
chore: update ecosystem config to include email worker with proper sc…
MuhammedMagdyy 466d738
docs: mark integration with BullMQ for background job processing as c…
MuhammedMagdyy 5cbff71
refactor: replace magic numbers with constants
MuhammedMagdyy f2c0f27
refactor: update EmailJobName type to use WORKERS constant
MuhammedMagdyy d173f48
refactor: enhance error logging for email sending and job handling
MuhammedMagdyy 05f6e70
refactor: implement email hashing for job keys and enhance logging
MuhammedMagdyy e476503
refactor: improve graceful shutdown process for email worker
MuhammedMagdyy 309dc70
refactor: add HMAC secret to environment configuration and update has…
MuhammedMagdyy a436c2b
refactor: change job backoff strategy to exponential for better retry…
MuhammedMagdyy 5c3be69
refactor: replace logger warning with error throw for unknown job names
MuhammedMagdyy 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
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,76 @@ | ||
| import { JobsOptions } from 'bullmq'; | ||
| import { emailQueue } from '../queues'; | ||
| import { HashingService } from '../services'; | ||
| import { SendEmailVerificationJob, SendForgetPasswordJob } from '../types'; | ||
| import { logger, MAGIC_NUMBERS, WORKERS } from '../utils'; | ||
|
|
||
| export class EmailJob { | ||
| static async addVerificationEmailJob( | ||
| verificationJobDetails: SendEmailVerificationJob, | ||
| ) { | ||
| try { | ||
| const hashedEmail = this.hashEmail(verificationJobDetails.email); | ||
| const key = `verify-${hashedEmail}`; | ||
| const jobOptions = this.createJobOptions(key); | ||
| await emailQueue.add( | ||
| WORKERS.SEND_VERIFICATION_EMAIL, | ||
| { | ||
| email: verificationJobDetails.email, | ||
| name: verificationJobDetails.name, | ||
| token: verificationJobDetails.token, | ||
| }, | ||
| jobOptions, | ||
| ); | ||
|
|
||
| logger.info( | ||
| `Verification email job scheduled for ${verificationJobDetails.email} with key ${key}`, | ||
| ); | ||
| } catch (error) { | ||
| logger.error(`Failed to add verification email job: ${error}`); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| static async addForgetPasswordEmailJob( | ||
| forgetPasswordDetails: SendForgetPasswordJob, | ||
| ) { | ||
| try { | ||
| const hashedEmail = this.hashEmail(forgetPasswordDetails.email); | ||
| const key = `forget-${hashedEmail}`; | ||
| const jobOptions = this.createJobOptions(key); | ||
| await emailQueue.add( | ||
| WORKERS.SEND_FORGET_PASSWORD_EMAIL, | ||
| { | ||
| email: forgetPasswordDetails.email, | ||
| name: forgetPasswordDetails.name, | ||
| otp: forgetPasswordDetails.otp, | ||
| }, | ||
| jobOptions, | ||
| ); | ||
|
|
||
| logger.info( | ||
| `Forget password email job scheduled for ${forgetPasswordDetails.email} with key ${key}`, | ||
| ); | ||
| } catch (error) { | ||
| logger.error(`Failed to add forget password email job: ${error}`); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| private static createJobOptions(jobId: string): JobsOptions { | ||
| return { | ||
| jobId, | ||
| attempts: MAGIC_NUMBERS.MAX_NUMBER_OF_RETRIES, | ||
| backoff: { | ||
| type: 'exponential', | ||
| delay: MAGIC_NUMBERS.FIVE_SECONDS_IN_MILLISECONDS, | ||
| }, | ||
| removeOnComplete: MAGIC_NUMBERS.MAX_COUNT_FOR_REMOVE_ON_COMPLETE, | ||
| removeOnFail: MAGIC_NUMBERS.MAX_COUNT_FOR_REMOVE_ON_FAILURE, | ||
| }; | ||
| } | ||
|
|
||
| private static hashEmail(email: string): string { | ||
| return HashingService.generateHashWithHmac(email); | ||
| } | ||
| } | ||
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 @@ | ||
| export * from './email.job'; |
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,8 @@ | ||
| import { Queue } from 'bullmq'; | ||
| import { redisHost, redisPort } from '../config'; | ||
| import { EmailJobData } from '../types'; | ||
| import { QUEUES } from '../utils'; | ||
|
|
||
| export const emailQueue = new Queue<EmailJobData>(QUEUES.EMAIL_QUEUE, { | ||
| connection: { host: redisHost, port: Number(redisPort) }, | ||
|
MuhammedMagdyy marked this conversation as resolved.
|
||
| }); | ||
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 +1,2 @@ | ||
| export * from './competition.queue'; | ||
| export * from './email.queue'; |
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export * from './jwt'; | ||
| export * from './queue'; | ||
| export * from './status'; | ||
| export * from './statusCodes'; | ||
| export * from './user'; |
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,17 @@ | ||
| import { WORKERS } from '../utils'; | ||
|
|
||
| export type SendEmailVerificationJob = { | ||
| email: string; | ||
| name: string; | ||
| token: string; | ||
| }; | ||
|
|
||
| export type SendForgetPasswordJob = { | ||
| email: string; | ||
| name: string; | ||
| otp: string; | ||
| }; | ||
|
|
||
| export type EmailJobData = SendEmailVerificationJob | SendForgetPasswordJob; | ||
|
|
||
| export type EmailJobName = (typeof WORKERS)[keyof typeof WORKERS]; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.