Refined task singleton to factory

This commit is contained in:
Wyatt Johnson
2017-07-25 12:53:52 +10:00
parent 07114aed1b
commit 0c8def722b
3 changed files with 20 additions and 17 deletions
+16
View File
@@ -138,3 +138,19 @@ if (process.env.NODE_ENV === 'test') {
} else {
module.exports.Task = Task;
}
module.exports.createTaskFactory = () => {
let taskInstance = null;
return (options) => {
if (taskInstance) {
return taskInstance;
}
options = Object.assign({}, options);
taskInstance = new module.exports.Task(options);
return taskInstance;
};
};
+2 -8
View File
@@ -1,6 +1,7 @@
const debug = require('debug')('talk:services:mailer');
const nodemailer = require('nodemailer');
const kue = require('./kue');
const taskFactory = kue.createTaskFactory();
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
@@ -69,7 +70,6 @@ if (SMTP_PORT) {
}
const defaultTransporter = nodemailer.createTransport(options);
let taskInstance = null;
const mailer = module.exports = {
@@ -77,15 +77,9 @@ const mailer = module.exports = {
* Create the new Task kue.
*/
get task() {
if (taskInstance) {
return taskInstance;
}
taskInstance = new kue.Task({
return taskFactory({
name: 'mailer'
});
return taskInstance;
},
sendSimple({template, locals, to, subject}) {
+2 -9
View File
@@ -1,12 +1,11 @@
const kue = require('./kue');
const taskFactory = kue.createTaskFactory();
const debug = require('debug')('talk:services:scraper');
const AssetModel = require('../models/asset');
const AssetsService = require('./assets');
const metascraper = require('metascraper');
let taskInstance = null;
/**
* Exposes a service object to allow operations to execute against the scraper.
* @type {Object}
@@ -17,15 +16,9 @@ const scraper = {
* Create the new Task kue singleton.
*/
get task() {
if (taskInstance) {
return taskInstance;
}
taskInstance = new kue.Task({
return taskFactory({
name: 'scraper'
});
return taskInstance;
},
/**