-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.mjs
123 lines (101 loc) · 3.64 KB
/
main.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
import cluster from 'cluster';
import os from 'os';
import path from 'path';
import Service from './service';
import git from 'simple-git';
import config from './config';
import logger from './logger';
const log = logger("main")
const MESSAGE_UPDATEDREPO = "UpdatedRepo"
process.on('unhandledRejection', (err) => {
log.Error(err);
process.exit(1);
});
let children = []
function childMsgHandler(msg) {
log.Debug("Master recieved a message: " + JSON.stringify(msg))
children.forEach(child => {
child.send(msg);
});
}
function startChild(env) {
var worker = cluster.fork(env)
worker.on('message', childMsgHandler)
return worker;
}
if (cluster.isMaster) {
log.Trace("Master started: " + JSON.stringify(config))
var debug = process.execArgv.indexOf('--debug') !== -1;
cluster.setupMaster({
execArgv: process.execArgv.filter(function (s) { return s !== '--debug' })
});
let numCPUs = os.cpus().length;
let numThreads = numCPUs;
log.Debug(`CPU Count: ${numCPUs}, Thread Count: ${numThreads}`)
for (let i = 0; i < numThreads; i++) {
children.push(startChild())
}
let gitWorker = startChild({ "CyberSaucier_Mode": "gitonly" })
cluster.on('exit', (worker, code, signal) => {
log.Debug(`worker ${worker.process.pid} died, restarting`);
if (worker.process.pid == gitWorker.process.pid) {
gitWorker = startChild({ "CyberSaucier_Mode": "gitonly" })
} else {
var workerIdx = children.indexOf(worker);
if (workerIdx > -1) {
children.splice(workerIdx, 1)
}
children.push(startChild())
}
});
} else {
log.Info("Creating Service")
let s = new Service()
const IsGitMode = process.env.CyberSaucier_Mode || false;
if (IsGitMode) {
const gitrun = async (srv) => {
log.Trace("Initializing Git")
await s.InitGit()
process.send({ "MESSAGE": MESSAGE_UPDATEDREPO })
const recipeFolder = path.resolve(config.RecipeFolder);
const doGitCheck = async() => {
log.Debug("Checking for updates from git repo")
git(recipeFolder).silent(true).pull((err, update) => {
if (err) {
log.Error("ERROR pulling from git: " + JSON.stringify(err))
}
if (update && update.files && update.files.length > 0) {
process.send({ "MESSAGE": MESSAGE_UPDATEDREPO, "Update": update })
} else {
log.Debug("No changes to git repo")
}
setTimeout(doGitCheck, config.GitInterval)
})
}
if (config.GitInterval > 0) {
setTimeout(doGitCheck, config.GitInterval)
}
}
if (config.RecipeGit && config.RecipeGit.length > 0) {
gitrun(s)
}
} else {
const run = async (srv) => {
process.on('message', function (msg) {
if (msg && msg.MESSAGE && msg.MESSAGE === MESSAGE_UPDATEDREPO) {
let update = msg.Update
srv.UpdateRecipies(update);
}
})
log.Debug("Initializing Service")
await srv.Init()
if (!config.RecipeGit || config.RecipeGit.length < 1) {
await srv.InitGit()
}
log.Info("Starting Service")
await srv.Start()
}
run(s)
}
}