-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (75 loc) · 2.32 KB
/
index.js
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
const schedule = require('node-schedule');
const qrcode = require("qrcode-terminal");
const { Client, LocalAuth } = require("whatsapp-web.js");
const puppeteer = require('puppeteer');
const { exec } = require('child_process');
const config = require('./config');
class WhatsAppService {
constructor() {
this.client = new Client({
authStrategy: new LocalAuth({ dataPath: config.dataPath }),
puppeteer: {
executablePath: puppeteer.executablePath(),
headless: true,
args: config.puppeteerArgs,
},
});
this.initializeClient();
}
initializeClient() {
this.client.initialize();
this.client.on("qr", (qr) => {
qrcode.generate(qr, { small: true });
});
}
ready() {
return new Promise((resolve) => {
this.client.on('ready', () => {
console.log("WhatsApp Watchdog is ready!");
console.log("Starting to monitor " + config.serviceName + " and sending a message to " + config.chatId + " in case of failure...");
resolve();
});
});
}
sendMessage(chatId, message) {
this.client.sendMessage(chatId, message).catch(error => {
console.error("Error sending message:", error);
});
}
}
class WindowsSystemMonitor {
static checkPythonService(callback) {
const psCommand = `get-wmiobject Win32_process -filter "Name='python.exe'" | foreach-object {$_.CommandLine}`;
exec(`powershell.exe -command "${psCommand.replace(/"/g, '\\"')}"`, callback);
}
}
async function main() {
console.log("Starting WhatsApp service...");
const whatsAppService = new WhatsAppService();
await whatsAppService.ready();
function monitorService() {
WindowsSystemMonitor.checkPythonService((err, stdout, stderr) => {
if (err) {
console.error("Error:", err);
return;
}
if (stderr) {
console.error("Stderr:", stderr);
return;
}
if (!stdout.includes(config.serviceName)) {
console.log("Service is not running");
whatsAppService.sendMessage(
config.chatId,
config.serviceName + " is not running, please check!"
);
console.log("Message sent to " + config.chatId);
} else {
console.log("Service is running");
}
});
}
monitorService();
schedule.scheduleJob(config.schedule, monitorService);
}
main();