Skip to content
This repository has been archived by the owner on Feb 4, 2021. It is now read-only.

Commit

Permalink
improved: moved some code around to enable multiple instances of stack
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed May 8, 2019
1 parent 1546321 commit bb306d8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 59 deletions.
28 changes: 22 additions & 6 deletions src/lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const Queue = require('better-queue');
const crypto = require('crypto');
const Sms = require('@lib/Sms');
const smsStack = require('@lib/stack');
const stack = require('@lib/stack');

const takeNextN = function(first, groupName) {
return function(n, cb) {
Expand Down Expand Up @@ -143,22 +143,38 @@ const getStore = function(groupName) {

module.exports = {
emailQueue: localLogger => {
let emailStack = stack().init(
1000,
() => {
//Tick callback
},
messages => {
messages.forEach(message => {
sendmail() //TODO: here
.then((response, body) => {
//localLogger.debug(response.statusCode, response.body);
})
.catch(err => {
localLogger.error(err);
});
});
}
);
return new Queue(
(input, cb) => {
//global.logger = localLogger;
//TODO: email logic
//TODO: implement emails !
emailStack.addToStack(input);
cb(null, {});
},
{
batchSize: 1,
concurrent: 1,
filo: true,
store: getStore('sms'),
store: getStore('email'),
}
);
},
smsQueue: localLogger => {
smsStack.init(
let smsStack = stack().init(
1000,
() => {
//Tick callback
Expand Down
101 changes: 48 additions & 53 deletions src/lib/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

const cron = require('node-cron');

var task = null;

var tasks = [];

var _maxLength = 0;

/**
* Chunk a string
* @param {String} str The string
* @param {Number} size The size of a chunk
* @returns {String[]} The array of chunks
*/
function chunkSubstr(str, size) {
const numChunks = Math.ceil(str.length / size);
const chunks = new Array(numChunks);
Expand All @@ -19,54 +19,49 @@ function chunkSubstr(str, size) {
return chunks;
}


const emptyAndSendStack = function(emptyQueue) {
emptyQueue(chunkSubstr(tasks.join("\n--\n"), _maxLength));
tasks = [];
};

const init = function(maxLength, cbTickSuccess, cbEmptyQueue) {
_maxLength = maxLength;
/**
* @callback emptyQueueCallback
* @param {String[]} chunks The chunks to send
*/

/**
*
* @param {String[]} tasks The tasks
* @param {Number} maxLength The max length
* @param {emptyQueueCallback} emptyQueue
*/
const emptyAndSendStack = function(tasks, maxLength, emptyQueue) {
emptyQueue(chunkSubstr(tasks.join('\n--\n'), maxLength));
tasks = [];
task = cron.schedule('*/30 * * * * *', () => {
emptyAndSendStack(cbEmptyQueue);
cbTickSuccess();
});
};

const addToStack = function(message) {
tasks.push(message);
};

const stop = function() {
/* istanbul ignore next */
if (task) {
task.stop();
}
};

const getMaxLength = function() {
return _maxLength;
};

const getTasksCount = function() {
return tasks.length;
};

const getTasks = function() {
return tasks.slice(0);
};

const getCronTask = function() {
return task;
};

module.exports = {
addToStack: addToStack,
init: init,
stop: stop,
getCronTask: getCronTask,
getMaxLength: getMaxLength,
getTasksCount: getTasksCount,
getTasks: getTasks,
module.exports = () => {
var task = null;

var tasks = [];

var _maxLength = 0;

return {
chunkSubstr: chunkSubstr,
addToStack: message => tasks.push(message),
init: (maxLength, cbTickSuccess, cbEmptyQueue) => {
_maxLength = maxLength;
tasks = [];
task = cron.schedule('*/30 * * * * *', () => {
emptyAndSendStack(tasks, maxLength, cbEmptyQueue);
cbTickSuccess();
});
},
stop: () => {
/* istanbul ignore next */
if (task) {
task.stop();
}
},
getCronTask: () => task,
getMaxLength: () => _maxLength,
getTasksCount: () => tasks.length,
getTasks: () => tasks.slice(0),
};
};

0 comments on commit bb306d8

Please sign in to comment.