Skip to content

Instantly share code, notes, and snippets.

@ivanseidel
Created March 9, 2017 14:00
Show Gist options
  • Save ivanseidel/5aa5bf2b20147dab959c7de23d8f41ff to your computer and use it in GitHub Desktop.
Save ivanseidel/5aa5bf2b20147dab959c7de23d8f41ff to your computer and use it in GitHub Desktop.
DraftLog cool examples
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