Skip to content

Commit

Permalink
fix(prometheus): bootstrap for egg (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
stone-jin authored May 17, 2021
1 parent 7d3eede commit aeb0888
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/prometheus/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@midwayjs/prometheus",
"version": "2.10.15",
"version": "2.10.15-beta.3",
"description": "midway component for prometheus",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -31,5 +31,8 @@
"dependencies": {
"prom-client": "^13.1.0",
"request": "^2.88.2"
},
"engines": {
"node": ">= 12.0.0"
}
}
11 changes: 8 additions & 3 deletions packages/prometheus/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@midwayjs/decorator';
import { join } from 'path';
import * as PromClient from 'prom-client';
import { isMaster } from './utils/utils';
import { isMaster, closeLock } from './utils/utils';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
Expand All @@ -31,8 +31,12 @@ export class AutoConfiguration {
PromClient.collectDefaultMetrics(this.prometheusConfig);
const modules = listModule('prometheus:master');
const handlers = {};
const sockFile = path.join(os.tmpdir(), 'midway-master.sock');
if (modules.length > 0) {
let sockFile = path.join(os.tmpdir(), 'midway-master.sock');
if (process.platform === 'win32') {
sockFile =
'\\\\.\\pipe\\' + sockFile.replace(/^\//, '').replace(/\//g, '-');
}
if (modules.length > 0 && process.platform !== 'win32') {
if (isMaster()) {
if (fs.existsSync(sockFile)) {
fs.unlinkSync(sockFile);
Expand Down Expand Up @@ -87,6 +91,7 @@ export class AutoConfiguration {

async onStop() {
if (isMaster()) {
closeLock();
this.http_server.close();
}
}
Expand Down
39 changes: 39 additions & 0 deletions packages/prometheus/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
import * as cluster from 'cluster';
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';

let gfp = null;
let bInit = false;
const ppid = process.ppid;
const now = new Date();
const lockFile = path.join(
os.tmpdir(),
`midway-master-${now.getFullYear()}-${
now.getMonth() + 1
}-${now.getDate()}-${ppid}.lock`
);
export function isMaster() {
if (cluster.isMaster) {
return true;
}

if (process.argv[1].indexOf('egg-cluster') >= 0) {
// Is run with egg-scripts
if (bInit && gfp) {
return true;
} else if (bInit) {
return false;
} else {
bInit = true;
try {
const result = fs.openSync(lockFile, 'wx');
gfp = result;
return true;
} catch (e) {
return false;
}
}
}

if (process.env && process.env.pm_id) {
//Is run with PM2
if (parseInt(process.env.NODE_APP_INSTANCE) === 0) {
Expand All @@ -14,3 +45,11 @@ export function isMaster() {

return false;
}

export function closeLock() {
if (gfp) {
fs.closeSync(gfp);
fs.unlinkSync(lockFile);
gfp = null;
}
}

0 comments on commit aeb0888

Please sign in to comment.