Skip to content

Commit

Permalink
Merge pull request #154 from GetRayo/feature/clean-up
Browse files Browse the repository at this point in the history
Custom log, no more `pino`, linted.
  • Loading branch information
aichholzer committed Nov 26, 2019
2 parents a287348 + eb1ba34 commit fa841f5
Show file tree
Hide file tree
Showing 23 changed files with 2,909 additions and 1,475 deletions.
3,421 changes: 2,363 additions & 1,058 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Expand Up @@ -16,8 +16,8 @@
"scripts": {
"eslint:fix": "eslint --quiet --fix .",
"pretest": "lerna bootstrap",
"test": "eslint --quiet . && nyc --reporter=lcov _mocha -R progress --timeout 20000 test/index.js",
"unit": "_mocha -R spec --timeout 20000 test/index.js",
"test": "eslint --quiet . && LOG_LEVEL=debug nyc --reporter=lcov _mocha -R progress --timeout 20000 test/index.js",
"unit": "LOG_LEVEL=debug _mocha -R spec --timeout 20000 test/index.js",
"posttest": "nyc report --reporter=text",
"report": "codecov && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage ./.nyc_output",
"copies": "cp readme.md ./packages/rayo/. && echo rayo/ benchmarks/ compress/ send/ storm/ | xargs -n 1 -I file cp LICENSE ./packages/file",
Expand All @@ -26,20 +26,20 @@
"update": "npm-check -u"
},
"devDependencies": {
"codecov": "^3.5.0",
"coveralls": "^3.0.4",
"eslint": "^6.0.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-prettier": "^3.1.0",
"lerna": "^3.15.0",
"mocha": "^6.1.4",
"codecov": "^3.6.1",
"coveralls": "^3.0.8",
"eslint": "^6.7.1",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-prettier": "^3.1.1",
"lerna": "^3.19.0",
"mocha": "^6.2.2",
"npm-check": "^5.8.0",
"nyc": "^14.1.1",
"prettier": "^1.18.2",
"prettier": "^1.19.1",
"should": "^13.2.3",
"sinon": "^7.3.2",
"sinon": "^7.5.0",
"supertest": "^4.0.2"
}
}
1 change: 1 addition & 0 deletions packages/benchmarks/index.js
Expand Up @@ -44,6 +44,7 @@ const cannon = (title = null) =>
let index = 0;
const benchmark = async (results = []) => {
results.push(
// eslint-disable-next-line no-async-promise-executor
await new Promise(async (yes, no) => {
let file = files[index];
if (argv.o && argv.o !== file) {
Expand Down
496 changes: 322 additions & 174 deletions packages/benchmarks/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/benchmarks/package.json
Expand Up @@ -20,13 +20,13 @@
},
"license": "MIT",
"dependencies": {
"autocannon": "^4.0.0",
"autocannon": "^4.4.0",
"cli-table": "^0.3.1",
"express": "^4.17.1",
"fastify": "^2.4.1",
"fastify": "^2.10.0",
"kleur": "^3.0.3",
"minimist": "^1.2.0",
"ora": "^3.4.0",
"ora": "^4.0.3",
"pancho": "^1.0.2",
"polka": "^0.5.2",
"rayo": "^1.3.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/compress/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 27 additions & 27 deletions packages/rayo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/send/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions packages/storm/index.js
@@ -1,7 +1,8 @@
const cluster = require('cluster');
const EventEmitter = require('events');
const cpus = require('os').cpus();
const { log, monitor, messageHandler } = require('./monitor');
const log = require('./log');
const { monitor, messageHandler } = require('./monitor');

class Storm extends EventEmitter {
constructor(work, options) {
Expand All @@ -16,6 +17,12 @@ class Storm extends EventEmitter {
this.fork = this.fork.bind(this);
this.stop = this.stop.bind(this);

if (cluster.isMaster) {
cluster.setupMaster({ silent: true });
// Is the process from the master process needs to be piped into the workers.
// cluster.fork().process.stdout.pipe(process.stdout);
}

if (cluster.isWorker) {
this.work();
return messageHandler.bind(null, process)();
Expand All @@ -28,22 +35,23 @@ class Storm extends EventEmitter {
let processes = options.workers || cpus.length;
process.on('SIGINT', this.stop).on('SIGTERM', this.stop);
cluster.on('online', (wrk) => {
log.info(`Worker ${wrk.process.pid} is online`);
log.debug(`Worker ${wrk.process.pid} is online`);
this.emit('worker', wrk.process.pid);
});
cluster.on('exit', (wrk) => {
this.emit('exit', wrk.process.pid);
return this.fork(wrk);
});

log.info(`Master (${process.pid}) is forking ${processes} worker processes.`);
log.debug(`Master (${process.pid}) is forking ${processes} workers.`);
while (processes) {
processes -= 1;
cluster.fork();
}

cluster.masterPid = process.pid;
if (options.master) {
log.debug(`Master process: ${process.pid}`);
options.master = options.master.bind(this, cluster);
options.master();
}
Expand All @@ -65,9 +73,9 @@ class Storm extends EventEmitter {
index -= 1;
}

log.info('The cluster has been terminated.');
log.debug('The cluster has been terminated.');
this.emit('offline');
process.exit();
setTimeout(process.exit, 200);
}

fork(wrk) {
Expand Down
54 changes: 54 additions & 0 deletions packages/storm/log.js
@@ -0,0 +1,54 @@
const levels = {
debug: 4,
info: 3,
warn: 2,
error: 1
};

/* istanbul ignore next */
let logLevel = process.env.LOG_LEVEL || 'error';
/* istanbul ignore next */
logLevel = levels[logLevel] || 1;

const format = (prop, args) => {
/* istanbul ignore next */
const values = args
.map((arg) =>
!(arg instanceof Error) && typeof arg === 'object'
? JSON.stringify(arg, null, 2)
: arg
)
.join('\n');

const date = new Intl.DateTimeFormat('de-AT', {
timeZone: 'UTC',
dateStyle: 'short',
timeStyle: 'long',
hour12: false
})
.format(new Date())
.replace(',', '');

const upperCase = prop.toUpperCase();
const message = date;
return `[${upperCase}] (${message}) ${values}`;
};

/**
* Keep in mind that logging (writing to the console) can be an expensive operation
* in high traffic situations and it will have an impact on performance-sensitive
* applications.
*
* Use with caution!
*/
/* istanbul ignore next */
module.exports = new Proxy(
{},
{
get: (target, prop) => (...args) => {
return (levels[prop] || 1) <= logLevel
? process.stdout.write(format(prop, args))
: null;
}
}
);
16 changes: 9 additions & 7 deletions packages/storm/monitor.js
@@ -1,9 +1,7 @@
const parseurl = require('parseurl');
const pino = require('pino');
const { createServer } = require('http');
const log = require('./log');

const { STORM_LOG_NAME = 'Rayo', STORM_LOG_LEVEL = 'info' } = process.env;
const log = pino({ name: STORM_LOG_NAME, level: STORM_LOG_LEVEL });
const round = (number) => Math.round(number * 100) / 100;
const reform = (item) => {
item.upTime = item.upTime ? Math.floor(item.upTime) : undefined;
Expand Down Expand Up @@ -58,7 +56,11 @@ const requestHandler = (cluster, req, res) => {
.split('/');

if (service === 'monitor') {
return requestDispatch.bind(null, cluster, res)({
return requestDispatch.bind(
null,
cluster,
res
)({
workerId: parseInt(workerId, 10) || null,
command
});
Expand All @@ -74,7 +76,7 @@ module.exports = {
messageHandler: (process) => {
// The `master` process sent this message/command to the worker.
process.on('message', (cmd) => {
log.info(`Worker (${process.pid}) received a message: ${cmd}.`);
log.debug(`Worker (${process.pid}) received a message: ${cmd}.`);
let response = null;
switch (cmd) {
case 'health':
Expand All @@ -100,7 +102,7 @@ module.exports = {
this.httpServer.listen(monitorPort);
this.httpServer.on('request', requestHandler.bind(null, cluster));
this.httpServer.on('listening', () => {
log.info(
log.debug(
`Monitoring ${Object.keys(cluster.workers).length} workers on port ${
this.httpServer.address().port
}`
Expand All @@ -111,7 +113,7 @@ module.exports = {
stop: () => {
if (this.httpServer) {
this.httpServer.close();
log.info('Monitoring has been stopped.');
log.debug('Monitoring has been stopped.');
}
}
}
Expand Down

0 comments on commit fa841f5

Please sign in to comment.