Skip to content

Commit

Permalink
feat: support config nodeExe
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu committed Sep 20, 2024
1 parent 9568f5f commit d590002
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bin/xtransit.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async function start(configPath) {
const logPath = getXtransitLogPath();
const out = await open(logPath, 'a');
const err = await open(logPath, 'a');
const nodeExe = await getNodeExe(process.pid, false);
const nodeExe = cfg.nodeExe || await getNodeExe(process.pid, false);
const proc = cp.spawn(nodeExe, [guard, configPath], {
detached: true,
stdio: ['ipc', out, err],
Expand Down
2 changes: 1 addition & 1 deletion commands/check_process_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function checkInstalled(nodeExe, cwd) {
}

async function checkStatus() {
const nodeExe = await getNodeExe(pid);
const nodeExe = process.env.XTRANSIT_NODE_EXE || await getNodeExe(pid);
status.nodeVersion = await execute(`${nodeExe} -v`);
status.alinodeVersion = await execute(`${nodeExe} -V`);

Expand Down
2 changes: 1 addition & 1 deletion commands/xprofctl.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function takeAction() {
// hanle coredump
if (command === 'generate_coredump') {
data.type = 'core';
const nodepath = await realpath(await getNodeExe(process.pid, false));
const nodepath = process.env.XTRANSIT_NODE_EXE || await realpath(await getNodeExe(process.pid, false));
data.executable_path = nodepath;
data.node_version = process.versions.node;
data.alinode_version = process.versions.alinode;
Expand Down
2 changes: 2 additions & 0 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class XtransitAgent extends EventEmitter {
this.checkAgentConfig(config);

// required config
this.config = config;
this.server = config.server;
this.appId = config.appId;
this.appSecret = config.appSecret;
Expand All @@ -45,6 +46,7 @@ class XtransitAgent extends EventEmitter {
this.customAgent = config.customAgent;
this.filterProcessEnvName = config.filterProcessEnvName;
this.customXprofilerLogs = config.customXprofilerLogs;
this.nodeExe = config.nodeExe;

// global var
this.conn = null;
Expand Down
13 changes: 10 additions & 3 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function realCommandFile(commands, file) {
return { find, file };
}

module.exports = async function(message) {
module.exports = async function(message, options) {
try {
this.logger.debug(`[from xtransit-server] >>>>>>>>>>>>>>>>>>> ${message}`);
const data = JSON.parse(message);
Expand Down Expand Up @@ -97,9 +97,16 @@ module.exports = async function(message) {
};

// exec command
const nodeExe = await getNodeExe(process.pid, false);
let nodeExe;
if (this.config && this.config.nodeExe) {
nodeExe = this.config.nodeExe;
execOptions.env.XTRANSIT_NODE_EXE = this.config.nodeExe;
} else {
nodeExe = await getNodeExe(process.pid, false);
}
this.logger.debug(`[execute command] ${nodeExe} ${args.join(' ')}`);
const { stdout, stderr: error } = await execFile(nodeExe, args, execOptions);
const aExecFile = options && options.execFile || execFile;
const { stdout, stderr: error } = await aExecFile(nodeExe, args, execOptions);
const stderr = stdout ? '' : error;

// check action file status
Expand Down
61 changes: 61 additions & 0 deletions test/config.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'use strict';

const cp = require('child_process');
const os = require('os');
const path = require('path');
const { promisify } = require('util');
const mm = require('mm');
const expect = require('expect.js');
const Agent = require('../lib/agent');
const messageHandler = require('../lib/handler');

function getError(config) {
let error;
Expand Down Expand Up @@ -105,4 +109,61 @@ describe('get config', function() {
});
expect(agent.logdir).to.be(logDir);
});

it('shoule use config.nodeExe', async function() {
const logdir = path.resolve('/path/to/xprofiler');
const logLevel = 3;
const errexp = /Error: /;
const reconnectBaseTime = 30;
const heartbeatInterval = 30;
const docker = true;
const ipMode = true;
const libMode = true;
const cleanAfterUpload = true;
const disks = ['/', '/', '/test'];
const errors = ['/error.log', '/error.log', '/error.log'];
const packages = ['/package.json', '/package.json', '/package.json'];
const titles = ['mock-node'];
const agent = new Agent({
nodeExe: 'foo',
server: 'ws://127.0.0.1',
appId: 1,
appSecret: 'test',
logdir,
logLevel,
errexp,
reconnectBaseTime,
heartbeatInterval,
docker,
ipMode,
libMode,
cleanAfterUpload,
disks,
errors,
packages,
titles,
});
let execOptions;
let exeFile;
mm(cp, 'execFile', (file, args, options, cb) => {
execOptions = options;
exeFile = file;
cb(null, {
stdout: 'succeed',
stderr: null,
});
});
const execFile = promisify(cp.execFile);
await messageHandler.call(agent, JSON.stringify({
traceId: 'mock_trace_id',
type: 'exec_command',
data: {
command: 'check_process_status 2',
},
}), {
execFile,
});
expect(exeFile).to.be('foo');
expect(execOptions.env.XTRANSIT_NODE_EXE).to.be('foo');
});
});

0 comments on commit d590002

Please sign in to comment.