Skip to content

Commit c685ebe

Browse files
authored
Merge pull request #137 from longui-io/add-env-useps
Add env vars that set the pidusage options
2 parents c7e3f86 + 94ede26 commit c685ebe

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,18 @@ Get pid informations.
156156
| Param | Type | Description |
157157
| --- | --- | --- |
158158
| pids | <code>Number</code> \| <code>Array.&lt;Number&gt;</code> \| <code>String</code> \| <code>Array.&lt;String&gt;</code> | A pid or a list of pids. |
159+
| [options] | <code>object</code> | Options object. See the table below. |
159160
| [callback] | <code>function</code> | Called when the statistics are ready. If not provided a promise is returned instead. |
160161

162+
### options
163+
164+
Setting the options programatically will override environment variables
165+
166+
| Param | Type | Environment variable | Default | Description |
167+
| --- | --- | --- | --- | --- |
168+
| [usePs] | <code>boolean</code> | `PIDUSAGE_USE_PS`| `false` | When true uses `ps` instead of proc files to fetch process information |
169+
| [maxage] | <code>number</code> | `PIDUSAGE_MAXAGE`| `60000` | Max age of a process on history. |
170+
161171
### pidusage.clear()
162172

163173
If needed this function can be used to delete all in-memory metrics and clear the event loop. This is not necessary before exiting as the interval we're registring does not hold up the event loop.

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ function pidusage (pids, options, callback) {
2121
options = {}
2222
}
2323

24+
options = Object.assign({
25+
usePs: process.env.PIDUSAGE_USE_PS,
26+
maxage: process.env.PIDUSAGE_MAXAGE
27+
}, options)
28+
2429
if (typeof callback === 'function') {
2530
stats(pids, options, callback)
2631
return

test/ps.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,22 @@ test('should parse ps output on *nix', async t => {
153153
// mockery.deregisterMock('child_process')
154154
// mockery.deregisterMock('os')
155155
})
156+
157+
test('should be able to set usePs from env var', async t => {
158+
let usePsFromStats
159+
160+
mockery.registerMock('./lib/stats', (_, options) => {
161+
usePsFromStats = !!options.usePs
162+
})
163+
164+
const beforeValue = process.env.PIDUSAGE_USE_PS
165+
process.env.PIDUSAGE_USE_PS = '1'
166+
167+
const pidusage = require('../')
168+
pidusage(1, () => {})
169+
170+
t.is(usePsFromStats, true)
171+
172+
process.env.PIDUSAGE_USE_PS = beforeValue
173+
mockery.deregisterMock('./lib/stats')
174+
})

0 commit comments

Comments
 (0)