Skip to content

Commit

Permalink
chore: prestart compact
Browse files Browse the repository at this point in the history
  • Loading branch information
a632079 committed Aug 27, 2020
1 parent 1e7eb92 commit 924ef14
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
2 changes: 1 addition & 1 deletion adapter/processes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {
from: 'cronJob',
listener: (targetDB) => {
winston.verbose(
'[AB] receiving signal, switching to db: ' + chalk.yellow(targetDB),
'[AB] received signal, switching to db: ' + chalk.yellow(targetDB),
)
AB.setDatabase(targetDB)
},
Expand Down
6 changes: 3 additions & 3 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ const winston = require('winston')
// const path = require('path')
// const pkg = require('./package.json')
// const mail = require('./src/mail')
const preStart = require('./src/prestart')
preStart.check() // Check Env

// Read Command
const commander = require('./src/commander')
const program = commander.process()

// PreStart
const preStart = require('./src/prestart')
preStart.check()
preStart.load(program.config_file || null)
if (program.dev) {
winston.verbose('[debug] you are running at Development mode.')
winston.level = 'verbose'
nconf.set('dev', true)
}

// Use blubird promise
Expand Down
3 changes: 0 additions & 3 deletions src/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,9 @@ class Cron {
}

require('./prestart').load(null, true)

if (process.env && process.env.dev === 'true') {
winston.level = 'verbose'
nconf.set('dev', true)
}

process.on('exit', (code) => {
if (code && code === 1000) {
winston.info('[cronJob] receiving exiting signal, cronJob process exits.')
Expand Down
35 changes: 13 additions & 22 deletions src/prestart.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ const dirname = path.join(__dirname, '../')
async function setupWinston() {
const logFile =
nconf.get('log_path') ||
path.join(__dirname, '../', './data/logs/', pkg.name + '.log')
path.join(__dirname, '../', './data/logs/', pkg.name + '_error.log')

// createDir while running at docker
const dirPath = path.join(logFile, '../')
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath)
}
if (!fs.existsSync(dirPath)) fs.mkdirSync(dirPath)

fs.existsSync(logFile) || fs.writeFileSync(logFile, '')
winston.remove(winston.transports.Console)
winston.add(winston.transports.File, {
filename: logFile,
level:
nconf.get('log_level') ||
(global.env === 'production' ? 'info' : 'verbose'),
level: 'error',
handleExceptions: true,
maxsize: 5242880,
maxFiles: 10,
Expand All @@ -37,8 +35,9 @@ async function setupWinston() {
: date.toISOString() + ' [' + global.process.pid + ']'
},
level:
nconf.get('log_level') ||
(global.env === 'production' ? 'info' : 'verbose'),
!nconf.get('dev') || !(process.env && process.env.dev === 'true')
? 'info'
: 'verbose',
json: !!nconf.get('json_logging'),
stringify: !!nconf.get('json_logging'),
})
Expand All @@ -48,9 +47,9 @@ function loadConfig(configFile, isChild = false, next) {
nconf.use('memory') // use memory store
nconf.argv().env() // 从参数中读取配置,并写入 nconf
// check config file while running at dokcer
if (!fs.existsSync(configFile)) {
if (!fs.existsSync(configFile))
fs.copyFileSync(path.join(__dirname, '../config.example.json'), configFile)
}

nconf.file({
file: configFile,
})
Expand All @@ -59,11 +58,7 @@ function loadConfig(configFile, isChild = false, next) {
base_dir: dirname,
version: pkg.version,
})

if (!nconf.get('isCluster')) {
nconf.set('isPrimary', 'true')
nconf.set('isCluster', 'false')
}
nconf.set('dev', !global.prod) // Inject Dev option
if (next && typeof next === 'function') {
Promise.resolve(next()).then(() => {
// Print logger
Expand Down Expand Up @@ -122,13 +117,9 @@ function check() {

module.exports = {
load: (configFile, isChild = false) => {
if (!configFile) {
if (!configFile)
configFile = path.join(__dirname, '../data', './config.json')
}
if (!isChild) {
printCopyright()
}
winston.level = 'info'
if (!isChild) printCopyright()
loadConfig(configFile, isChild, setupWinston)
},
check,
Expand Down

0 comments on commit 924ef14

Please sign in to comment.