用node.js做cluster,监听异常的邮件提醒服务

释放双眼,带上耳机,听听看~!

1
2
3
4
5
6
7
8
1                    __                __           __                                       _ __
2   ____  ____  ____/ /__        _____/ /_  _______/ /____  _____      ___  ____ ___  ____ _(_) /
3  / __ \/ __ \/ __  / _ \______/ ___/ / / / / ___/ __/ _ \/ ___/_____/ _ \/ __ `__ \/ __ `/ / /
4 / / / / /_/ / /_/ /  __/_____/ /__/ / /_/ (__  ) /_/  __/ /  /_____/  __/ / / / / / /_/ / / /
5/_/ /_/\____/\__,_/\___/      \___/_/\__,_/____/\__/\___/_/         \___/_/ /_/ /_/\__,_/_/_/
6
7
8

A module for taking advantage of the built-in cluster module in node v0.8 and above.send email if cluster exception,you will recieve a detail email about worker exception.

Installation


1
2
3
1npm install https://github.com/sunNode/node-cluster-email
2
3

Usage

Initialize mail plugin with the given email(s), with the given options.

Options:

  • from sender email
  • timeout sendmail timeout in milliseconds
  • subject defaulting to "cluster({worker}) exception: {message}"
  • template function called with local variables (usually jade / ejs template etc)

Example


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1const cluster = require('cluster')
2const cpu = require ('os').cpus().length
3const mail = require('node-cluster-mail')
4
5/**
6 * [if description]
7 * @param  {[type]} cluster.isMaster [description]
8 * @return {[type]}                  [description]
9 */
10if (cluster.isMaster) {
11  // Fork workers.
12  for (let i = 0; i < cpu; i++) {
13    cluster.fork();
14  }
15
16  cluster.on('exit', function(worker, code, signal) {
17    mail('phonenix@gmail.com',{'from':'phonenix-test@gmail.com','cc':'phonenix-test2@gmail.com'})
18    console.log('worker ' + worker.process.pid + ' died');
19  });
20
21  cluster.on('fork', function(worker) {
22  console.log(`workers: ${worker.id} worker.process.pid :${worker.process.pid}`)
23});
24} else {
25  // Workers can share any TCP connection
26  // In this service its a restify server
27  require('./app')
28}
29
30

Reference

node-cluster-email

给TA打赏
共{{data.count}}人
人已打赏
安全技术

DES 加密 解密

2021-8-18 16:36:11

安全技术

C++ 高性能服务器网络框架设计细节

2022-1-11 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索