-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support "processes" to handle extra procs
- Loading branch information
Showing
6 changed files
with
222 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright (c) 2015, Groupon, Inc. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* | ||
* Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* Neither the name of GROUPON nor the names of its contributors may be | ||
* used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/* | ||
Given configuration like: | ||
{ | ||
// ... | ||
processes: { | ||
memcached: { | ||
port: 11211, | ||
command: 'memcached', | ||
commandArgs: ['-u', 'memcached', '-d'], | ||
reuseExisting: true, | ||
}, | ||
// ... | ||
} | ||
} | ||
This will turn them into server configuration bits and start things up | ||
for you; skipping them if something's already listening on that port | ||
*/ | ||
|
||
const { isAvailable } = require('find-open-port'); | ||
const debug = require('debug')('testium-core:processes:extra'); | ||
|
||
/** | ||
* @typedef Server | ||
* @property {string} name | ||
* @property {() => SubprocessOpts | Promise<SubprocessOpts>} getOptions | ||
* | ||
* @typedef SubprocessOpts | ||
* @property {number} port | ||
* ...and a bunch more options; see subprocess/README.md | ||
* | ||
* @typedef {SubprocessOpts & { reuseExisting?: boolean }} ExtraProcOpts | ||
*/ | ||
|
||
/** @param {Record<string, ExtraProcOpts>} extraProcs */ | ||
async function checkExtraProcs(extraProcs) { | ||
/** @type {Server[]} */ | ||
const servers = []; | ||
|
||
/** @type {[ string, SubprocessOpts & { port: number } ]} */ | ||
const toCheck = []; | ||
|
||
for (const [name, opts] of Object.entries(extraProcs)) { | ||
const { reuseExisting = false, ...subPOpts } = opts; | ||
|
||
if (!reuseExisting) { | ||
servers.push({ name, getOptions: () => subPOpts }); | ||
continue; | ||
} | ||
|
||
if (!subPOpts.port) { | ||
throw new Error( | ||
`process.* with reuseExisting=true must include static port` | ||
); | ||
} | ||
|
||
toCheck.push([name, subPOpts]); | ||
} | ||
|
||
if (toCheck.length > 0) { | ||
await Promise.all( | ||
toCheck.map(async ([name, opts]) => { | ||
const { port } = opts; | ||
if (await isAvailable(port)) { | ||
servers.push({ name, getOptions: () => opts }); | ||
} else { | ||
debug( | ||
`Not starting reuseExisting process ${name}: port ${port} is in use` | ||
); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
return servers; | ||
} | ||
exports.checkExtraProcs = checkExtraProcs; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const { createServer } = require('net'); | ||
|
||
const findOpenPort = require('find-open-port'); | ||
|
||
const { checkExtraProcs } = require('../../lib/processes/extra'); | ||
|
||
describe('extra processes in config', () => { | ||
/** @type {import('net').Server | undefined} */ | ||
let server; | ||
afterEach(async () => { | ||
if (server) { | ||
await new Promise(r => server.close(r)); | ||
server = undefined; | ||
} | ||
}); | ||
|
||
it('returns server objects for extra procs', async () => { | ||
// don't parallelize to avoid collisions | ||
const freePort1 = await findOpenPort.findPort(); | ||
const freePort2 = await findOpenPort.findPort(); | ||
|
||
server = createServer(); | ||
await new Promise(r => server.listen({ port: 0, host: '127.0.0.1' }, r)); | ||
const usedPort = server.address().port; | ||
|
||
const processes = { | ||
reuse1: { | ||
command: 'nc', | ||
commandArgs: ['-l', '%port%'], | ||
reuseExisting: true, | ||
port: freePort1, | ||
}, | ||
reuse2: { | ||
command: 'nc', | ||
commandArgs: ['-l', '%port%'], | ||
reuseExisting: true, | ||
port: usedPort, | ||
}, | ||
start1: { | ||
command: 'nc', | ||
commandArgs: ['-l', '%port%'], | ||
port: freePort2, | ||
}, | ||
start2: { | ||
command: 'nc', | ||
commandArgs: ['-l', '%port%'], | ||
}, | ||
}; | ||
|
||
const servers = await checkExtraProcs(processes); | ||
|
||
assert.ok( | ||
servers.every(s => s.name !== 'reuse2'), | ||
'reuseExisting with port in use should not be in servers list' | ||
); | ||
|
||
assert.strictEqual(servers.length, 3); | ||
assert.deepStrictEqual( | ||
servers.find(s => s.name === 'reuse1').getOptions(), | ||
{ | ||
command: 'nc', | ||
commandArgs: ['-l', '%port%'], | ||
port: freePort1, | ||
} | ||
); | ||
}); | ||
|
||
it('errors if configs are incorrect', async () => { | ||
await assert.rejects( | ||
checkExtraProcs({ | ||
kaboom: { reuseExisting: true }, | ||
}), | ||
{ message: /reuseExisting.+static port/ } | ||
); | ||
}); | ||
}); |