Skip to content

Commit

Permalink
feat: support config nodeExe (#54)
Browse files Browse the repository at this point in the history
* feat: support config nodeExe
  • Loading branch information
killagu authored Sep 20, 2024
1 parent 9568f5f commit 3f2bb0d
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 28 deletions.
3 changes: 1 addition & 2 deletions bin/xtransit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node
'use strict';

const os = require('os');
const fs = require('fs');
const path = require('path');
const moment = require('moment');
Expand Down Expand Up @@ -106,7 +105,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
3 changes: 1 addition & 2 deletions commands/xprofctl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const fs = require('fs');
const os = require('os');
const path = require('path');
const { promisify } = require('util');
const exists = promisify(fs.exists);
Expand Down Expand Up @@ -55,7 +54,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: 1 addition & 1 deletion common/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Parser {
reject(err);
};

cleanup = function () {
cleanup = function() {
readable.removeListener('data', onData);
readable.removeListener('end', onEnd);
readable.removeListener('error', onError);
Expand Down
12 changes: 6 additions & 6 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ exports.getNodeProcessInfo = function(proc, platform) {

exports.sleep = promisify(setTimeout);

exports.getXprofilerPath = function () {
exports.getXprofilerPath = function() {
const prefix = process.env.XPROFILER_PREFIX || process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR || os.homedir();
return path.join(prefix, '.xprofiler');
}
};

exports.getXtransitPath = function () {
exports.getXtransitPath = function() {
const prefix = process.env.XTRANSIT_PREFIX || process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR || os.homedir();
return path.join(prefix, '.xtransit');
}
};

exports.getXtransitLogPath = function () {
exports.getXtransitLogPath = function() {
const prefix = process.env.XTRANSIT_PREFIX || process.env.XPROFILER_UNIT_TEST_TMP_HOMEDIR || os.homedir();
return path.join(prefix, '.xtransit.log');
}
};
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
2 changes: 1 addition & 1 deletion orders/xprofiler_log.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ exports = module.exports = async function() {
lines: process.env.UNIT_TEST && logs[0] ? logs[0].split('\n').length : 0,
};

if(typeof this.customXprofilerLogs === 'function') {
if (typeof this.customXprofilerLogs === 'function') {
const logs = message.data.logs;
logs.push(...await this.customXprofilerLogs(logs));
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test-single": "mocha -t 0 -R spec",
"cov": "nyc --reporter=html --reporter=text --reporter=lcov mocha -R spec test/*.test.js --timeout 0",
"cov-single": "nyc --reporter=html --reporter=text --reporter=lcov mocha --timeout 0 -R spec",
"ci": "npm run lint && npm run cov && codecov",
"ci": "npm run lint && npm run cov",
"release": "node scripts/release.js",
"autod": "autod"
},
Expand Down Expand Up @@ -55,7 +55,6 @@
},
"devDependencies": {
"autod": "^3.1.0",
"codecov": "^3.7.2",
"eslint": "^6.8.0",
"eslint-config-egg": "^8.1.2",
"expect.js": "^0.3.1",
Expand Down
63 changes: 63 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 All @@ -16,6 +20,8 @@ function getError(config) {
}

describe('get config', function() {
afterEach(mm.restore);

it('should throw error', function() {
expect(getError({})).to.be('config.server must be passed in!');
expect(getError({ server: 'mock' })).to.be('config.server must be passed in!');
Expand Down Expand Up @@ -105,4 +111,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');
});
});
21 changes: 11 additions & 10 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,59 @@
'use strict';

const path = require('path');
const utils = require('../common/utils');
const assert = require('assert');
const mm = require('mm');
const os = require('os');

describe('utils.test.js', function () {
describe('utils.test.js', function() {
afterEach(mm.restore);

describe('get xprofiler path', () => {
it('should work with XPROFILER_PREFIX', () => {
mm(process.env, 'XPROFILER_PREFIX', '/tmp');
const p = utils.getXprofilerPath();
assert.equal(p, '/tmp/.xprofiler');
assert.equal(p, path.normalize('/tmp/.xprofiler'));
});

it('should work default is home', () => {
mm(os, 'homedir', () => {
return '/home/xxx'
return '/home/xxx';
});
const p = utils.getXprofilerPath();
assert.equal(p, '/home/xxx/.xprofiler');
assert.equal(p, path.normalize('/home/xxx/.xprofiler'));
});
});

describe('get xtransit path', () => {
it('should work with XTRANSIT_PREFIX', () => {
mm(process.env, 'XTRANSIT_PREFIX', '/tmp');
const p = utils.getXtransitPath();
assert.equal(p, '/tmp/.xtransit');
assert.equal(p, path.normalize('/tmp/.xtransit'));
});

it('should work default is home', () => {
mm(os, 'homedir', () => {
return '/home/xxx'
return '/home/xxx';
});
const p = utils.getXtransitPath();
assert.equal(p, '/home/xxx/.xtransit');
assert.equal(p, path.normalize('/home/xxx/.xtransit'));
});
});

describe('get xtransit log path', () => {
it('should work with XTRANSIT_PREFIX', () => {
mm(process.env, 'XTRANSIT_PREFIX', '/tmp');
const p = utils.getXtransitLogPath();
assert.equal(p, '/tmp/.xtransit.log');
assert.equal(p, path.normalize('/tmp/.xtransit.log'));
});

it('should work default is home', () => {
mm(os, 'homedir', () => {
return '/home/xxx'
return '/home/xxx';
});
const p = utils.getXtransitLogPath();
assert.equal(p, '/home/xxx/.xtransit.log');
assert.equal(p, path.normalize('/home/xxx/.xtransit.log'));
});
});
});

0 comments on commit 3f2bb0d

Please sign in to comment.