mirror of
https://github.com/wassname/talk.git
synced 2026-08-02 13:10:23 +08:00
Refined task singleton to factory
This commit is contained in:
@@ -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
@@ -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
@@ -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;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user