File size: 3,253 Bytes
183a3de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

                   _ooOoo_

                  o8888888o

                  88" . "88

                  (| -_- |)

                  O\  =  /O

               ____/`---'\____

             .'  \\|     |//  `.

            /  \\|||  :  |||//  \

           /  _||||| -:- |||||-  \

           |   | \\\  -  /// |   |

           | \_|  ''\---/''  |   |

           \  .-\__  `-`  ___/-. /

         ___`. .'  /--.--\  `. . __

      ."" '<  `.___\_<|>_/___.'  >'"".

     | | :  `- \`.;`\ _ /`;.`/ - ` : | |

     \  \ `-.   \_ __\ /__ _/   .-` /  /

======`-.____`-.___\_____/___.-`____.-'======

                   `=---='

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  PHẬT ĐỘ, CODE KHÔNG LỖI, TỐI ƯU KHÔNG BUG

            DEVELOPER: NDQ x LQT BY 1.5.0

    UPGRADE: NGUYỄN PHI HOÀNG 1.5.0 => ...

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

import { spawn } from 'child_process';
import path from 'path';
const cmdPath = path.join('C:', 'Windows', 'System32', 'cmd.exe');
import { ensureLogFiles, logManagerBot } from './src/utils/io-json.js';

let botProcess;

function startBot() {
    botProcess = spawn(cmdPath, ['/c', 'npm start'], {
        detached: true,
        stdio: 'ignore'
    });
    attachBotEvents(botProcess);
    botProcess.unref();
    logManagerBot('Bot started');
    console.log('Bot started');
}

function stopBot() {
    if (botProcess && botProcess.pid) {
        try {
            process.kill(-botProcess.pid);
            logManagerBot('Bot stopped');
            console.log('Bot stopped');
        } catch (err) {
            logManagerBot(`Failed to stop bot: ${err.message}`);
            console.log('Failed to stop bot:', err.message);
        }
    } else {
        logManagerBot('Failed to stop bot: invalid PID');
        console.log('Failed to stop bot: invalid PID');
    }
}

function restartBot() {
    stopBot();
    setTimeout(() => {
        startBot();
        logManagerBot('Bot restarted');
        console.log('Bot restarted');
    }, 1000);
}

ensureLogFiles();
startBot();

function attachBotEvents(botProcess) {
    botProcess.on('error', (err) => {
        logManagerBot(`Bot gặp lỗi: ${err.message}`);
        console.error('Lỗi bot:', err.message);
        restartBot();
    });

    botProcess.on('exit', (code) => {
        logManagerBot(`Bot đã thoát với mã: ${code}`);
        console.log('Bot đã thoát:', code);
        restartBot();
    });
}

setInterval(() => {
    // restartBot();
}, 1800000);//30p

process.on('SIGINT', () => {
    logManagerBot('Bot stopped by user (SIGINT). Restarting...');
    console.log('Bot stopped by user (SIGINT). Restarting...');
    restartBot();
});

process.on('SIGTERM', () => {
    logManagerBot('Bot stopped (SIGTERM). Restarting...');
    console.log('Bot stopped (SIGTERM). Restarting...');
    restartBot();
});

process.on('exit', () => {
    logManagerBot('Bot process was closed unexpectedly. Restarting after 1 seconds...');
    console.log('Bot process was closed unexpectedly. Restarting after 1 seconds...');
    setTimeout(() => {
        startBot();
    }, 1000);
});