-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* gc sidecar as stream, add usage * fix win * remove unused code * use stream, remove GC class * remove unused prop * GC as stream, use os.kill * use text-decoder * fix win * gc to lib/gc * rebase and update lib errors * remove text-decoder * cmd to resource, errors * add await * inline close
- Loading branch information
1 parent
8e93b41
commit f2cd5b0
Showing
6 changed files
with
131 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
const { print, outputter, InputError } = require('./iface') | ||
const parse = require('../lib/parse') | ||
|
||
const output = outputter('gc', { | ||
kill: ({ pid }) => `Killed sidecar with pid: ${pid}`, | ||
complete: ({ killed }) => { return killed.length > 0 ? `Total killed sidecars: ${killed.length}` : 'No running sidecars' }, | ||
error: ({ code, message, stack }) => `GC Error (code: ${code || 'none'}) ${message} ${stack}` | ||
}) | ||
|
||
module.exports = (ipc) => async function gc (args) { | ||
try { | ||
const flags = parse.args(args, { | ||
boolean: ['json'] | ||
}) | ||
const { _, json } = flags | ||
const [resource] = _ | ||
if (!resource) throw new InputError('A <cmd> must be specified.') | ||
if (resource !== 'sidecar') throw new InputError(`Resource '${resource}' is not valid`) | ||
const stream = ipc.gc({ pid: Bare.pid, resource }, ipc) | ||
await output(json, stream) | ||
} catch (err) { | ||
if (err instanceof InputError || err.code === 'ERR_INVALID_FLAG') { | ||
print(err.message, false) | ||
ipc.userData.usage.output('gc') | ||
} else { | ||
print('An error occured', false) | ||
} | ||
Bare.exit(1) | ||
} finally { | ||
await ipc.close() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict' | ||
const { isBare, isWindows } = require('which-runtime') | ||
const os = isBare ? require('bare-os') : require('os') | ||
const { spawn } = isBare ? require('bare-subprocess') : require('child_process') | ||
const { Readable } = require('streamx') | ||
|
||
module.exports = class GarbageCollector extends Readable { | ||
constructor (client, engine) { | ||
super() | ||
this.client = client | ||
this.engine = engine | ||
} | ||
|
||
_destroy (cb) { | ||
cb(null) | ||
} | ||
|
||
sidecar ({ pid }) { | ||
const name = 'pear-runtime' | ||
const flag = '--sidecar' | ||
|
||
const [sh, args] = isWindows | ||
? ['cmd.exe', ['/c', `wmic process where (name like '%${name}%') get name,executablepath,processid,commandline /format:csv`]] | ||
: ['/bin/sh', ['-c', `ps ax | grep -i -- '${name}' | grep -i -- '${flag}'`]] | ||
|
||
const sp = spawn(sh, args) | ||
let output = '' | ||
let pidIndex = isWindows ? -1 : 0 | ||
let isHeader = !!isWindows | ||
const killed = [] | ||
|
||
sp.stdout.on('data', (data) => { | ||
output += data.toString() | ||
const lines = output.split(isWindows ? '\r\r\n' : '\n') | ||
output = lines.pop() | ||
for (const line of lines) { | ||
if (!line.trim()) continue | ||
const columns = line.split(isWindows ? ',' : ' ').filter(col => col) | ||
if (isHeader && isWindows) { | ||
const index = columns.findIndex(col => /processid/i.test(col.trim())) | ||
pidIndex = index !== -1 ? index : 4 | ||
isHeader = false | ||
} else { | ||
const id = parseInt(columns[pidIndex]) | ||
if (!isNaN(id) && ![Bare.pid, sp.pid, pid].includes(id)) { | ||
os.kill(id) | ||
this.push({ tag: 'kill', data: { pid: id } }) | ||
killed.push(id) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
sp.on('exit', (code, signal) => { | ||
if (code !== 0 || signal) { | ||
this.#error(new Error(`Process exited with code: ${code}, signal: ${signal}`)) | ||
} | ||
this.push({ tag: 'complete', data: { killed } }) | ||
this.push({ tag: 'final', data: { success: true } }) | ||
this.push(null) | ||
}) | ||
} | ||
|
||
#error (err) { | ||
const { stack, code, message } = err | ||
this.push({ tag: 'error', data: { stack, code, message, success: 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