Skip to content

Commit

Permalink
fix: duplicate process info
Browse files Browse the repository at this point in the history
PR-URL: #83
Reviewed-BY: hyj1991 <[email protected]>
  • Loading branch information
hyj1991 authored Jul 28, 2020
1 parent df3987d commit 3610483
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
22 changes: 18 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,30 @@ exports.splitLogDirInfo = splitLogDirInfo;

function setLogDirToFile(logdir) {
const dataFile = path.join(os.homedir(), '.xprofiler');
fs.writeFileSync(dataFile, composeLogDirInfo(logdir), { flag: 'a' });

// check pid exists
let shouldWrite = true;
if (fs.existsSync(dataFile)) {
const content = fs.readFileSync(dataFile, 'utf8');
if (content.trim().split('\n').some(proc => proc
&& Number(proc.split('\u0000')[0]) === process.pid)) {
shouldWrite = false;
}
}

// write process info
if (shouldWrite) {
fs.writeFileSync(dataFile, composeLogDirInfo(logdir), { flag: 'a' });
}

// clean invalid record after randon 1min
const checkTime = process.env.UNIT_TEST_CHECK_TIME || (50 + Math.random() * 15);
const timer = setTimeout(() => {
const processes = fs
let processes = fs
.readFileSync(dataFile, 'utf8')
.split('\n')
.filter(p => p && processAlive(p.split(SPLITTER)[0]))
.join('\n') + '\n';
.filter(p => p && processAlive(p.split(SPLITTER)[0]));
processes = Array.from(new Set(processes)).join('\n') + '\n';
fs.writeFileSync(dataFile, processes);
}, parseInt(checkTime * 1000));
timer.unref();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@istanbuljs/schema": "^0.1.2",
"autod": "^3.1.0",
"clang-format": "^1.4.0",
"codecov": "^3.7.0",
"codecov": "^3.7.2",
"eslint": "^6.8.0",
"expect.js": "^0.3.1",
"formstream": "^1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion scripts/6u.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const build = require('./build');
const nodeVersions = [
'node-v8.17.0',
'node-v9.11.2',
'node-v10.19.0',
'node-v10.22.0',
'node-v11.15.0',
];

Expand Down
4 changes: 2 additions & 2 deletions scripts/7u.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const build = require('./build');

const nodeVersions = [
'node-v12.18.0',
'node-v12.18.3',
'node-v13.14.0',
'node-v14.4.0',
'node-v14.6.0',
];

build(nodeVersions);
6 changes: 3 additions & 3 deletions scripts/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const build = require('./build');
const nodeVersions = [
'node-v8.17.0',
'node-v9.11.2',
'node-v10.19.0',
'node-v10.22.0',
'node-v11.15.0',
'node-v12.18.0',
'node-v12.18.3',
'node-v13.14.0',
'node-v14.4.0'
'node-v14.6.0'
];

build(nodeVersions);
8 changes: 8 additions & 0 deletions test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe(`xprofiler starting`, function () {
mm(process.env, 'UNIT_TEST_CHECK_TIME', checkTime);
fs.writeFileSync(xprofilerPath, [invaidPid, logdir].join(SPLITTER) + '\n');
xprofiler({ log_dir: logdir });
xprofiler({ log_dir: logdir });
xprofiler({ log_dir: logdir });
await utils.sleep((checkTime + 3) * 1000);
});

Expand Down Expand Up @@ -73,4 +75,10 @@ describe(`xprofiler starting`, function () {
});
});
});

it('duplicate process info not exists in ~/.xprofiler', function () {
const content = fs.readFileSync(xprofilerPath, 'utf8').trim();
const aliveProcessInfo = content.split('\n').map(line => line.split(SPLITTER));
expect(aliveProcessInfo.length).to.be(1);
});
});

0 comments on commit 3610483

Please sign in to comment.