refactor: adjust code after updated dependencies

Closes #39
This commit is contained in:
Jovan Krunić
2019-06-05 13:58:26 +02:00
committed by Rainer Killinger
parent 42c7350c36
commit 8b457c9911
24 changed files with 574 additions and 343 deletions

View File

@@ -13,11 +13,16 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SMTP} from '@openstapps/logger/lib/SMTP';
import {Transport, TransportWithVerification} from '@openstapps/logger/lib/Transport';
import {SMTP} from '@openstapps/logger/lib/smtp';
import {Transport, VerifiableTransport} from '@openstapps/logger/lib/transport';
export function isTransportWithVerification(instance: Transport): instance is TransportWithVerification {
return typeof (instance as TransportWithVerification).verify === 'function';
/**
* Provides information if a transport is a verifiable transport
*
* @param instance A transport that needs to be checked
*/
export function isTransportWithVerification(instance: Transport): instance is VerifiableTransport {
return typeof (instance as VerifiableTransport).verify === 'function';
}
/**
@@ -26,18 +31,32 @@ export function isTransportWithVerification(instance: Transport): instance is Tr
* In the future this may support more than loading SMTP as a transport.
*/
export class BackendTransport {
/**
* One (and only one) instance of the backend transport
*/
private static _instance: BackendTransport;
private waitingForVerification: boolean;
/**
* Stores information if transport is in state of waiting for the verification
*/
private readonly waitingForVerification: boolean;
/**
* A (SMTP) transport which includes settings for sending mails
*/
protected transport: SMTP | undefined;
/**
* Provides an instance of a transport
*/
public static getTransportInstance(): SMTP | undefined {
if (this._instance) {
return this._instance.transport;
if (typeof BackendTransport._instance !== 'undefined') {
return BackendTransport._instance.transport;
}
this._instance = new this();
return this._instance.transport;
BackendTransport._instance = new BackendTransport();
return BackendTransport._instance.transport;
}
private constructor() {
@@ -58,19 +77,24 @@ export class BackendTransport {
if (typeof this.transport !== 'undefined' && isTransportWithVerification(this.transport)) {
this.waitingForVerification = true;
this.transport.verify().then((message) => {
if (typeof message === 'string') {
// tslint:disable-next-line:no-console
console.log(message);
}
}).catch((err) => {
throw err;
});
this.transport.verify()
.then((message) => {
if (typeof message === 'string') {
// tslint:disable-next-line:no-console
console.log(message);
}
})
.catch((err) => {
throw err;
});
} else {
this.waitingForVerification = false;
}
}
/**
* Provides information if transport is in state of waiting for the verification
*/
public isWaitingForVerification(): boolean {
return this.waitingForVerification;
}

View File

@@ -14,15 +14,25 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.nse along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SMTP} from '@openstapps/logger/lib/SMTP';
import {Logger} from '@openstapps/logger';
import {SMTP} from '@openstapps/logger/lib/smtp';
import {MailOptions} from 'nodemailer/lib/sendmail-transport';
import * as Queue from 'promise-queue';
import {logger} from '../common';
/**
* A queue that can send mails in serial
*/
export class MailQueue {
/**
* Number of allowed verification attempts after which the initialization of transport fails
*/
static readonly MAX_VERIFICATION_ATTEMPTS = 5;
/**
* Number of milliseconds after which verification check should be repeated
*/
static readonly VERIFICATION_TIMEOUT = 5000;
/**
* A queue that saves mails, before the transport is ready. When
* the transport gets ready this mails are getting pushed in to
@@ -42,9 +52,9 @@ export class MailQueue {
/**
* Creates a mail queue
* @param transport
* @param transport Transport which is used for sending mails
*/
constructor(private transport: SMTP) {
constructor(private readonly transport: SMTP) {
this.queue = new Queue(1);
@@ -62,35 +72,36 @@ export class MailQueue {
*/
private checkForVerification() {
if (this.verificationCounter > 5) {
if (this.verificationCounter > MailQueue.MAX_VERIFICATION_ATTEMPTS) {
throw new Error('Failed to initialize the SMTP transport for the mail queue');
}
if (!this.transport.isVerified()) {
this.verificationCounter++;
setTimeout(() => {
logger.warn('Transport not verified yet. Trying to send mails here...');
setTimeout(async () => {
Logger.warn('Transport not verified yet. Trying to send mails here...');
this.checkForVerification();
}, 5000);
}, MailQueue.VERIFICATION_TIMEOUT);
} else {
logger.ok('Transport for mail queue was verified. We can send mails now');
Logger.ok('Transport for mail queue was verified. We can send mails now');
// if the transport finally was verified send all our mails from the dry queue
this.dryQueue.forEach((mail) => {
this.queue.add<string>(() => (this.transport as SMTP).sendMail(mail));
this.dryQueue.forEach(async (mail) => {
await this.queue.add<string>(() => (this.transport as SMTP).sendMail(mail));
});
}
}
/**
* Push a mail into the queue so it gets send when the queue is ready
* @param mail
*
* @param mail Information required for sending a mail
*/
public push(mail: MailOptions) {
public async push(mail: MailOptions) {
if (!this.transport.isVerified()) { // the transport has verification, but is not verified yet
// push to a dry queue which gets pushed to the real queue when the transport is verified
this.dryQueue.push(mail);
} else {
this.queue.add<string>(() => (this.transport as SMTP).sendMail(mail));
await this.queue.add<string>(() => (this.transport as SMTP).sendMail(mail));
}
}
}