SkeptiCOM/index.js

178 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-12-09 21:19:58 +01:00
/*
SkeptCOM | Ceci est l'index, c'est ici qu'on va load les process PM2
*/
// CONF
const skepticonf = require('./conf/skepticom.json')
// REQUIRE
const pm2 = require('pm2')
const path = require('path')
// FUNCTIONS
function buildAppName(name) {
return {
"name" : skepticonf.pm2.prefix + name,
"script" : "./services/" + name + ".js",
"cwd" : path.resolve(__dirname),
"instances" : 1,
"exec_mode" : skepticonf.pm2.mode,
"args" : [ "--color" ],
"watch" : false,
"ignore_watch" : ["node_modules", "conf", "db"],
"watch_options": {
"followSymlinks": true,
"persistent" : true,
"ignoreInitial" : true
}
}
}
// INIT
if (process.argv[2] && process.argv[2] === '--service') {
let service = process.argv[3]
let action = process.argv[4]
let options = process.argv.slice(5, process.argv.length)
// console.log(`[coucou toi]`, service, action, options) // DEV ONLY
if (service == 'list') {
pm2.list((err, list) => {
if (err) return console.log(`ERROR pm2.list()`)
if (list.length == 0) return console.log(` -- NO SERVICE RUNNING`)
for (let i in list) {
console.log(` -- SERVICE ${list[i].name.replace(skepticonf.pm2.prefix, '')} [WORKING] [${Number(i)+1}/${list.length}] `)
}
pm2.disconnect()
})
} else if (!service || !action) {
return console.error(`[ERROR] '--service NomDuService ActionDuService'`)
} else {
if (action == 'stop') {
pm2.list((err, list) => {
if (err) return console.error(`[ERROR:1.1] '--service NomDuService stop'`)
let servDesc
for (let i in list) {
if (list[i].name.replace(skepticonf.pm2.prefix, '') === service) {
servDesc = list[i]
}
}
if (servDesc != undefined) {
pm2.connect(err => {
if (err) console.error(err)
pm2.delete(servDesc.name, err => {
if (err) return console.error(`[ERROR:1.2] '--service NomDuService stop'`)
console.log(` -- SERVICE ${servDesc.name} [STOP]`)
pm2.disconnect()
})
})
} else {
console.log(` [INFO] This service is not load, Check '--service list' for more information`)
}
pm2.disconnect()
})
} else if (action === 'start') {
if(!skepticonf.services.includes(service)) return console.log(` [INFO] This service does not exist`)
pm2.list((err, list) => {
if (err) return console.error(`[ERROR:2.1] '--service NomDuService start'`)
let servDesc
for (let i in list) {
if (list[i].name.replace(skepticonf.pm2.prefix, '') === service) {
servDesc = list[i]
}
}
if (servDesc === undefined) {
let appName = buildAppName(service)
pm2.connect(err => {
if (err) console.error(err)
pm2.start(appName, err => {
if (err) return console.error(`[ERROR:2.2] '--service NomDuService start'`)
console.log(` -- SERVICE ${service} [LOAD]`)
pm2.disconnect()
})
})
} else {
console.log(` [INFO] This service is load, Check '--service list' for more information`)
}
pm2.disconnect()
})
} else if (action === 'restart') {
if(!skepticonf.services.includes(service)) return console.log(` [INFO] This service does not exist`)
pm2.list((err, list) => {
if (err) return console.error(`[ERROR:3.1] '--service NomDuService restart'`)
let servDesc
for (let i in list) {
if (list[i].name.replace(skepticonf.pm2.prefix, '') === service) {
servDesc = list[i]
}
}
if (servDesc != undefined) {
let appName = buildAppName(service)
pm2.connect(err => {
if (err) console.error(err)
pm2.restart(appName, err => {
if (err) return console.error(`[ERROR:3.2] '--service NomDuService restart'`)
console.log(` -- SERVICE ${service} [RELOAD]`)
pm2.disconnect()
})
})
} else {
console.log(` [INFO] This service is not load, Check '--service list' for more information`)
}
pm2.disconnect()
})
}
}
} else {
pm2.connect(err => {
if (err) console.error(err)
for (let i in skepticonf.services) {
let appName = buildAppName(skepticonf.services[i])
if (process.argv[2] === "stop") {
pm2.delete(appName.name, err => {
if (err) return console.log(` -- SERVICE ${skepticonf.services[i]} [UNLOADED] [${Number(i)+1}/${skepticonf.services.length}]`)
console.log(` -- SERVICE ${skepticonf.services[i]} [STOP] [${Number(i)+1}/${skepticonf.services.length}]`)
})
} else {
pm2.start(appName, err => {
if (err) return console.error(err)
console.log(` -- SERVICE ${skepticonf.services[i]} [LOAD] [${Number(i)+1}/${skepticonf.services.length}]`)
})
}
if (i == (skepticonf.services.length-1)) {
setTimeout(function(){
console.log(` -- ALL PROCESS FINISH -- \n`)
pm2.disconnect()
process.exit()
}, 2000)
}
}
})
}
module.exports = {
ListenerRssInfos: require('./src/Models/ListenerRSSInfos'),
ListenerRss: require('./src/ListenerRss')
}