Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit fc62b2c

Browse files
committed
Auto Restart Processes
1 parent fd52fe0 commit fc62b2c

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/scanner.mjs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const getColor = (data, mode) => {
4646
}
4747
case "status":
4848
if (data.extra > 5) {
49-
return colors[0] + "Error"
49+
return colors[0] + "Down"
5050
} else {
5151
if (data.data === 0) {
5252
return colors[2] + "Idle"
@@ -71,7 +71,7 @@ const Main = async () => {
7171
for (var i = 0; i < os.availableParallelism(); i++) {
7272
processList.push([cprocess.fork("./libs/thread.mjs", [i]), []])
7373
logger.debug("Process: " + processList[i][0].pid + " started")
74-
assignedList.push([i, { currentIp: "", lastresponsetext: 0, status: "Idle", assigned: 0, finds: 0, total: 0, currentPings: 0 }])
74+
assignedList.push([i, { currentIp: "", lastresponsetext: 0, status: "Idle", assigned: 0, finds: 0, total: 0, currentPings: 0, restarts: 0 }])
7575
}
7676
for (var i = 0; i < processList.length; i++) {
7777
processList[i][0].on("message", (data) => {
@@ -86,29 +86,39 @@ const Main = async () => {
8686
assignedList[x][1].assigned--
8787
assignedList[x][1].lastresponsetext = parseInt(Date.now()) - data.time
8888
assignedList[x][1].currentIp = data.ip
89+
var y = processList[assignedList[x][0]][1].findIndex((sarray) => {
90+
const [number] = sarray
91+
return number === data.ip
92+
})
93+
processList[assignedList[x][0]][1].splice(y, 1)
8994
logger.debug(processList[assignedList[x][0]][0].pid + " processed data from ip: " + assignedList[x][1].currentIp);
9095
}
9196
else if (data.status === "error") {
9297
currData.assigned--
9398
assignedList[x][1].assigned--
9499
assignedList[x][1].lastresponsetext = parseInt(Date.now()) - data.time
95100
assignedList[x][1].currentIp = data.ip
101+
var y = processList[assignedList[x][0]][1].findIndex((sarray) => {
102+
const [number] = sarray
103+
return number === data.ip
104+
})
105+
processList[assignedList[x][0]][1].splice(y, 1)
96106
logger.debug(processList[assignedList[x][0]][0].pid + " failed to processing ip: " + assignedList[x][1].currentIp);
97107
}
98108
else if (data.status === "ping") {
99109
assignedList[x][1].currentPings = 0
100-
logger.info(processList[assignedList[x][0]][0].pid + " responded to ping request.")
110+
logger.debug(processList[assignedList[x][0]][0].pid + " responded to ping request.")
101111
}
102112
})
103113
}
104114
console.log("Parser will be started in 3 seconds...")
105115
setTimeout(() => {
106-
//startParser('java -Xmx5G -jar ' + process.cwd() + '/libs/copenJSParser-1.0-SNAPSHOT-5ms.jar')
116+
startParser('java -Xmx5G -jar ' + process.cwd() + '/libs/copenJSParser-1.0-SNAPSHOT-5ms.jar')
107117
setInterval(async () => {
108118
assignedList.sort((a, b) => { return a[0] - b[0] })
109119
let defaultText = `Main Process: \n Current IP/s: ${currData.total - currData.totalLast} IP/s \n Total Assigned: ${currData.assigned} \n Total TCP Restarts: ${currData.tcpRestarts} \n Total Finds & Total Try: ${currData.finds} & ${currData.total} \n Current Rate: ${((currData.finds / currData.total) * 100).toFixed(2)}% \n Total Sub-Processes: ${currData.subprocesses} \n Current IP: ${currData.lastIp} \nSub-Processes: `
110120
assignedList.forEach(async (data, n) => {
111-
defaultText += "\n Process processNumber: Current IP: currentIP Last Response: lastResponseText Status: statusText Total Assigned: assignedInt Total Finds: findInt ".replace("processNumber", n.toString()).replace("findInt", data[1].finds).replace("assignedInt", data[1].assigned).replace("currentIP", data[1].currentIp).replace("lastResponseText", `${getColor(data[1].lastresponsetext, "ms")}${data[1].lastresponsetext}ms\x1b[0m`).replace("statusText", `${getColor({ data: data[1].assigned, extra: data[1].currentPings }, "status")}\x1b[0m`)
121+
defaultText += "\n Process processNumber: Current IP: currentIP Last Response: lastResponseText Status: statusText Total Assigned: assignedInt Total Finds: findInt Restarts: restartInt Responseless Pings: pingInt ".replace("processNumber", n.toString()).replace("findInt", data[1].finds).replace("assignedInt", data[1].assigned).replace("currentIP", data[1].currentIp).replace("lastResponseText", `${getColor(data[1].lastresponsetext, "ms")}${data[1].lastresponsetext}ms\x1b[0m`).replace("statusText", `${getColor({ data: data[1].assigned, extra: data[1].currentPings }, "status")}\x1b[0m`).replace("restartInt", data[1].restarts).replace("pingInt", data[1].currentPings)
112122
})
113123
text.text = defaultText
114124
currData.totalLast = currData.total
@@ -127,13 +137,32 @@ const Main = async () => {
127137
}
128138
}
129139
}, 10000)
140+
setInterval(async () => {
141+
for (var i = 0; i < processList.length; i++) {
142+
if (assignedList[i][1].currentPings > 5) {
143+
logger.info("Restarting Thread: " + assignedList[i][0]),
144+
processList[assignedList[i][0]][0].kill("SIGINT")
145+
processList[assignedList[i][0]][0] = cprocess.fork("./libs/thread.mjs", [assignedList[i][0]])
146+
assignedList[i][1].restarts++
147+
processList[assignedList[i][0]][1].shift()
148+
assignedList[i][1].assigned--
149+
processList[assignedList[i][0]][1].forEach((data) => {
150+
processList[assignedList[i][0]][0].send({ mode: "search", ip: data, time: Date.now() })
151+
processList[assignedList[i][0]][1].shift()
152+
assignedList[i][1].assigned--
153+
});
154+
assignedList[i][1].currentPings = 0
155+
}
156+
}
157+
}, 30000)
130158
}, 3000)
131159
})
132160
server.on("connection", function (socket) {
133161
socket.on('error', function (err) {
134162
console.log("Client lost connection")
135163
logger.warn("The client lost the connection " + err)
136164
startParser('java -Xmx5G -jar ' + process.cwd() + '/libs/copenJSParser-1.0-SNAPSHOT-5ms.jar "' + currData.lastIp + '"')
165+
currData.tcpRestarts++
137166
})
138167
socket.on('end', function () {
139168
console.log("Client disconnected")
@@ -150,6 +179,7 @@ const Main = async () => {
150179
}
151180
else {
152181
processList[assignedList[x][0]][0].send({ mode: "search", ip: ip.toString(), time: Date.now() })
182+
processList[assignedList[x][0]][1].push(ip.toString())
153183
logger.debug(processList[assignedList[x][0]][0].pid + " assigned to " + ip.toString());
154184
assignedList[x][1].assigned++
155185
assignedList[x][1].total++

0 commit comments

Comments
 (0)