1
0
mirror of synced 2025-12-16 18:18:10 +08:00

auto commit

This commit is contained in:
yitter
2021-08-31 11:52:53 +08:00
parent 2308932d25
commit c669af4cbe
8 changed files with 37 additions and 37 deletions

View File

@@ -0,0 +1,54 @@
/**
* @description:
* @author: bubao
* @date: 2021-04-27 23:38:30
* @last author: bubao
* @last edit time: 2021-04-28 10:35:20
*/
const Redis = require("ioredis");
const { spawn } = require("child_process");
const config = require("../env.config.js");
const redis = new Redis(config);
//保存被子进程实例数组
var workers = {};
//这里的被子进程理论上可以无限多
// var appsPath = [__dirname+'/service/clickService.js',__dirname+'/service/showService.js'];
var createWorker = function (appPath, i) {
//保存spawn返回的进程实例
var worker = spawn('node', [appPath, i]);
//监听子进程exit事件
worker.on('exit', async function () {
console.info('worker:' + worker.pid + 'exited');
delete workers[worker.pid];
// createWorker(appPath);
if (Object.keys(workers).length === 0) {
console.log(await redis.scard('setTest'));
await redis.del("setTest");
redis.end();
}
});
workers[worker.pid] = worker;
console.info('create worker:' + worker.pid);
};
redis.del("setTest").then(() => {
//启动所有子进程
for (var i = 10; i > 0; i--) {
createWorker(__dirname + '/test.js', i);
}
});
//父进程退出时杀死所有子进程
process.on('exit', async function () {
console.info('parent exit.');
for (var pid in workers) {
workers[pid].kill('SIGHUP');
}
if (Object.keys(workers).length===0&&redis.status!=="end") {
console.log(await redis.scard('setTest'));
await redis.del("setTest");
redis.end();
}
});

21
JavaScript/test/test.js Normal file
View File

@@ -0,0 +1,21 @@
/**
* @description:
* @author: bubao
* @date: 2021-04-27 17:23:36
* @last author: bubao
* @last edit time: 2021-04-28 10:34:24
*/
const GenId = require('..')
const Redis = require("ioredis");
const config = require("../env.config.js");
const redis = new Redis(config);
const genid = new GenId({ WorkerId: (process.argv[2] || 1) - 0 });
(async () => {
for (let index = 0; index < 5000; index++) {
await redis.sadd("setTest", genid.NextId());
}
redis.end();
})();

20
JavaScript/test/test2.js Normal file
View File

@@ -0,0 +1,20 @@
const GenId = require('..')
function test1() {
const genid = new GenId({ WorkerId: 1 })
for (let index = 0; index < 5000; index++) {
console.log(genid.NextId());
}
}
function test2() {
const genid = new GenId({ WorkerId: 1 })
const id = genid.NextId()
console.log(typeof (id))
console.log(id, id.toString().length)
}
function main() {
test2()
}
main()