Skip to content

Instantly share code, notes, and snippets.

View Unitech's full-sized avatar

Alexandre Strzelewicz Unitech

View GitHub Profile
@Unitech
Unitech / gist:10624965
Created April 14, 2014 07:43
../fsevents.cc:10:43: fatal error: CoreFoundation/CoreFoundation.h: No such file or directory
make: Leaving directory `/home/node/.nvm/v0.11.11/lib/node_modules/pm2/node_modules/usage/build'
npm http 200 https://registry.npmjs.org/configurable/-/configurable-0.0.1.tgz
npm http 200 https://registry.npmjs.org/keypress/-/keypress-0.1.0.tgz
> [email protected] install /home/node/.nvm/v0.11.11/lib/node_modules/pm2/node_modules/chokidar/node_modules/fsevents
> node-gyp rebuild
make: Entering directory `/home/node/.nvm/v0.11.11/lib/node_modules/pm2/node_modules/chokidar/node_modules/fsevents/build'
CXX(target) Release/obj.target/fse/fsevents.o
../fsevents.cc:10:43: fatal error: CoreFoundation/CoreFoundation.h: No such file or directory
------------> ✔ process should have been restarted
Killing pm2...
Process server-watch stopped
All processes stopped
PM2 stopped
{ online: true, success: true, pid: 10455, pm2_version: '0.8.2' }
PM2 Process launched
┌──────────────┬────┬─────────┬───────┬────────┬──────┬───────────┬────────┬─────────────┬───────────┐
│ App name │ id │ mode │ PID │ status │ port │ restarted │ uptime │ memory │ watching │
├──────────────┼────┼─────────┼───────┼────────┼──────┼───────────┼────────┼─────────────┼───────────┤
1z [tknew:~/Unitech/pm2-software/pm2] master(+18/-17)+* ± ./bin/pm2 start test/fixtures/server-watch.bak.js --watch
{ online: true, success: true, pid: 26944, pm2_version: '0.8.2' }
PM2 Process launched
┌──────────────────┬────┬─────────┬───────┬────────┬──────┬───────────┬────────┬─────────────┬───────────┐
│ App name │ id │ mode │ PID │ status │ port │ restarted │ uptime │ memory │ watching │
├──────────────────┼────┼─────────┼───────┼────────┼──────┼───────────┼────────┼─────────────┼───────────┤
│ server-watch.bak │ 0 │ cluster │ 26954 │ online │ │ 0 │ 0s │ 11.680 MB │ activated │
└──────────────────┴────┴─────────┴───────┴────────┴──────┴───────────┴────────┴─────────────┴───────────┘
Use `pm2 desc[ribe] <id>` to get more details
1z [tknew:~/Unitech/pm2-software/pm2] master(+18/-17)+* ± ./bin/pm2 restart 0
@Unitech
Unitech / gist:cb39287b9f083f156bd2
Created May 12, 2014 13:47
Simple POST nodejs
obj.post = function(url, data, cb) {
var http = require('http');
var dt = JSON.stringify(data);
var options = {
host: url,
path: '/new',
port: 3000,
method: 'POST',
function createParser () {
var parser = new Transform();
parser._transform = function(data, encoding, done) {
console.log(data);
this.push(data);
done();
};
return parser;
}
var sinon = require('sinon')
, EventEmitter = require('events').EventEmitter;
describe('EventEmitter', function(){
describe('#emit()', function(){
it('should invoke the callback', function(){
var spy = sinon.spy()
, emitter = new EventEmitter;
emitter.on('foo', spy);
@Unitech
Unitech / gist:fd9561765bd3bc0d61d1
Created May 16, 2014 12:53
Wrapping functions
function before(fn) {
var ret = null;
return function() {
try {
ret = fn.apply(this, arguments);
} catch(e) {
console.error(e);
}
return ret;
};
var assert = require('assert');
function Plan(count, done) {
this.done = done;
this.count = count;
}
Plan.prototype.ok = function(expression) {
assert(expression);

Clustering: The basics

The trick? pass the file descriptor from a parent process and have the server.listen reuse that descriptor. So multiprocess in their own memory space (but with ENV shared usually)

It does not balance, it leaves it to the kernel.

In the last nodejs > 0.8 there is a cluster module (functional although marked experimental)

/*
* Returns a function that is the composition of a list of functions,
* each consuming the return value of the function that follows.
*/
var slice = Array.prototype.slice;
var compose = function() {
var funcs = slice.call(arguments);
return function() {
var args = slice.call(arguments);
for (var i=funcs.length-1; i >= 0; i--) {