Total Complexity | 9 |
Complexity/F | 2.25 |
Lines of Code | 39 |
Function Count | 4 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | // @see https://nodejs.org/dist/latest-v4.x/docs/api/cluster.html |
||
2 | 'use strict' |
||
3 | |||
4 | // import kue from 'kue' |
||
5 | import cluster from 'cluster' |
||
6 | import logger from 'winston' |
||
7 | |||
8 | export default function (done) { |
||
9 | const isProduction = process.env.NODE_ENV === 'production' |
||
10 | if (isProduction && cluster.isMaster) { |
||
11 | const numCPUs = require('os').cpus() |
||
12 | |||
13 | // init cluster |
||
14 | numCPUs.forEach(() => { |
||
15 | const worker = cluster.fork() |
||
16 | |||
17 | // for parallel jobs processing kue |
||
18 | // kue.app.listen(8089) |
||
19 | // kue.app.set('title', 'My background jobs') |
||
20 | |||
21 | worker.on('online', () => { |
||
22 | logger.warn('[cluster][worker]', `${worker.process.pid} is online now!`) |
||
23 | }) |
||
24 | |||
25 | worker.on('exit', (code, signal) => { |
||
26 | if (signal) { |
||
27 | logger.warn('[cluster][worker]', `was killed by signal: ${signal}`) |
||
28 | } else if (code !== 0) { |
||
29 | logger.warn('[cluster][worker]', `exited with error code: ${code}`) |
||
30 | } else { |
||
31 | logger.warn('[cluster][worker]', 'success!') |
||
32 | } |
||
33 | }) |
||
34 | }) |
||
35 | |||
36 | logger.warn('[cluster]', `Clustering: I will start ${numCPUs.length} workers...`) |
||
37 | } else if (typeof done === 'function') { |
||
38 | done() |
||
39 | } |
||
40 | } |
||
41 |