Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

Node.js Kue多进程事件队列用例

$
0
0

第一次社区发帖,破个处~

因为之前项目,线上因为超预期的用户量导致服务器cpu爆满,然后大家都懂的~ 今天恰好看到一篇node的事件队列的文章,作者大力推荐kue,说是基于redis的pub/sub… 于是开了个test, 废话不说,直接干~

既然是事件队列,如果单进程内测试多没意思,下面代码使用node的cluster模块创建4个子进程作为 kue的工作进程,然后在master内创建任务

'use strict';

const kue = require('kue');
const cluster = require('cluster');
const logger = require('log4js').getLogger("default");

const queues = kue.createQueue();

function createJob() {
	let job = queues.create("email", {
		name: ["jason", "kitty", "jion", "mali"][Math.random() * 4 | 0]
	}).save();
	logger.debug("create job [%s] from master[%d]", job.data.name, process.pid);
	setTimeout(createJob, Math.random() * 10000);
}

if (cluster.isMaster) {

	logger.debug("master %s running!", process.pid);

	for (let i = 0; i < 4; i++) {
		cluster.fork();
	}
	createJob();
	cluster.on('exit', function (worker, code) {
		logger.debug("worker %s is died by %s", worker.process.pid, code);
	});
} else {
	queues.process('email', (job, done)=> {
		logger.debug("worker %s process mail job[%d][%s]", process.pid, job.id, job.data.name);
		setTimeout(done, Math.random() * 20000);
	});
}

说明下 上面栗子 需要启动本机的redis 6379端口,另外需要一个log4js的模块(或者替换console输出) 执行输出: [2017-05-25 16:11:31.937] [DEBUG] default - master 52761 running! [2017-05-25 16:11:31.950] [DEBUG] default - create job [mali] from master[52761] [2017-05-25 16:11:32.134] [DEBUG] default - worker 52763 process mail job[72][mali] [2017-05-25 16:11:38.802] [DEBUG] default - create job [jion] from master[52761] [2017-05-25 16:11:38.812] [DEBUG] default - worker 52764 process mail job[73][jion] [2017-05-25 16:11:47.315] [DEBUG] default - create job [kitty] from master[52761] [2017-05-25 16:11:47.324] [DEBUG] default - worker 52766 process mail job[74][kitty] [2017-05-25 16:11:49.151] [DEBUG] default - create job [jion] from master[52761] [2017-05-25 16:11:49.159] [DEBUG] default - worker 52765 process mail job[75][jion] [2017-05-25 16:11:53.356] [DEBUG] default - create job [jason] from master[52761] [2017-05-25 16:11:53.361] [DEBUG] default - worker 52763 process mail job[76][jason] [2017-05-25 16:12:00.009] [DEBUG] default - create job [jason] from master[52761] [2017-05-25 16:12:00.018] [DEBUG] default - worker 52766 process mail job[77][jason] [2017-05-25 16:12:07.785] [DEBUG] default - create job [mali] from master[52761] [2017-05-25 16:12:07.789] [DEBUG] default - worker 52764 process mail job[78][mali] [2017-05-25 16:12:11.305] [DEBUG] default - create job [kitty] from master[52761] [2017-05-25 16:12:11.309] [DEBUG] default - worker 52763 process mail job[79][kitty]

最后在大概描述下个人理解的作用! 首先可以作为用户消息入口,输出到逻辑代码,这样用户的请求指令会被缓存到redis内,不会因为逻辑执行压力而在导致node内存暴涨崩溃~ 接上面说,可以扩展,接口和逻辑分离。 今天是第一次测试,看kue官方文档,支持事件优先等级和失败重试等,貌似非常强大!。 最后奉上github地址:https://github.com/Automattic/kue


Viewing all articles
Browse latest Browse all 14821

Trending Articles