-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(neotracker): add neotracker command to neo-one build (#1870)
- Loading branch information
1 parent
a82552e
commit 9a3e32a
Showing
28 changed files
with
2,073 additions
and
116 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
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
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
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
25 changes: 25 additions & 0 deletions
25
packages/neo-one-cli/src/__e2e__/cmd/start/neotracker.test.ts
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,25 @@ | ||
import fetch from 'cross-fetch'; | ||
import { isRunning } from '../../../common'; | ||
|
||
describe('start network', () => { | ||
it('starts a private network', async () => { | ||
const execAsync = one.createExecAsync('ico'); | ||
execAsync('start network'); | ||
const config = await one.getProjectConfig('ico'); | ||
|
||
await one.until(async () => { | ||
const [live, ready] = await Promise.all([ | ||
fetch(`http://localhost:${config.network.port}/live_health_check`), | ||
fetch(`http://localhost:${config.network.port}/ready_health_check`), | ||
]); | ||
expect(live.ok).toEqual(true); | ||
expect(ready.ok).toEqual(true); | ||
}); | ||
|
||
execAsync('start neotracker'); | ||
await one.until(async () => { | ||
const live = await isRunning(config.neotracker.port); | ||
expect(live).toEqual(true); | ||
}); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
packages/neo-one-cli/src/__e2e__/cmd/stop/neotracker.test.ts
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,35 @@ | ||
import fetch from 'cross-fetch'; | ||
import { isRunning } from '../../../common'; | ||
|
||
describe('start network', () => { | ||
it('starts a private network', async () => { | ||
const execAsync = one.createExecAsync('ico'); | ||
execAsync('start network'); | ||
const config = await one.getProjectConfig('ico'); | ||
|
||
await one.until(async () => { | ||
const [live, ready] = await Promise.all([ | ||
fetch(`http://localhost:${config.network.port}/live_health_check`), | ||
fetch(`http://localhost:${config.network.port}/ready_health_check`), | ||
]); | ||
expect(live.ok).toEqual(true); | ||
expect(ready.ok).toEqual(true); | ||
}); | ||
|
||
execAsync('start neotracker'); | ||
|
||
await one.until(async () => { | ||
const live = await isRunning(config.neotracker.port); | ||
expect(live).toEqual(true); | ||
}); | ||
|
||
await one.createExec('ico')('stop neotracker'); | ||
let success = false; | ||
try { | ||
success = await isRunning(config.neotracker.port); | ||
} catch { | ||
// do nothing | ||
} | ||
expect(success).toEqual(false); | ||
}); | ||
}); |
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,44 @@ | ||
import { Configuration } from '@neo-one/cli-common'; | ||
import execa from 'execa'; | ||
import { isRunning } from '../common'; | ||
import { Command } from '../types'; | ||
import { findKillProcess } from '../utils'; | ||
|
||
export const startNeotracker = async (cmd: Command, config: Configuration, reset: boolean) => { | ||
const args = cmd.args.concat(['start', 'neotracker']); | ||
if (reset) { | ||
await findKillProcess('neotracker', config); | ||
} | ||
|
||
const proc = execa(cmd.bin, reset ? args.concat(['--reset']) : args, { | ||
cleanup: false, | ||
detached: true, | ||
stdio: 'ignore', | ||
}); | ||
proc.unref(); | ||
|
||
const start = Date.now(); | ||
const timeoutMS = 30 * 1000; | ||
let ready = false; | ||
if (reset) { | ||
await new Promise((resolve) => setTimeout(resolve, 3000)); | ||
} | ||
// tslint:disable-next-line no-loop-statement | ||
while (Date.now() - start < timeoutMS) { | ||
try { | ||
const response = await isRunning(config.neotracker.port); | ||
if (response) { | ||
ready = true; | ||
break; | ||
} | ||
} catch { | ||
// do nothing | ||
} | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 500)); | ||
} | ||
|
||
if (!ready) { | ||
throw new Error('Neotracker is not ready.'); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import yargs from 'yargs'; | ||
import * as neotracker from './neotracker'; | ||
import * as network from './network'; | ||
|
||
export const command = 'start'; | ||
export const describe = 'Start NEO•ONE services.'; | ||
export const builder = (yargsBuilder: typeof yargs) => yargsBuilder.command(network); | ||
export const builder = (yargsBuilder: typeof yargs) => yargsBuilder.command(network).command(neotracker); |
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,55 @@ | ||
import { cliLogger } from '@neo-one/logger'; | ||
import { Yarguments } from '@neo-one/utils-node'; | ||
import execa from 'execa'; | ||
import * as nodePath from 'path'; | ||
import yargs from 'yargs'; | ||
import { isRunning, start } from '../../common'; | ||
import { findKillProcess } from '../../utils'; | ||
import { writePidFile } from './writePidFile'; | ||
|
||
export const command = 'neotracker'; | ||
export const describe = 'Start a NEO tracker instance using the project configuration.'; | ||
export const builder = (yargsBuilder: typeof yargs) => | ||
yargsBuilder | ||
.boolean('reset') | ||
.describe('reset', 'Reset the NEO tracker database.') | ||
.default('reset', false); | ||
export const handler = (argv: Yarguments<ReturnType<typeof builder>>) => { | ||
start(async (_cmd, config) => { | ||
const running = await isRunning(config.neotracker.port); | ||
if (running) { | ||
if (argv.reset) { | ||
await findKillProcess('neotracker', config); | ||
} else { | ||
cliLogger.info('NEO tracker is already running'); | ||
|
||
return undefined; | ||
} | ||
} | ||
|
||
const args = [ | ||
'neotracker', | ||
'--port', | ||
`${config.neotracker.port}`, | ||
'--nodeRpcUrl', | ||
`http://localhost:${config.network.port}/rpc`, | ||
'--dbFileName', | ||
nodePath.resolve(config.neotracker.path, 'db.sqlite'), | ||
]; | ||
const proc = execa( | ||
nodePath.resolve(require.resolve('@neotracker/core/bin'), '../', 'neotracker'), | ||
argv.reset ? args.concat(['--resetDB']) : args, | ||
{ | ||
cleanup: false, | ||
stdio: 'ignore', | ||
}, | ||
); | ||
proc.unref(); | ||
|
||
await writePidFile('neotracker', proc, config); | ||
|
||
return async () => { | ||
proc.kill(); | ||
}; | ||
}); | ||
}; |
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,15 @@ | ||
import { Configuration } from '@neo-one/cli-common'; | ||
import { ExecaChildProcess } from 'execa'; | ||
import * as fs from 'fs-extra'; | ||
import * as nodePath from 'path'; | ||
import { getProcessIDFile } from '../../common'; | ||
|
||
export const writePidFile = async ( | ||
name: 'network' | 'neotracker', | ||
proc: NodeJS.Process | ExecaChildProcess, | ||
config: Configuration, | ||
) => { | ||
const pidFile = getProcessIDFile(config, name); | ||
await fs.ensureDir(nodePath.dirname(pidFile)); | ||
await fs.writeFile(pidFile, proc.pid); | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import yargs from 'yargs'; | ||
import * as neotracker from './neotracker'; | ||
import * as network from './network'; | ||
|
||
export const command = 'stop'; | ||
export const describe = 'Stop NEO•ONE services.'; | ||
export const builder = (yargsBuilder: typeof yargs) => yargsBuilder.command(network); | ||
export const builder = (yargsBuilder: typeof yargs) => yargsBuilder.command(network).command(neotracker); |
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,12 @@ | ||
import yargs from 'yargs'; | ||
import { start } from '../../common'; | ||
import { findKillProcess } from '../../utils'; | ||
|
||
export const command = 'neotracker'; | ||
export const describe = 'Stops the local neotracker instance.'; | ||
export const builder = (yargsBuilder: typeof yargs) => yargsBuilder; | ||
export const handler = () => { | ||
start(async (_cmd, config) => { | ||
await findKillProcess('neotracker', config); | ||
}); | ||
}; |
Oops, something went wrong.