Skip to content

Commit

Permalink
some extra checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsangazar committed May 4, 2024
1 parent 9ba1082 commit 101bed5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/routes/mail-scheduler/config/flowMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type IflowMap = {
const flowMap = {
testEmail: {
when: {
seconds: 5,
hours: 2,
},
subject: "Welcome To Gazar.dev",
body: "This is a test Email sent from Gazar.dev based on your request",
Expand Down
24 changes: 22 additions & 2 deletions src/routes/mail-scheduler/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,28 @@ const postEvent = async (req, res) => {
});
}

if (req.mailSchedulerFlowManager)
req.mailSchedulerFlowManager.emit(req.body.eventName, req.body.userEmail);
if (req.mailSchedulerFlowManager) {
if (req.mailSchedulerFlowManager.taskQueueServiceObj.getLength() < 20) {
if (
!req.mailSchedulerFlowManager.taskQueueServiceObj.search(
req.body.userEmail
)
) {
req.mailSchedulerFlowManager.emit(
req.body.eventName,
req.body.userEmail
);
} else {
res.status(400).json({
message: "User already in queue",
});
}
} else {
res.status(400).json({
message: "System is overloaded with tasks, you can try later",
});
}
}

res.status(200).json({
message: "event emitted",
Expand Down
11 changes: 10 additions & 1 deletion src/routes/mail-scheduler/services/TaskQueueService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
interface Task {
executionTime: number;
data: any;
data: {
userEmail: string;
retry?: number;
};
}

class TaskHeapService {
Expand Down Expand Up @@ -75,6 +78,12 @@ class TaskHeapService {
return min;
}

search(userEmail) {
return (
this.heap.findIndex((item) => item.data.userEmail === userEmail) !== -1
);
}

getLength() {
return this.heap.length;
}
Expand Down

0 comments on commit 101bed5

Please sign in to comment.