Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test/unit/coverage/**
test/unit/*.js
test/e2e/*.js
18 changes: 18 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
parserOptions: {
sourceType: 'module',
},
env: {
node: true,
},
extends: ['standard', 'prettier'],
globals: {},
plugins: [],
rules: {
'linebreak-style': 0,
'no-console': process.env.NODE_ENV === 'production' ? 2 : 0,
'no-unused-vars': process.env.NODE_ENV === 'production' ? 2 : 0,
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
},
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

/** NPM **/
/node_modules
npm-debug.log
npm-debug.log
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": false,
"singleQuote": true,
"trailingComma": "all"
}
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "stable"
- '0.10'
- '0.12'
- 'stable'
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ npm install fork-pool

```javascript
// Parent process
var Pool = new pool(__dirname + '/child.js', null, null, {});
Pool.enqueue('hello', function (err, obj) {
console.dir(obj); // FTW!
});
var Pool = new pool(__dirname + '/child.js', null, null, {})
Pool.enqueue('hello', function(err, obj) {
console.dir(obj) // FTW!
})
```

```javascript
// Child process
process.on('message', function (message) {
process.send('world');
});
process.on('message', function(message) {
process.send('world')
})
```

## Parameters

- path: Child process path (generally, you will want to prefix with "__dirname")
- args: Child process arguments
- options: Child process options
- settings: Pool settings
- name (Optional, Defaults to "fork-pool")
- size (Optional, Defaults to # of CPUs)
- log (Optional, Defaults to false)
- timeout (Optional, Defaults to 30000ms)
- path: Child process path (generally, you will want to prefix with "\_\_dirname")
- args: Child process arguments
- options: Child process options
- settings: Pool settings
- name (Optional, Defaults to "fork-pool")
- size (Optional, Defaults to # of CPUs)
- log (Optional, Defaults to false)
- timeout (Optional, Defaults to 30000ms)

## Testing

```bash
npm test
```
```
88 changes: 44 additions & 44 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,78 +8,78 @@
/**
* Dependencies
*/
var _ = require('lodash');
var childProcess = require('child_process');
var generic = require('generic-pool');
var _ = require('lodash')
var childProcess = require('child_process')
var generic = require('generic-pool')

/**
* Constructor
*/
function Pool (path, args, options, settings) {
function Pool(path, args, options, settings) {
_.defaults(settings, {
name: 'fork-pool',
size: require('os').cpus().length,
log: false,
timeout: 30000,
debug: false,
debugPort: process.debugPort // Default debugging port for the main process. Skip from here.
});
name: 'fork-pool',
size: require('os').cpus().length,
log: false,
timeout: 30000,
debug: false,
debugPort: process.debugPort, // Default debugging port for the main process. Skip from here.
})

//

this.pool = generic.Pool({
this.pool = generic.Pool({
settings: settings,
name: settings.name,
create: function (callback) {
var debugArgIdx = process.execArgv.indexOf('--debug');
create: function(callback) {
var debugArgIdx = process.execArgv.indexOf('--debug')
if (debugArgIdx !== -1) {
// Remove debugging from process before forking
process.execArgv.splice(debugArgIdx, 1);
process.execArgv.splice(debugArgIdx, 1)
}
if (this.settings.debug) {
// Optionally set an unused port number if you want to debug the children.
// This only works if idle processes stay alive (long timeout), or you will run out of ports eventually.
process.execArgv.push('--debug=' + (++this.settings.debugPort));
process.execArgv.push('--debug=' + ++this.settings.debugPort)
}
var childNode = childProcess.fork(path, args, options);
callback(null, childNode);
var childNode = childProcess.fork(path, args, options)
callback(null, childNode)
},
destroy: function (client) {
client.kill();
destroy: function(client) {
client.kill()
},
max: settings.size,
min: settings.size - 1,
idleTimeoutMillis: settings.timeout,
log: settings.log
});
};
log: settings.log,
})
}

Pool.prototype.enqueue = function (data, callback) {
var instance = this.pool;
instance.acquire(function (err, client) {
Pool.prototype.enqueue = function(data, callback) {
var instance = this.pool
instance.acquire(function(err, client) {
if (err) {
callback(err);
callback(err)
} else {
client.send(data);
client.once('message', function (message) {
client.send(data)
client.once('message', function(message) {
var a = {
pid: client.pid,
stdout: message
};
pid: client.pid,
stdout: message,
}

instance.release(client);
callback(null, a);
});
instance.release(client)
callback(null, a)
})
}
});
};
})
}

Pool.prototype.drain = function (callback) {
var instance = this.pool;
Pool.prototype.drain = function(callback) {
var instance = this.pool
instance.drain(function() {
instance.destroyAllNow();
callback(null);
});
};
instance.destroyAllNow()
callback(null)
})
}

module.exports = Pool;
module.exports = Pool
Loading