Skip to content

Commit

Permalink
feat: Add environment variables support for workers (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX authored and fengmk2 committed Nov 18, 2016
1 parent 73cbd2d commit 4021033
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ process.on('uncaughtException', function (err) {
- **limit**: limit refork times within the `duration`, default is `60`
- **duration**: default is `60000`, one minute (so, the `refork times` < `limit / duration`)
- **autoCoverage**: auto fork with istanbul when `running_under_istanbul` env set, default is `false`
- **env**: attach some environment variable key-value pairs to the worker / slave process, default to an empty object.

## License

Expand Down
3 changes: 3 additions & 0 deletions fixtures/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ cfork({
count: 4,
duration: 60000,
autoCoverage: true,
env: {
CFORK_ENV_TEST: '😂',
},
})
.on('fork', function (worker) {
console.warn('[%s] [worker:%d] new worker start', Date(), worker.process.pid);
Expand Down
3 changes: 3 additions & 0 deletions fixtures/slave.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ var app = http.createServer(function (req, res) {
if (req.url === '/exit') {
process.exit(0);
}
if (req.url === '/env') {
return res.end(process.env.CFORK_ENV_TEST);
}
res.end(req.method + ' ' + req.url);
}).listen(port);

Expand Down
3 changes: 3 additions & 0 deletions fixtures/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var app = http.createServer(function (req, res) {
if (req.url === '/exit') {
process.exit(0);
}
if (req.url === '/env') {
return res.end(process.env.CFORK_ENV_TEST);
}
res.end(req.method + ' ' + req.url);
}).listen(port);

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function fork(options) {
var limit = options.limit || 60;
var duration = options.duration || 60000; // 1 min
var reforks = [];
var attachedEnv = options.env || {};
var newWorker;

if (options.exec) {
Expand Down Expand Up @@ -241,6 +242,6 @@ function fork(options) {
cluster.settings = settings;
cluster.setupMaster();
}
return cluster.fork();
return cluster.fork(attachedEnv);
}
}
21 changes: 20 additions & 1 deletion test/cfork.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,33 @@ describe('cfork.test.js', function () {
});
});

it('should get correct env value', function (done) {
urllib.request('http://localhost:1984/env', function (err, body, resp) {
should.ifError(err);
body.toString().should.equal('😂');
resp.statusCode.should.equal(200);
urllib.request('http://localhost:1985/env', function (err, body, resp) {
should.ifError(err);
body.toString().should.equal('😂');
resp.statusCode.should.equal(200);
done();
});
});
});

it('should slave error and refork', function (done) {
urllib.request('http://localhost:1985/error', function (err) {
should.exist(err);
urllib.request('http://localhost:1985/', function (err, body, res) {
should.not.exist(err);
body.toString().should.equal('GET /');
res.statusCode.should.equal(200);
done();
urllib.request('http://localhost:1985/env', function (err, body, resp) {
should.ifError(err);
body.toString().should.equal('😂');
resp.statusCode.should.equal(200);
done();
});
});
});
});
Expand Down

0 comments on commit 4021033

Please sign in to comment.