Created
March 9, 2017 14:00
-
-
Save ivanseidel/5aa5bf2b20147dab959c7de23d8f41ff to your computer and use it in GitHub Desktop.
DraftLog cool examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const async = require('async') | |
const chalk = require('chalk') | |
const TAG = chalk.green('[PM2]') | |
require('draftlog').into(console) | |
/* | |
* Statuses is an array of each server status. | |
*/ | |
function ProcessStatus(name, statuses) { | |
let NAME = `[${name}]` | |
let STATUS = statuses.map(colorize) | |
let PAD = ' '.repeat(16 - NAME.length) | |
let SPACER = chalk.dim('▇') | |
let started = statuses.filter(s => s == 'STARTED') | |
return `${TAG} ${NAME}${PAD} ${started.length}/${statuses.length} ${STATUS.join('')}` | |
} | |
// Colorize a given status | |
function colorize(s) { | |
return { | |
STARTED: chalk.green('▇▇'), | |
RESTARTING: chalk.blue('▇▇'), | |
WAITING: chalk.red('▇▇'), | |
}[s] | |
} | |
var processes = { | |
test: ['WAITING', 'WAITING', 'WAITING', 'WAITING', 'WAITING', 'WAITING', 'WAITING', 'WAITING', 'WAITING'], | |
app: ['WAITING', 'WAITING', 'WAITING', 'WAITING', 'WAITING'], | |
app2: ['WAITING', 'WAITING', 'WAITING', 'WAITING', 'WAITING'], | |
app3: ['WAITING', 'WAITING', 'WAITING', 'WAITING', 'WAITING'], | |
app5: ['WAITING', 'WAITING', 'WAITING'], | |
} | |
function restartProcess(statuses, name, next) { | |
let log = console.draft(ProcessStatus(name, statuses)) | |
async.timesSeries(statuses.length, (n, next) => { | |
statuses[n] = 'RESTARTING' | |
log(ProcessStatus(name, statuses)) | |
restartProcessChild(() => { | |
statuses[n] = 'STARTED' | |
log(ProcessStatus(name, statuses)) | |
next() | |
}) | |
}, next) | |
} | |
function restartProcessChild(callback) { | |
setTimeout(callback, 100 + Math.random() * 1000) | |
} | |
console.log() | |
console.log() | |
console.log(TAG, 'Applying action restartProcessId on app [all]') | |
async.mapValuesLimit(processes, 2, restartProcess) | |
process.on('exit', () => { | |
console.log() | |
console.log() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment