puppyperpetual 1.2.6 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -58
- package/package.json +1 -1
- package/src/ConfigManager.js +4 -2
- package/src/ProcessManager.js +53 -6
package/README.md
CHANGED
|
@@ -1,59 +1,57 @@
|
|
|
1
|
-
# puppyperpetual
|
|
2
|
-
|
|
3
|
-
node script to run things... perpetually. adapted from [legacyshell](https://github.com/onlypuppy7/LegacyShell)
|
|
4
|
-
|
|
5
|
-
[On npm @ https://www.npmjs.com/package/puppyperpetual](https://www.npmjs.com/package/puppyperpetual)
|
|
6
|
-
|
|
7
|
-
[And GitHub @ https://github.com/onlypuppy7/perpetual](https://github.com/onlypuppy7/perpetual)
|
|
8
|
-
|
|
9
|
-
## usage (as an npm package)
|
|
10
|
-
|
|
11
|
-
you can use it to run your node stuff perpetually:
|
|
12
|
-
|
|
13
|
-
```js
|
|
14
|
-
import Perpetual from '../index.js';
|
|
15
|
-
|
|
16
|
-
const perpetual = new Perpetual('test_process', {
|
|
17
|
-
process_cmd: 'node ./src/test/test_process.js',
|
|
18
|
-
});
|
|
19
|
-
await perpetual.run();
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
or anything really:
|
|
23
|
-
|
|
24
|
-
```js
|
|
25
|
-
import Perpetual from '../index.js';
|
|
26
|
-
|
|
27
|
-
const perpetual = new Perpetual('test_process', {
|
|
28
|
-
process_cmd: 'cd . && git pull && date',
|
|
29
|
-
});
|
|
30
|
-
await perpetual.run();
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## usage (as a node app thing)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
then
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
## it doesnt work?
|
|
58
|
-
|
|
1
|
+
# puppyperpetual
|
|
2
|
+
|
|
3
|
+
node script to run things... perpetually. adapted from [legacyshell](https://github.com/onlypuppy7/LegacyShell)
|
|
4
|
+
|
|
5
|
+
[On npm @ https://www.npmjs.com/package/puppyperpetual](https://www.npmjs.com/package/puppyperpetual)
|
|
6
|
+
|
|
7
|
+
[And GitHub @ https://github.com/onlypuppy7/perpetual](https://github.com/onlypuppy7/perpetual)
|
|
8
|
+
|
|
9
|
+
## usage (as an npm package)
|
|
10
|
+
|
|
11
|
+
you can use it to run your node stuff perpetually:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import Perpetual from '../index.js';
|
|
15
|
+
|
|
16
|
+
const perpetual = new Perpetual('test_process', {
|
|
17
|
+
process_cmd: 'node ./src/test/test_process.js',
|
|
18
|
+
});
|
|
19
|
+
await perpetual.run();
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
or anything really:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import Perpetual from '../index.js';
|
|
26
|
+
|
|
27
|
+
const perpetual = new Perpetual('test_process', {
|
|
28
|
+
process_cmd: 'cd . && git pull && date',
|
|
29
|
+
});
|
|
30
|
+
await perpetual.run();
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## usage (as a node app thing)
|
|
34
|
+
|
|
35
|
+
as per tradition for my projects, simple installation!
|
|
36
|
+
|
|
37
|
+
1. `npm i`
|
|
38
|
+
|
|
39
|
+
then run once to create config
|
|
40
|
+
|
|
41
|
+
- `npm run start`
|
|
42
|
+
|
|
43
|
+
then customise it in `store/config.yaml`, making a new entry for every thing
|
|
44
|
+
|
|
45
|
+
then run like this:
|
|
46
|
+
|
|
47
|
+
- `node .\perpetual.js --default`
|
|
48
|
+
|
|
49
|
+
alternatively, just pass in your command:
|
|
50
|
+
|
|
51
|
+
- `node .\perpetual.js "cd . && echo date"`
|
|
52
|
+
|
|
53
|
+
then you dont need to mess around with the horrible yaml. dont worry, i hate doing it too.
|
|
54
|
+
|
|
55
|
+
## it doesnt work?
|
|
56
|
+
|
|
59
57
|
i dont care. i use this for my own stuff. i know that it works on linux and thats all i need.
|
package/package.json
CHANGED
package/src/ConfigManager.js
CHANGED
|
@@ -12,7 +12,7 @@ export class ConfigManager {
|
|
|
12
12
|
const __dirname = path.dirname(__filename);
|
|
13
13
|
|
|
14
14
|
this.defaultConfigPath = path.join(__dirname, 'defaultconfig.yaml');
|
|
15
|
-
this.configPath = path.join(
|
|
15
|
+
this.configPath = path.join(perpConfigLocation);
|
|
16
16
|
|
|
17
17
|
if (!noYAML) {
|
|
18
18
|
this.ensureConfigExists();
|
|
@@ -41,6 +41,7 @@ export class ConfigManager {
|
|
|
41
41
|
if (cdMatch) {
|
|
42
42
|
console.log("Detected 'cd' command in process_cmd:", cdMatch[1]);
|
|
43
43
|
passed.dir = cdMatch[1];
|
|
44
|
+
// passed.process_cmd = cdMatch[2];
|
|
44
45
|
};
|
|
45
46
|
};
|
|
46
47
|
}
|
|
@@ -56,7 +57,7 @@ export class ConfigManager {
|
|
|
56
57
|
dailyrestart_quickpull: passed.dailyrestart_quickpull,
|
|
57
58
|
//file logging
|
|
58
59
|
logfile_enable: passed.logfile_enable,
|
|
59
|
-
logfile_location: passed.logfile_location || path.join(
|
|
60
|
+
logfile_location: passed.logfile_location || path.join("store", "logs", this.serverName), //no editing kek
|
|
60
61
|
//webhook logging
|
|
61
62
|
webhook_url: passed.webhook_url || "", //false or empty is disabled
|
|
62
63
|
webhook_username: passed.webhook_username || "Webhook", //eg "LegacyShell: Client Server"
|
|
@@ -65,6 +66,7 @@ export class ConfigManager {
|
|
|
65
66
|
webhook_ping_role: passed.webhook_ping_role || false, //this might flood EVERYONE'S shit
|
|
66
67
|
//pulling
|
|
67
68
|
is_puller: this.config.pullers?.includes(this.serverName) || false,
|
|
69
|
+
restart_on_update: passed.restart_on_update ?? true,
|
|
68
70
|
};
|
|
69
71
|
|
|
70
72
|
if (passed.process_cmd_prefix) passed.process_cmd = `${passed.process_cmd_prefix}${passed.process_cmd}`;
|
package/src/ProcessManager.js
CHANGED
|
@@ -13,6 +13,7 @@ export class ProcessManager {
|
|
|
13
13
|
this.runningProcess = null;
|
|
14
14
|
this.restartScheduled = false;
|
|
15
15
|
this.autoRestart();
|
|
16
|
+
this.pullerSetup();
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
async startProcess(purposefulStop = false) {
|
|
@@ -54,7 +55,7 @@ export class ProcessManager {
|
|
|
54
55
|
this.logger.logSend(`Starting process: ${this.options.process_cmd}`);
|
|
55
56
|
|
|
56
57
|
const useWorkerThreads = this.options.useWorkerThreads ?? false;
|
|
57
|
-
const isNodeScript = this.options.process_cmd.startsWith('node ') || this.options.
|
|
58
|
+
const isNodeScript = this.options.process_cmd.startsWith('node ') || this.options.isNodeScript;
|
|
58
59
|
let scriptPath = this.options.process_cmd;
|
|
59
60
|
|
|
60
61
|
if (isNodeScript && useWorkerThreads) {
|
|
@@ -114,13 +115,20 @@ export class ProcessManager {
|
|
|
114
115
|
|
|
115
116
|
} else {
|
|
116
117
|
let bash = 'bash';
|
|
117
|
-
let options = ['-c', 'export FORCE_COLOR=3;
|
|
118
|
+
let options = ['-c', 'export FORCE_COLOR=3;' + this.options.process_cmd];
|
|
118
119
|
if (process.platform === 'win32') {
|
|
119
|
-
bash = this.options.winBashPath || 'C:\\Program Files\\Git\\bin\\bash.exe';
|
|
120
|
+
// bash = this.options.winBashPath || 'C:\\Program Files\\Git\\bin\\bash.exe';
|
|
120
121
|
if (isNodeScript) {
|
|
121
122
|
bash = 'node';
|
|
122
123
|
options = scriptPath.replace(/^node\s+/, '').trim().split(' ');
|
|
123
124
|
// options.push("FORCE_COLOR=3");
|
|
125
|
+
} else {
|
|
126
|
+
//check if bash exists at the specified path
|
|
127
|
+
bash = this.options.winBashPath || 'C:\\Program Files\\Git\\bin\\bash.exe';
|
|
128
|
+
if (!fs.existsSync(bash)) {
|
|
129
|
+
bash = 'cmd.exe';
|
|
130
|
+
options = ['/c', this.options.process_cmd];
|
|
131
|
+
}
|
|
124
132
|
}
|
|
125
133
|
}
|
|
126
134
|
this.runningProcess = spawn(bash, options, {
|
|
@@ -166,7 +174,32 @@ export class ProcessManager {
|
|
|
166
174
|
}
|
|
167
175
|
}
|
|
168
176
|
|
|
169
|
-
|
|
177
|
+
async gitPull() {
|
|
178
|
+
this.logger.logSend("Performing git pull...");
|
|
179
|
+
this.executeCommand('git', ['pull'], 'ignore');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async getGitHash() {
|
|
183
|
+
return new Promise((resolve) => {
|
|
184
|
+
const gitProcess = spawn('git', ['rev-parse', 'HEAD'], { cwd: this.options.dir || process.cwd() });
|
|
185
|
+
let hash = '';
|
|
186
|
+
|
|
187
|
+
gitProcess.stdout.on('data', (data) => {
|
|
188
|
+
hash += data.toString();
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
gitProcess.on('close', (code) => {
|
|
192
|
+
if (code === 0) {
|
|
193
|
+
resolve(hash.trim());
|
|
194
|
+
} else {
|
|
195
|
+
this.logger.logSend(`git rev-parse exited with code ${code}`);
|
|
196
|
+
resolve(null);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async autoRestart() {
|
|
170
203
|
if (!this.options.dailyrestart_enable) return;
|
|
171
204
|
if (this.restartScheduled) {
|
|
172
205
|
this.logger.logSend("Restart already scheduled.");
|
|
@@ -194,8 +227,7 @@ export class ProcessManager {
|
|
|
194
227
|
|
|
195
228
|
setTimeout(async () => {
|
|
196
229
|
if (this.options.dailyrestart_quickpull) {
|
|
197
|
-
this.
|
|
198
|
-
this.executeCommand('git', ['pull'], 'ignore');
|
|
230
|
+
await this.gitPull();
|
|
199
231
|
}
|
|
200
232
|
this.logger.logSend("Auto-restarting process.");
|
|
201
233
|
await this.startProcess();
|
|
@@ -204,6 +236,21 @@ export class ProcessManager {
|
|
|
204
236
|
}, timeUntilRestart);
|
|
205
237
|
}
|
|
206
238
|
|
|
239
|
+
async pullerSetup() {
|
|
240
|
+
this.currentHash = await this.getGitHash();
|
|
241
|
+
setInterval(async () => {
|
|
242
|
+
if (this.options.is_puller) {
|
|
243
|
+
await this.gitPull();
|
|
244
|
+
}
|
|
245
|
+
const newHash = await this.getGitHash();
|
|
246
|
+
if (newHash && newHash !== this.currentHash && this.config.restart_on_update) {
|
|
247
|
+
this.logger.logSend(`Git hash changed from ${this.currentHash} to ${newHash}. Restarting process.`);
|
|
248
|
+
this.currentHash = newHash;
|
|
249
|
+
await this.startProcess();
|
|
250
|
+
}
|
|
251
|
+
}, 60e3);
|
|
252
|
+
}
|
|
253
|
+
|
|
207
254
|
executeCommand(command, args, stdio = "inherit") {
|
|
208
255
|
let dir = this.options.dir || "";
|
|
209
256
|
|