Skip to content
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

feat: add queue #24

Draft
wants to merge 1 commit into
base: 1-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/controllers/SMTPController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
} from "kuzzle";

import { SMTPClient } from "../messenger-clients";
import TaskQueue from "../helper/TaskQueue";

export class SMTPController {
private context: PluginContext;
private config: JSONObject;

private queue: TaskQueue;
private smtpClient: SMTPClient;

definition: ControllerDefinition;
Expand All @@ -28,6 +29,7 @@ export class SMTPController {
this.config = config;
this.context = context;
this.smtpClient = smtpClient;
this.queue = new TaskQueue();

this.definition = {
actions: {
Expand Down Expand Up @@ -62,10 +64,12 @@ export class SMTPController {
const from = fromEmail.length === 0 ? null : fromEmail;
const attachments = attachmentsEmail.length === 0 ? null : attachmentsEmail;

await this.smtpClient.sendEmail(account, to, subject, html, {
attachments,
from,
});
this.queue.add(() =>
this.smtpClient.sendEmail(account, to, subject, html, {
attachments,
from,
})
);
}

async addAccount(request: KuzzleRequest) {
Expand Down
26 changes: 26 additions & 0 deletions lib/helper/TaskQueue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default class TaskQueue {
private tasks: (() => Promise<void>)[] = [];
private busy = false;

async add(task: () => Promise<void>) {
this.tasks.push(task);
this.process();
}

private async process() {
if (this.busy || this.tasks.length === 0) {
return;
}

this.busy = true;
const task = this.tasks.shift();

if (task) {
console.log("Processing task==============================");
await task();
}
this.busy = false;

this.process();
}
}
Loading