主要对于已经完成的子任务的清理 metrics.js 为prometheus metrics 定义 app.js exp
以下是一个简单的基于nodejs 的fork 子进程创建子任务,同时使用prometheus 袒露一些简单的metrics
使用express 框架
项目布局
├── Dockerfile
├── README.md
├── app.js
├── docker-compose.yaml
├── grafana
│ └── metrics.json
├── metrics.js
├── package.json
├── prometheus.yml
├── send_mail.js
├── taskstatus.js
├── utils.js
└── yarn.lock
布局说明
send_mail.js 为简单的fork 子进程的代码
taskstatus.js 为存储子进程任务状态的
utils.js 为一个简单的工具模块,,主要对付已经完成的子任务的清理
metrics.js 为prometheus metrics 界说
app.js express rest 接口操纵以及任务措置惩罚惩罚的核心代码
docker-compose.yaml docker-compose 全家桶运行
Dockerfile nodejs 应用容器化
grafana/metrics.json 为应用grafana 对付 prometheus 的dashboard 配置
代码说明
app.js 核心措置惩罚惩罚
const { fork } = require(‘child_process‘)
const express = require("express")
const util = require("./utils")
const uuid = require("uuid/v4")
const { child_process_status_all, child_process_status_pending, child_process_status_ok } = require("./taskstatus")
const { child_process_status_all_counter, child_process_status_pending_gauge, child_process_status_ok_counter, initMetrics, initGcStats, process_ok_clean_timer__status, up } = require("./metrics")
const app = express();
const main_process_id = process.pid;
let interval = false;
?
/**
* metrics route register 注册prometheus metrcis 路由
*/
app.get(‘/metrics‘, (req, res) => {
initMetrics(req, res);
})
/**
* disable process clean timer 禁用按时任务清理
*/
app.get("/disable_timer", (req, res) => {
if (interval) {
interval = false;
}
process_ok_clean_timer__status.set(0)
res.send({ timer_statuss: false })
})
/**
* enable process clean timer 启用按时任务清理子进程
*/
app.get("/enable_timer", (req, res) => {
if (interval == false) {
interval = true;
}
process_ok_clean_timer__status.set(1)
res.send({ timer_statuss: true })
})
?
/**
* for create process workers
*/
app.get(‘/endpoint‘, (req, res) => {
// fork another process 子进程的创建以及动静的通信(状态以及部分prometheus metrics 维护)
const myprocess = fork(‘./send_mail.js‘);
child_process_status_pending[myprocess.pid] = {
status: "pending"
}
child_process_status_all[myprocess.pid] = {
status: "pending"
}
child_process_status_all_counter.inc(1)
child_process_status_pending_gauge.inc(1)
console.log(`fork process pid:${myprocess.pid}`)
const mails = uuid();
// send list of e-mails to forked process
myprocess.send({ mails });
// listen for messages from forked process
myprocess.on(‘message‘, (message) => {
console.log(`Number of mails sent ${message.counter}`);
child_process_status_ok[myprocess.pid] = {
status: "ok"
}
child_process_status_ok_counter.inc(1)
child_process_status_pending_gauge.dec(1)
child_process_status_all[myprocess.pid] = {
status: "ok"
}
delete child_process_status_pending[myprocess.pid]
});
return res.json({ status: true, sent: true });
});
?
/**
* call api for stop processed workers 删除任务完成的子进程
*/
app.get("/stop", (req, res) => {
util.stopProcess(main_process_id, (err, data) => {
if (err == null) {
res.send({ timer_clean_status: "ok" })
}
})
})
?
// init gc metrics gc metrcis 袒露
initGcStats()
// clean ok process timer 按时任务清理完成的进程
setInterval(function () {
if (interval) {
util.stopProcess(main_process_id, (err, data) => {
if (err == null) {
console.log({ timer_clean_status: "ok" })
} else {
process_ok_clean_timer__status.set(0)
}
})
}
}, 10000)
// set metric status to up
up.set(1)
app.listen(8080, "0.0.0.0", () => {
console.log(`go to :8080/ to generate traffic`)
}).on("error", () => {
up.set(0)
})
utils.js 按时任务清理模块
const psTree = require("ps-tree")
const {spawn } = require(‘child_process‘)
const {child_process_status_ok} = require("./taskstatus")
const {process_ok_clean_timer__status} = require("./metrics")
/**
*
* @param {mainProcessID} mainProcessID
* @param {cb} callback for check status
*/
function stopProcess(mainProcessID,cb){
psTree(mainProcessID, function (err, children) {
if (err){
process_ok_clean_timer__status.set(0)
}
let pids = [];
for (const key in child_process_status_ok) {
if (child_process_status_ok.hasOwnProperty(key)) {
pids.push(key)
delete child_process_status_ok[key]
}
}
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31945.html
- 上一篇: --子选择器:以表示
- 下一篇:动态请求才转发请求到后台交由Tomcat进行处理