Skip to content

Instantly share code, notes, and snippets.

View devarajchidambaram's full-sized avatar

devaraj devarajchidambaram

View GitHub Profile

Effective Engineer - Notes

What's an Effective Engineer?

  • They are the people who get things done. Effective Engineers produce results.

Adopt the Right Mindsets

@devarajchidambaram
devarajchidambaram / fs.js
Last active January 22, 2018 04:42
Access File descriptor in nodejs
const fs = require('fs')
fs.open('./index.js', 'r', (err, fd) => {
//Access fd here
if(err) return "error while open FD";
fs.readFile(fd, function(err, msg) {
if(err) return "error while read file";
console.log('msg===', msg.toString())
@devarajchidambaram
devarajchidambaram / nodejs_cluster.js
Last active January 23, 2018 05:11
assign separate server/port to each worker?
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
var worker_id = '';
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
worker_id = '900'+i;
@devarajchidambaram
devarajchidambaram / recluster.js
Created February 1, 2018 13:16
To gently reload the config in nodejs
//https://www.npmjs.com/package/recluster
var recluster = require('recluster'),
path = require('path');
var cluster = recluster(path.join(__dirname, 'server.js'),{workers : 2});
cluster.run();
process.on('SIGUSR2', function() {
console.log('Got SIGUSR2, reloading cluster...');
@devarajchidambaram
devarajchidambaram / recluster.js
Created February 1, 2018 13:16
To gently reload the config in nodejs
//https://www.npmjs.com/package/recluster
var recluster = require('recluster'),
path = require('path');
var cluster = recluster(path.join(__dirname, 'server.js'),{workers : 2});
cluster.run();
process.on('SIGUSR2', function() {
console.log('Got SIGUSR2, reloading cluster...');
@devarajchidambaram
devarajchidambaram / Cluster.js
Created February 2, 2018 05:46
Nodejs cluster up the worker process if it fails
var http = require('http');
var cluster = require('cluster');
// The master's job to spawn workers initially and when they die
if (cluster.isMaster) {
// Get the number of processor cores
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
@devarajchidambaram
devarajchidambaram / Proxy.js
Created March 15, 2018 05:14
Proxy in nodejs
let validator = {
set: function name(Obj, param, value) {
if(param === 'age'){
if(! Number.isInteger(value)) throw new Error("age is not an number type")
if( !(value > 18 && value < 80)){
throw Error("age range error, age must between 18 to 80 ");
}
}
@devarajchidambaram
devarajchidambaram / D3.js
Created March 15, 2018 05:19
tried d3 js
var svgWidth = 800;
var svgHeight = 300;
var margin = {top: 30, right: 40, bottom: 50, left: 60};
var width = svgWidth - margin.left - margin.right;
var height = svgHeight - margin.top - margin.bottom;
var originalCircle = {"cx" : -150 ,
"cy" : -15 ,
@devarajchidambaram
devarajchidambaram / customer.sql
Created March 15, 2018 05:22
Customer table sample data
This file has been truncated, but you can view the full file.
-- phpMyAdmin SQL Dump
-- version 4.0.9
-- http://www.phpmyadmin.net
--
-- Inang: localhost
-- Waktu pembuatan: 02 Mei 2014 pada 12.05
-- Versi Server: 5.5.34
-- mysql -u username -p database_name < file.sql
--create table customer (
@devarajchidambaram
devarajchidambaram / eventloopblocked.js
Created March 20, 2018 09:56
To check eventloop blocked time
module.exports.eventLoopblocked = function(func, opts){
var option = opts | {};
var threshold = opts.threshold | 10;
var interval = opts.threshold | 100;
var startTime= process.hrtime();
var delta;
var timer = setInterval(function(){