mirror of
https://github.com/koush/scrypted.git
synced 2026-02-18 12:32:37 +00:00
server: refactor runtimes
This commit is contained in:
60
server/src/plugin/runtime/python-worker.ts
Normal file
60
server/src/plugin/runtime/python-worker.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { RuntimeWorker, RuntimeWorkerOptions as RuntimeWorkerOptions } from "./runtime-worker";
|
||||
import child_process from 'child_process';
|
||||
import path from 'path';
|
||||
import { EventEmitter } from "ws";
|
||||
import { Writable, Readable } from 'stream';
|
||||
import { RpcMessage, RpcPeer } from "../../rpc";
|
||||
import readline from 'readline';
|
||||
import { ChildProcessWorker } from "./child-process-worker";
|
||||
|
||||
export class PythonRuntimeWorker extends ChildProcessWorker {
|
||||
|
||||
constructor(pluginId: string, options: RuntimeWorkerOptions) {
|
||||
super(pluginId, options);
|
||||
|
||||
const { env, pluginDebug } = options;
|
||||
const args: string[] = [
|
||||
'-u',
|
||||
];
|
||||
if (pluginDebug) {
|
||||
args.push(
|
||||
'-m',
|
||||
'debugpy',
|
||||
'--listen',
|
||||
`0.0.0.0:${pluginDebug.inspectPort}`,
|
||||
'--wait-for-client',
|
||||
)
|
||||
}
|
||||
args.push(
|
||||
path.join(__dirname, '../../../python', 'plugin-remote.py'),
|
||||
)
|
||||
|
||||
this.worker = child_process.spawn('python3', args, {
|
||||
// stdin, stdout, stderr, peer in, peer out
|
||||
stdio: ['pipe', 'pipe', 'pipe', 'pipe', 'pipe'],
|
||||
env: Object.assign({
|
||||
PYTHONPATH: path.join(process.cwd(), 'node_modules/@scrypted/types'),
|
||||
}, process.env, env),
|
||||
});
|
||||
|
||||
this.setupWorker();
|
||||
}
|
||||
|
||||
setupRpcPeer(peer: RpcPeer): void {
|
||||
const peerin = this.worker.stdio[3] as Writable;
|
||||
const peerout = this.worker.stdio[4] as Readable;
|
||||
|
||||
peerin.on('error', e => this.emit('error', e));
|
||||
peerout.on('error', e => this.emit('error', e));
|
||||
|
||||
const readInterface = readline.createInterface({
|
||||
input: peerout,
|
||||
terminal: false,
|
||||
});
|
||||
readInterface.on('line', line => peer.handleMessage(JSON.parse(line)));
|
||||
}
|
||||
|
||||
send(message: RpcMessage, reject?: (e: Error) => void): void {
|
||||
(this.worker.stdio[3] as Writable).write(JSON.stringify(message) + '\n', e => e && reject?.(e));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user