puppyperpetual 1.0.1 → 1.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "puppyperpetual",
3
3
  "type": "module",
4
- "version": "v1.0.1",
4
+ "version": "1.0.3",
5
5
  "description": "Run stuff FOREVER! PERPETUALLY!",
6
6
  "main": "perpetual.js",
7
7
  "keywords": [
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.logQueue.push(msg);
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
  }
@@ -12,6 +12,7 @@ export class ProcessManager {
12
12
 
13
13
  this.runningProcess = null;
14
14
  this.restartScheduled = false;
15
+ this.autoRestart();
15
16
  }
16
17
 
17
18
  async startProcess(purposefulStop = false) {
@@ -148,13 +149,20 @@ export class ProcessManager {
148
149
  return;
149
150
  }
150
151
 
151
- const now = new Date();
152
- const [hour, minute] = this.options.dailyrestart_time.split(':').map(Number);
153
- const nextRestart = new Date();
154
- nextRestart.setHours(hour, minute, 0, 0);
155
- if (nextRestart < now) nextRestart.setDate(nextRestart.getDate() + 1);
152
+ const restartTimes = Array.isArray(this.options.dailyrestart_time) ? this.options.dailyrestart_time : [this.options.dailyrestart_time];
153
+ let earliestTime = null;
154
+ for (const timeStr of restartTimes) {
155
+ const [hour, minute] = timeStr.split(':').map(Number);
156
+ const now = new Date();
157
+ const restartTime = new Date();
158
+ restartTime.setHours(hour, minute, 0, 0);
159
+ if (restartTime < now) restartTime.setDate(restartTime.getDate() + 1);
160
+ if (!earliestTime || restartTime < earliestTime) {
161
+ earliestTime = restartTime;
162
+ }
163
+ }
156
164
 
157
- let timeUntilRestart = nextRestart - now;
165
+ let timeUntilRestart = earliestTime - new Date();
158
166
  if (timeUntilRestart <= 0) timeUntilRestart += 24*60*60*1000;
159
167
 
160
168
  this.logger.logSend(`Scheduled restart in ${Math.floor(timeUntilRestart / 1000 / 60)} minutes.`);
package/src/index.js CHANGED
@@ -14,7 +14,6 @@ export class Perpetual {
14
14
  }
15
15
 
16
16
  async run() {
17
- if (this.options.webhook_url) this.logger.startWebhookInterval();
18
17
  await this.processManager.startProcess();
19
18
 
20
19
  const rl = readline.createInterface({
package/src/test/test.js CHANGED
@@ -3,5 +3,8 @@ 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',
7
+ dailyrestart_enable: true,
8
+ dailyrestart_time: ['15:26', '15:27', '15:28', '15:29', '15:30', '15:31', '20:00'],
6
9
  })
7
10
  await perpetual.run();