Skip to content

Instantly share code, notes, and snippets.

View WaleedAshraf's full-sized avatar
:octocat:
Focusing

Waleed Ashraf WaleedAshraf

:octocat:
Focusing
View GitHub Profile
@WaleedAshraf
WaleedAshraf / workers-mq.js
Last active July 5, 2018 02:57
Switching from cluster module to PM2 & RabbitMQ
// exports RabbitMQ connection
const MQ = require('./rabbitmq-config');
global.smsWorker = {
send: function (message) {
// publish message on sms exchange
return MQ.publish('sms', message);
}
};
var pm2Config = {
"apps": [
{
"name": "app",
"script": "app.js",
"exec_mode": "cluster_mode",
"instances": "max"
},
{
"name": "smsWorker",
@WaleedAshraf
WaleedAshraf / smsWorker.js
Created March 23, 2018 17:26
Switching from cluster module to PM2 & RabbitMQ
process.on('message', function(data) {
sendSMS(data); // use twilio or something to send sms
});
@WaleedAshraf
WaleedAshraf / cluster-module.js
Last active July 5, 2018 02:57
Switching from cluster module to PM2 & RabbitMQ
var cluster = require('cluster');
const numCPUs = require('os').cpus().length;
module.exports.create = function (options, callback) {
if (cluster.isMaster) {
// fork child process for notif/sms/email worker
global.smsWorker = require('child_process').fork('./smsWorker');
global.emailWorker = require('child_process').fork('./emailWorker');
global.notifiWorker = require('child_process').fork('./notifWorker');
@WaleedAshraf
WaleedAshraf / cluster-module.js
Last active March 25, 2018 13:21
Switching from cluster module to PM2 & RabbitMQ
var cluster = require('cluster');
const numCPUs = require('os').cpus().length;
module.exports.create = function(options, callback){
if (cluster.isMaster) {
// fork child process for notif/sms/email worker
global.smsWorker = require('child_process').fork('./smsWorker');
global.emailWorker = require('child_process').fork('./emailWorker');
global.notifiWorker = require('child_process').fork('./notifWorker');
@WaleedAshraf
WaleedAshraf / cluster-module.js
Last active October 31, 2018 02:50
Switching from cluster module to PM2 & RabbitMQ
var cluster = require('cluster');
const numCPUs = require('os').cpus().length;
module.exports.create = function(options, callback){
if (cluster.isMaster) {
// fork child process for notif/sms/email worker
global.smsWorker = require('child_process').fork('./smsWorker');
global.emailWorker = require('child_process').fork('./emailWorker');
global.notifiWorker = require('child_process').fork('./notifWorker');
@WaleedAshraf
WaleedAshraf / mock-session-4.js
Last active March 18, 2018 19:06
Mock Express Session
exports.submit = function(req, res) {
if(req.session.count && req.session.count > 0) { // true
//do something
res.sendStatus(200);
}
}
@WaleedAshraf
WaleedAshraf / mock-session-3.js
Last active March 18, 2018 19:07
Mock Express Session
// name = "my-session" -> base64 value of required object (cookie)
// name.sig = "my-session.sig" -> signed value of cookie using Keygrip
let cookie = Buffer.from(JSON.stringify({"count":2})).toString('base64'); // base64 converted value of cookie
let kg = Keygrip(['testKey']) // same key as I'm using in my app
let hash = kg.sign('my-session=' + cookie);
await request(app).get('api/image/submit').set("Accept", "text/html")
.set('cookie', ['my-session=' + cookie + '; ' + 'my-session.sig=' + hash + ';'])
@WaleedAshraf
WaleedAshraf / mock-session-2.js
Last active March 18, 2018 19:07
Mock Express Session
exports.submit = function(req, res) {
if (req.session.count && req.session.count > 0) { // get count from req.session
// perform further tasks
res.sendStatus(200);
} else {
res.sendStatus(400);
}
}
@WaleedAshraf
WaleedAshraf / mock-session-1.js
Last active March 18, 2018 19:07
Mock Express Session
exports.create = function(req, res) {
// upload image to s3
var photos = PhotosTable.create(req.files.names); // add entries in database
req.session.count = photos.length; // set count in req.session
res.redirect(`api/image/submit`);
}