puppyperpetual 1.0.1 → 1.0.2
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/package.json +1 -1
- package/src/Logger.js +8 -42
- package/src/index.js +0 -1
- package/src/test/test.js +1 -0
package/package.json
CHANGED
package/src/Logger.js
CHANGED
|
@@ -2,6 +2,7 @@ import fs from 'node:fs';
|
|
|
2
2
|
import fetch from 'node-fetch';
|
|
3
3
|
import { getTimestamp, stripAnsi, divideString, ensureDirExists } from 'puppymisc';
|
|
4
4
|
import log from 'puppylog';
|
|
5
|
+
import PuppyWebhook from 'puppywebhook';
|
|
5
6
|
|
|
6
7
|
export class Logger {
|
|
7
8
|
constructor(options) {
|
|
@@ -11,6 +12,12 @@ export class Logger {
|
|
|
11
12
|
this.maxMessageLength = 1900;
|
|
12
13
|
this.messagesSent = 0;
|
|
13
14
|
|
|
15
|
+
if (options.webhook_url) this.webhook = new PuppyWebhook({
|
|
16
|
+
webhookUrl: options.webhook_url,
|
|
17
|
+
username: options.webhook_username,
|
|
18
|
+
avatar_url: options.webhook_avatar,
|
|
19
|
+
});
|
|
20
|
+
|
|
14
21
|
ensureDirExists(options.logfile_location);
|
|
15
22
|
this.logFilePath = `${options.logfile_location}/${getTimestamp(true)}.log`;
|
|
16
23
|
ensureDirExists(this.logFilePath);
|
|
@@ -18,7 +25,7 @@ export class Logger {
|
|
|
18
25
|
|
|
19
26
|
appendLog(msg, noSend = false) {
|
|
20
27
|
msg = stripAnsi(msg);
|
|
21
|
-
if (!noSend) this.
|
|
28
|
+
if (this.webhook && !noSend) this.webhook.send(msg)
|
|
22
29
|
fs.appendFile(this.logFilePath, `${msg}\n`, () => {});
|
|
23
30
|
}
|
|
24
31
|
|
|
@@ -33,45 +40,4 @@ export class Logger {
|
|
|
33
40
|
log.muted(line);
|
|
34
41
|
this.appendLog(line, true);
|
|
35
42
|
}
|
|
36
|
-
|
|
37
|
-
async sendToWebhook() {
|
|
38
|
-
while (this.logQueue.length) {
|
|
39
|
-
let msg = this.logQueue.shift();
|
|
40
|
-
if (msg.length > this.maxMessageLength) {
|
|
41
|
-
this.logQueue.unshift(...divideString(msg, this.maxMessageLength));
|
|
42
|
-
} else {
|
|
43
|
-
const lastChunk = this.queuedChunks[0] || "";
|
|
44
|
-
const newChunk = lastChunk + `\n${msg}`;
|
|
45
|
-
if (newChunk.length > this.maxMessageLength) {
|
|
46
|
-
this.queuedChunks.unshift(`\n${msg}`);
|
|
47
|
-
} else {
|
|
48
|
-
this.queuedChunks[0] = newChunk;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (this.queuedChunks.length && this.options.webhook_url) {
|
|
54
|
-
try {
|
|
55
|
-
const response = await fetch(this.options.webhook_url, {
|
|
56
|
-
method: 'POST',
|
|
57
|
-
headers: { 'Content-Type': 'application/json' },
|
|
58
|
-
body: JSON.stringify({
|
|
59
|
-
username: this.options.webhook_username,
|
|
60
|
-
avatar_url: this.options.webhook_avatar,
|
|
61
|
-
content: this.queuedChunks[this.queuedChunks.length - 1].slice(0, 2000),
|
|
62
|
-
})
|
|
63
|
-
});
|
|
64
|
-
if (!response.ok) throw new Error(response.statusText);
|
|
65
|
-
this.logNoSend(`Logs sent to webhook (${this.messagesSent})`);
|
|
66
|
-
this.queuedChunks.pop();
|
|
67
|
-
this.messagesSent++;
|
|
68
|
-
} catch (err) {
|
|
69
|
-
this.logNoSend(`Error sending webhook: ${err.message}`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
startWebhookInterval(interval = 15000) {
|
|
75
|
-
this.webhookInterval = setInterval(() => this.sendToWebhook(), interval);
|
|
76
|
-
}
|
|
77
43
|
}
|
package/src/index.js
CHANGED
package/src/test/test.js
CHANGED
|
@@ -3,5 +3,6 @@ import Perpetual from '../index.js';
|
|
|
3
3
|
const perpetual = new Perpetual('test_process', {
|
|
4
4
|
// process_cmd: 'node ./src/test/test_process.js',
|
|
5
5
|
process_cmd: 'date',
|
|
6
|
+
webhook_url: 'https://discord.com/api/webhooks/1460782220192256031/Fkibsumk3hWg4M6F2eCmDe_ylXaJlY_z3W9XeCMWbiw4r8zcYfQJg64YeIhaHT-7tpbT',
|
|
6
7
|
})
|
|
7
8
|
await perpetual.run();
|